sensors – I can’t manage to upload multiple sketches

0
22

im doing a gardening project. It involves multiple sensors and even having the sketches for them, I don’t know how to make them run together and get a reading on them at the same time. Im really new with this. It’s my first arduino ever. I seem to manage getting a read on one of the sensors, but whenever I try to upload a new sketch, it fails to load. Here are a couple of the codes I want to run together. One is a temperature/humidity sensor and the other one a soil humidity sensor. Thanks in advance!!!

#include "DHT.h"          // DHT & AM2302 library


// Data pin connected to AM2302
#define DHTPIN 2

#define DHTTYPE DHT22       // DHT 22  (AM2302)

DHT dht(DHTPIN, DHTTYPE);   // LED pins

//////////////////////////////////////////////////////
//
// SETUP
//
void setup() {
  // Setup serial monitor
  Serial.begin(9600);
  dht.begin();
}


void loop() {

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow     sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then     something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println(F("Failed to read from DHT"));
  }
  else {
Serial.print(F("Humedad: ")); 
Serial.print(h);
Serial.print(F(" %t"));
Serial.print(F("Temperatura: ")); 
Serial.print(t);
Serial.println(F(" C"));
  }

  // Wait 3 seconds
  delay(3000);
}

The other code:

   //////////////////////////////////////////////
  //        ARDUINO SOIL MOISTURE DEMO        //
 //                                          //
//           http://www.educ8s.tv           //
/////////////////////////////////////////////



int sensorPin = A0;  
int sensorValue = 0;  
int percent = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {

  sensorValue = analogRead(sensorPin);
  percent = convertToPercent(sensorValue);
  printValuesToSerial();
  delay(1000);
}

int convertToPercent(int value)
{
  int percentValue = 0;
  percentValue = map(value, 1023, 465, 0, 100);
  return percentValue;
}

void printValuesToSerial()
{
  Serial.print("nnAnalog Value: ");
  Serial.print(sensorValue);
  Serial.print("nPercent: ");
  Serial.print(percent);
  Serial.print("%");
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here