-1

I'm 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. I'm 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("\n\nAnalog Value: ");
  Serial.print(sensorValue);
  Serial.print("\nPercent: ");
  Serial.print(percent);
  Serial.print("%");
}
  • Only one sketch at a time. You will have to merge them (somehow) – Greenonline Jul 31 '18 at 15:43
  • 1
    @Greenonline, OP did it some hours ago. read the answer and comments – Juraj Jul 31 '18 at 16:14
  • @Juraj - good point, but then, in that case, the original question has been changed substantially – Greenonline Jul 31 '18 at 16:16
2

I can't manage to upload multiple sketches

The arduino can only run one sketch at a time. You can do multiple things in a single sketch, but you can only have one sketch.

... but whenever I try to upload a new sketch, it fails to load.

What exactly fails? You should be able to change sketches, the new sketch should replace the old sketch; If this isn't working you have another problem all together.

Here are a couple of the codes I want to run together ...

You will have to write a new sketch that combines the behavior of the two sketches.

something like:

...
setup()
{
    // common setup code goes here
}
...
loop()
{
    int sensor_reading_1 = analogRead(sensor_pin);
    float sensor_reading_2_h = dht.readHumidity();
    float sensor_reading_2_t = dht.readTemperature();             

    ... // do stuff with the readings
}
  • The error is because I used 2 Voids in 2 separate tabs, apparently. – guillo Jul 31 '18 at 1:00
  • 2
    @guillo umm ... what? that doesn't make any sense at all! – esoterik Jul 31 '18 at 1:04
  • Here's the error that appeared: "Redefinition of void setup" Cant manage to paste the entire error code here – guillo Jul 31 '18 at 1:34
  • A couple of hours in the making I managed to merge them together. Thanks for your replys tho! – guillo Jul 31 '18 at 4:31
  • 1
    @guillo just to be clear, it's not "I used 2 Voids", but "I declared 2 setups". You can have infinite void functions, since void is the return type, but every function should have a unique name – frarugi87 Jul 31 '18 at 7:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.