LEDs alternate instead of turning off

Hi I’m in the middle of class and the robotc code for our stoplight isn’t working because the unexpected else statement is being ignored

turnLEDOn(redwest);
turnLEDOn(rednorth);
while(1==1)
{

if     (SensorValue(ultrawest<10))

{
turnLEDOn(yellownorth);
wait(3);
turnLEDOff(yellownorth);
turnLEDOn(rednorth);
wait(0.5);
turnLEDOn(greenwest);

}
if (SensorValue(ultranorth<10))
{
turnLEDOn(yellowwest);
wait(3);
turnLEDOff(yellowwest);
turnLEDOn(redwest);
wait(0.5);
turnLEDOn(greennorth);
}
}

Also if anyone could respond before class ends (1:10) that’d be great. Also I asked my teacher if I could do this he said yes.

Ok nvm it got fixed but the lights keep alternating instead of staying off, any fixes?

(Formatted for readability)

turnLEDOn(redwest);
turnLEDOn(rednorth);
while(1==1)
{

if     (SensorValue(ultrawest<10))

{
turnLEDOn(yellownorth);
wait(3);
turnLEDOff(yellownorth);
turnLEDOn(rednorth);
wait(0.5);
turnLEDOn(greenwest);

}
if (SensorValue(ultranorth<10))
{
turnLEDOn(yellowwest);
wait(3);
turnLEDOff(yellowwest);
turnLEDOn(redwest);
wait(0.5);
turnLEDOn(greennorth);
}
}

In both of your ‘if’s’ you don’t turn off the green light or the red light. That is probabaly the source of your problems.

2 Likes

Thank you so much, that fixed somethings, but now the code keeps repeating the if statements. Again thank you so much for responding so fast

{
turnLEDOn(greennorth);
turnLEDOn(greenwest);
while(1==1)
{

}
if (SensorValue(ultrawest<10))
{
turnLEDOff(greennorth);
turnLEDOn(yellownorth);
wait(3);
turnLEDOff(yellownorth);
turnLEDOn(rednorth);
wait(0.5);
turnLEDOff(redwest);
turnLEDOn(greenwest);

}

if (SensorValue(ultranorth<10))

{
turnLEDOff(greenwest);
turnLEDOn(yellowwest);
wait(3);
turnLEDOff(yellowwest);
turnLEDOn(redwest);
wait(0.5);
turnLEDOff(rednorth);
turnLEDOn(greennorth);
}

It will repeat your ‘if’ statements as long as the sensor value is <10. To fix this you could change the sensor value in the code after the light changing is done running, but you will have to manually reset it every time. Could also add a statement (in a different ‘if’ that you can manually run) that sets the value to <10 to help reset.

2 Likes

Thank you so much for your help!

1 Like