how to make to sensor as an input

here my coding

int led4 = 5;
#define Sensor A0 //lm35 is connected to pin 0
#define Sensor2 A1
int val = 0; //val is used to store state of input pin
int val2 = 0;

void setup() {

//pinMode(led1, OUTPUT);
//pinMode(led2, OUTPUT);
//pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(Sensor, INPUT);
pinMode(Sensor2, INPUT); //sets analog pin as input
Serial.begin(9600);
}

void loop() {

val = analogRead(Sensor);
float suhu = val*0.48828125;
Serial.print(“temperature1”);
Serial.print(suhu);
Serial.print(“C"); delay(500); val2 = analogRead(Sensor2); float suhu2 = val2*0.48828125; Serial.print("temperature2"); Serial.print(suhu2); Serial.print("C”);
delay(500);
if ((suhu>=20&&suhu<=30)&&(suhu2>=20&&suhu2<=30))
{
digitalWrite(led4, HIGH);
}

else if ((suhu<20)&&(suhu2<20))
{
//digitalWrite(led1, LOW);
digitalWrite(led4, LOW);
}
else if ((suhu>30)&&(suhu2>30))
{
//digitalWrite(led1, LOW);
digitalWrite(led4, LOW);
}

}

but when i try i just get, if two sensor in same condition (that’s lower or higher the led will turn off) but i want just one sensor turn off light when it low or high and if another sensor do the same with previous sensor the sensor have some function to turn off the led

thank before and sorry for poor english :smiley_cat:

I didn’t quite understand what your problem is, but I think if you change the && in the else ifs to || that will do what you want?

But if I read the code correctly, you can do that by taking the else ifs out and just turn the led off in an else (not an else if)

if ((suhu>=20&&suhu<=30)&&(suhu2>=20&&suhu2<=30))
    {
 digitalWrite(led4, HIGH);
 }
   
else
    {  
  digitalWrite(led4, LOW);
    }

ok thank you, but have solve it now just change the “else if” with “if”

You have implemented the following actions for the 9 possible conditions:

| S1 \ S2 | Low | In Range | High | | - | - | - | - | | Low | LED OFF | no change | no change | | In Range | no change | LED ON | no change | | High | no change | no change | LED OFF |

Is that what you wanted to do? If not, what did you want to do?

trhawky:
but when i try i just get, if two sensor in same condition (that’s lower or higher the led will turn off) but i want just one sensor turn off light when it low or high and if another sensor do the same with previous sensor the sensor have some function to turn off the led

Why don’t you put your program into a clear structure of handling

  • input
  • processing
  • output

I do it all the time for any kind of Arduino program.
So the loop function becomes very simple.

And you just have to write three funktions for input(), processing() and output() by yourself, depending on the actual program.

Example:

#define PIN_SENSOR1 A0
#define PIN_SENSOR2 A1
#define LED4 5
#define STRANGEFLOAT 0.48828125

float temp1, temp2;
boolean led4_ON;

void input() // Reading the input values
{
  temp1=analogRead(PIN_SENSOR1) * STRANGEFLOAT;
  temp2=analogRead(PIN_SENSOR2) * STRANGEFLOAT;
}

void processing() // Processing the programming logic
{
  // Now we can do as many logical changes as we like to do
  led4_ON=true; // just assume, everything is fine
  // but the LED is off under certain conditions
  if (temp1<20) led4_ON=false; 
  if (temp1>30) led4_ON=false;
  if (temp2<20) led4_ON=false;
  if (temp2>30) led4_ON=false;
  // until now, no physical change to the LED pin had been made
  // we just worked on the current logical state
}

void output() // setting/sending the output to the world outside the Atmega
{
  // now put the logical state to the output,
  // set LED pin HIGH or LOW and show results on Serial
  if (led4_ON) digitalWrite(LED4, HIGH);
  else  digitalWrite(LED4, LOW);
  Serial.print(temp1);
  Serial.print('\t');
  Serial.print(temp2);
  Serial.print('\t');
  if (led4_ON) Serial.println("ON");
  else Serial.println("OFF");
}


void setup() {
  Serial.begin(9600);
  pinMode(LED4, OUTPUT);
}


void loop() {
  input();
  processing();
  output();  
  delay(1000);
}

You see: If output shall not be sent to “LED” and “Serial”, but if you want to send the output to a text display, a graphical display, to a server on the internet or by e-mail, the programm would stay all the same except the “output()” function.

Or if you would decide that you don’t want to use your LM35 sensor, but some digital I2C-sensors to read the temperature, you just would change the “input()” function and all the rest of the program can stay the same.

And if the programming logic is not wat you want, you just change the processing() function.

You can write nearly every Arduino programm following these three steps in the loop function:

  • input
  • processing
  • output

P.S: Using this programming model it would even be easy to create fancy blinking LED output as well. For example if the LED state could not only be ON and OFF, but it also could be “SLOWBLINK” or “FASTBLINK”. Perhaps with this logical setting:

  • both temperatures are in range ==> LED slowly blinking
  • one temperature is out of range ==> LED fast blinking
  • both temperatures out of range ==> LED ON
    Please ask if you have any question about that.

ok thank guys i have clear it, but my sketch is just like this one


int led4 = 5;
#define Sensor A0 //lm35 is connected to pin 0
#define Sensor2 A1
int val = 0; //val is used to store state of input pin
int val2 = 0;

void setup() {

//pinMode(led1, OUTPUT);
//pinMode(led2, OUTPUT);
//pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(Sensor, INPUT);
pinMode(Sensor2, INPUT); //sets analog pin as input
Serial.begin(9600);
}

void loop() {

val = analogRead(Sensor);
float suhu = val*0.48828125;
Serial.print("temperature1 ");
Serial.print(suhu);
Serial.print("C"); delay(500); val2 = analogRead(Sensor2); float suhu2 = val2*0.48828125; Serial.print("temperature2 "); Serial.print(suhu2); Serial.print(" C ");
delay(500);
if ((suhu>=20)&&(suhu<=30))//&&(suhu2>=20&&suhu2<=30))
{
digitalWrite(led4, LOW);
}
if((suhu2>=20)&&(suhu2<=30)){
digitalWrite(led4, LOW);}
if (suhu<20){//&&(suhu2<20))
digitalWrite(led4, HIGH);}
if(suhu2<20){
digitalWrite(led4, HIGH);}
//digitalWrite(led1, LOW);
//digitalWrite(led4, LOW);

if (suhu>30)//&&(suhu2>30))
{
//digitalWrite(led1, LOW);
digitalWrite(led4, HIGH);
}
if(suhu2>30){
digitalWrite(led4, HIGH);}
}