Please read the post at the top of the forum about how to post your questions. There are plenty of people here who can and will help you, if we have the right information. Right now, we would need to read your mind about what is wrong with your program and what the rest of your code looks like.
#define groenPin 8 #define geelPin 9 #define roodPin 10
// Pin 13: Arduino has an LED connected on pin 13
// Pin 11: Teensy 2.0 has the LED on pin 11
// Pin 6: Teensy++ 2.0 has the LED on pin 6
// Pin 13: Teensy 3.0 has the LED on pin 13
#define sensor1Pin 2 #define sensor2Pin 3
// variables will change:
int sensor1State = 0, last1State=0; // variable for reading the pushbutton status
int sensor2State = 0, last2State = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(groenPin, OUTPUT);
pinMode(roodPin,OUTPUT);
pinMode(geelPin,OUTPUT);
// initialize the sensor pin as an input:
pinMode(sensor1Pin, INPUT_PULLUP);
digitalWrite(sensor1Pin, HIGH); // turn on the pullup
pinMode(sensor2Pin,INPUT_PULLUP);
digitalWrite(sensor2Pin,HIGH);
Serial.begin(9600);
}
void loop(){
// read the state of the pushbutton value:
float sensor1State = digitalRead(sensor1Pin);
float sensor2State = digitalRead(sensor2Pin);
// check if the sensor beam is broken
// if it is, the sensor1State is LOW:
if ((!sensor1State && last1State) && (!sensor2State && last2State)){
// turn LED off:
digitalWrite(groenPin, LOW);
digitalWrite(geelPin,HIGH);
delay(3000);
digitalWrite(geelPin,LOW);
digitalWrite(roodPin,HIGH);
}
else if ((sensor1State && !last1State)&&(sensor2State && !last2State)){
// turn LED on:
digitalWrite(groenPin, HIGH);
digitalWrite(roodPin,LOW);
digitalWrite(geelPin,LOW);
}
if ((sensor1State && !last1State)) {
Serial.println("Unbroken");
}
if (!sensor1State && last1State) {
Serial.println("Broken");
}
last1State = sensor1State;
last2State = sensor2State;
}
with "if ((!sensor1State && last1State) && (!sensor2State && last2State))" and with "else if ((sensor1State && !last1State)&&(sensor2State && !last2State))"
only the green LED is light up
when the sensor 1 and 2 are low, the yellow LED lights up and after 3 seconds it goes out and then the red LED lights up.
when the sensor 1 and 2 are high, only the green LED lights up
but with my 2 conditions, only the green LED lights up
i'll guess that you have two push buttons that ground an input when pressed. In other words, they are active low.
when you press both buttons, you expect the first case to be exercised and the green LED to turn on. And it does.
but for the second case, both buttons input have to become high simultaneously since you're comparing both inputs and their last inputs at the same time. considering that this condition is test 1000s of times a second, it's unlikely for that to happen.
you'll have to think about how recognize when the condition that triggers the LED to become yellow and then red and prevent it from repeatedly occurring.