Go Down

Topic: I need help in formatting my code (Read 242 times) previous topic - next topic

Toxiic

So essentially I want to make my photo resistor turn on both red led lights interrupting the changeLights() loop than restoring it after two seconds. The problem is it only works after the changeLights() goes through the loop once then proceeds to get interrupted. How can I make so it gets interrupted instantly so both red lights turn no matter where it's at in the changeLights() once the threshold is surpassed?

Code: [Select]

int redLedPin=5;
int yellowLedPin=6;
int greenLedPin=4;
int redLedPin1=10;
int yellowLedPin1=9;
int greenLedPin1=8;
int blueLed=11;
int button=12;
int photoresistor = 0;
int d;
int threshold = 800;
void setup() {
 
  pinMode(redLedPin, OUTPUT);
  pinMode(yellowLedPin , OUTPUT);
  pinMode(greenLedPin, OUTPUT);
  pinMode(redLedPin1, OUTPUT);
  pinMode(yellowLedPin1 , OUTPUT);
  pinMode(greenLedPin1, OUTPUT);
  pinMode(blueLed,OUTPUT);
  pinMode(button,INPUT_PULLUP);
  pinMode(photoresistor, INPUT);
  Serial.begin(9600);
}



 void loop() {

   int v = analogRead(photoresistor);

   if (v > threshold) {
     digitalWrite(redLedPin, HIGH);
     digitalWrite(redLedPin1, HIGH);
     delay(2000);
   } else {
    changeLights();
   }
      Serial.print(v);
      delay(1000);
}

// the loop function runs over and over again forever
void changeLights() {
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin1,HIGH);
digitalWrite(redLedPin,LOW);
delay(5000);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(redLedPin1,HIGH);
digitalWrite(greenLedPin, LOW);
delay(2000);
digitalWrite(yellowLedPin, LOW);
digitalWrite(redLedPin, HIGH);
delay(2000);
digitalWrite(greenLedPin1, HIGH);
digitalWrite(redLedPin1,LOW);
delay(5000);
digitalWrite(greenLedPin1, LOW);
digitalWrite(yellowLedPin1, HIGH);
delay(2000);
digitalWrite(redLedPin,HIGH);
digitalWrite(redLedPin1,HIGH);
digitalWrite(yellowLedPin1,LOW);
delay(2000);
}


PaulS

Quote
How can I make so it gets interrupted instantly so both red lights turn no matter where it's at in the changeLights() once the threshold is surpassed?
Ctrl-A, Ctrl-X.

You need to completely re-write the code and NOT use a single call to delay(). The blink without delay example is a mandatory starting point, and state machines are a good place to spend some quality time with Mr. Google.
The art of getting good answers lies in asking good questions.

Go Up