I tried to make code following your instruction about mills, and it looks like this. Unfortunately, it is not working. Is there problem with my code?

Here is my code:

#include <LiquidCrystal.h>

Servo myservo;  // create servo object to control a servo

// These constants won't change. They're used to give names to the pins used:
const int analogInPin1 = A0;  // Analog input pin that the photosensor is attached to
const int analogInPin2 = A1;  // Analog input pin that the photosensor is attached to
const int analogInPin3 = A2;  // Analog input pin that the photosensor is attached to
const int analogInPin4 = A3;  // Analog input pin that the photosensor is attached to
int sensorValue1;   
int sensorValue2;   
int sensorValue3;   
int sensorValue4;   
unsigned long m;

//declare counter as an input
int Chactr =0;
int Disctr =0;

byte state; 

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.write(0);  // back to 0 degrees.
  delay(1000); // wait for a second.
  pinMode(13, OUTPUT); // initialize digital pin 13 as an output.
  Serial.begin(9600);  // initialize serial communications at 9600 bps:


void loop() {

  // read the analog in value:
  sensorValue1 = analogRead(analogInPin1);
  sensorValue2 = analogRead(analogInPin2);
  sensorValue3 = analogRead(analogInPin3);
  sensorValue4 = analogRead(analogInPin4);


  // print the results to the Serial Monitor:
  Serial.print("sensor1 = ");

  Serial.print("sensor2 = ");

  Serial.print("sensor3 = ");

  Serial.print("sensor4 = ");

  Serial.print("Led State:  ");

  if (sensorValue1 >=569 && sensorValue2 >=520 && sensorValue3 >=419 && sensorValue4 >=250)

  if(sensorValue1 <=568 && sensorValue2 <=519 &&sensorValue3 <=418 && sensorValue4 <=249)

  if (sensorValue1 >=569 && sensorValue2 >=520 && sensorValue3 >=419 && sensorValue4 >=250 && m==millis())
    digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
    delay(5000); // wait for a second

    myservo.attach (9);

    myservo.detach ();
    delay (1000);

    Disctr = Disctr + 1;


    Serial.print("Battery Full: Discharge ON");
  else if (sensorValue1 ==0 && sensorValue2 ==0 && sensorValue3 ==0 && sensorValue4 ==0 )

  if(sensorValue1 >=1 && sensorValue2 >=1 && sensorValue3 >=1 && sensorValue4 >=1)

  if(sensorValue1 ==0 && sensorValue2 ==0 && sensorValue3 ==0 && sensorValue4  ==0 && m==millis())
    digitalWrite(13, HIGH ); // turn the LED On by making the voltage high
    delay(5000); // wait for a second    

    Chactr = Chactr + 1;

    Serial.print("Battery Empty: Charging ON");
    lcd.print("Charge Cycle: ");


    lcd.print("Discha Cycle: ");


  • 1
    What exactly is your problem with your sketch? What code have you written until now? How does your circuit look like (schematic)? Please give us more information. You can edit your question for that.
    – chrisl
    Sep 18 '18 at 10:24
  • The point of using millis is to get rid of all of the delay calls. You can't leave any of them. While those delay calls are running your millis code isn't and will still miss doing what you want it to do.
    – Delta_G
    May 19 '20 at 16:46

So what you want to do is be able to activate the servo when 4 green bars appear over the course of one minute correct?

I think you would need to create three if statements and one new variable. The first if statement will be, if it detects four green bar, then m=(millis() + 60000). Then you will need to nest another if statement in here to say if it detects ONLY three bar, then m = 0. Your third if the statement is if it detects the four green bars AND m == millis() then the servo will activate.

This is how I would do it, but I am not very experienced. I'm pretty sure others will have a cleaner and simpler code but this should work.

  • Thank you for the help.But one more question. Even there are only three bars, the LED sensor ckt still senses the 4th bar because the 4th bar is blinking and the servo will activate. The LED sensor ckt shoud ignore the blinking bars of the battery in order for the servo not to move. The servo should activate once the 4bars appear steadily and no more blinking.any idea?Thank you.
    – lonelyboy
    Sep 20 '18 at 0:39
  • I understand that even with only three bars, the 4th one will blink. In terms of the logic of how this code will work it will be like this: Once the fourth bar appears for less than a second, m = miillis() + 60000. Then it will disappear and only three bars will appear. This will then set m = 0 and the process will repeat UNTIL the fourth bar stays on. When the fourth bar stays on, then m will not equal 0 anymore and m will be millis() + 600000. As milis() continue to increase for up to a minute (at which m == millis()), then the servo will activate.
    – Chris Do
    Sep 22 '18 at 2:30
  • 1
    You've got a bug in this answer code if it runs past rollover. Always handle millis by using subtraction to see how long it has been. Never use addition to try to predict the time in the future. Save the old timestamp and compare to that.
    – Delta_G
    May 19 '20 at 16:44

Your Answer

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

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