1

I have 3 analog input(0-1024). I want two outputs based on levels of analog inputs. Eg. inputs have 2 levels. for A0 at 300,digital output pin3 turns ON; and A0 at 500, digital output pin 4 turns ON. Similar for all analog inputs have two levels. Digital output pin 3 turns ON for 1st level and for 2nd level, pin 4 should turn ON. Both should not turn ON at same time. Also, pin 3, should turn ON for specific time in a day for 1 hour.

Problem Facing: (1) I tried this program but outputs doesnt switch according to levels of analog inputs. Once Pin 3 turns ON, it doesnt let pin4 to turn ON. Also, after removing analog inputs, LED remains ON as it is (2) RTC doesnt turn ON on correct time.

#include<DS3231.h>
#include<Wire.h>
DS3231 rtc(SDA,SCL);
Time t;
const int OnHour=15;  // specified time to turn pin3 ON
const int OnMin=26;
const int OffHour=15;
const int OffMin=28;
int CO2=0;
int Temp=0;
int Humidity=0;

void setup() {

Serial.begin(115200);
rtc.begin();
for(int i=2;i<6;i++){
  pinMode(i, OUTPUT);
}
 }

void loop() {
  t=rtc.getTime();
  delay(1000);
  CO2=analogRead(A0);   //analog input 1
  Temp=analogRead(A1);   //analog input 2
  Humidity=analogRead(A2); //analog input 3

  if((CO2>500)&&(CO2<700)) //500 is low level and 700 is high level
  {
    digitalWrite(2,HIGH);  //indicator LED for low level
    digitalWrite(4,HIGH);  //actual output for low level
    digitalWrite(3,LOW);   //indicator LED for high level
    digitalWrite(5,LOW);   //actual output for high level


  }
  else if(CO2>700) //700 is high level
  {
    digitalWrite(3,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(4,LOW);

  }

  if((Temp>450)&&(Temp<600))
  {
    digitalWrite(4,HIGH);
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(5,LOW);

  }
  else if(Temp>600)
  {
    digitalWrite(3,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(4,LOW);

  }

  if((Humidity>300)&&(Humidity<850))
  {
    digitalWrite(4,HIGH);
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(5,LOW);

  }
  else if(Humidity>850)
  {
    digitalWrite(5,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(4,LOW);

  }

  if (t.hour==OnHour && t.min==OnMin)
  {
    digitalWrite(2,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(3,LOW);
   digitalWrite(5,LOW);

  }
  else if(t.hour==OffHour && t.min==OffMin)
  {
    digitalWrite(2,LOW);
    digitalWrite(4,LOW);
    digitalWrite(3,LOW);
    digitalWrite(5,LOW);

 } 
}
5
  • 1
    Please format your code correctly like you did with your first code. You can put a horizontal line between the codes by adding --- surrounded by free lines. Also please be more specific how the codes don't work. Explain, what the code does and what you expected from it. And do you really want to have 2 seperate codes? – chrisl Dec 18 '19 at 22:16
  • I don't see how the code isn't working, maybe it's because of this line: for(int i=2;i<6;i++){ pinMode(i, OUTPUT); }. try defining pin 2-6 as OUTPUT one by one instead of using that method... – ElectronSurf Dec 19 '19 at 7:27
  • 1
    The first problem I see is that you did not set the actual time of the RTC. Do you program it with another piece of code? The second problem I see is, that you are manipulating all LEDs in all conditional cases. Probably you overwrite the information of CO2 with Humidity or Temp or both. But as far as I don't know what the LEDs mean and how exactly you want them to behave, this is just a rough guess. – Peter Paul Kiefer Dec 19 '19 at 9:32
  • Your description is not clear, but definitely different from your code. >"Also I put other 3 LEDs to read which analog input is higher." I don't see such a comparison. – DataFiddler Dec 19 '19 at 12:11
  • 1
    thank you for sparing your time to read my question. I have edited my question here. I guess it can be clearly pictured now. – Varun Patel Dec 19 '19 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.