user48456: 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("%");
}


user20533: 
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
}


48456: <p>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.</p>

<p>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.</p>

<p>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.</p>

<p>Thanks in advance!!!</p>

<pre><code>#include "DHT.h"          // DHT &amp; 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);
}
</code></pre>

<p><strong>The other code:</strong></p>

<pre><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("%");
}
</code></pre>
 20533: <blockquote>
  <p>I can't manage to upload multiple sketches</p>
</blockquote>

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

<blockquote>
  <p>... but whenever I try to upload a new sketch, it fails to load.</p>
</blockquote>

<p>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.</p>

<blockquote>
  <p>Here are a couple of the codes I want to run together ...</p>
</blockquote>

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

<p>something like:</p>

<pre><code>...
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
}
</code></pre>
