Is there a way to stop or do another code while another code is running in TinkerCad?

20 views Asked by At

For a schoolwork, we were tasked on to make a traffic light with the implementation of a pedestrian button. Without any input, the stoplight should go Green for 10 seconds, Yellow for 3 seconds, Red for 10 seconds.

The problem is the pedestrian button part, while the Green is going for 10 seconds, any other code wouldn't work. It's supposed to be that regardless of time, when a button is pressed the it will immediately jump to yellow. Currently my code looks like this.

void setup()
{
  pinMode(4, INPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop()
{
  Button = digitalRead(4);
  while (Button == LOW) {
    digitalWrite(1, HIGH);
    delay(10000); // Wait for 10000 millisecond(s)
    digitalWrite(1, LOW);
    digitalWrite(2, HIGH);
    delay(3000); // Wait for 3000 millisecond(s)
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    delay(10000); // Wait for 10000 millisecond(s)
    digitalWrite(3, LOW);
  }
}

I could very well be approaching this from the wrong angle, any help would be useful. Thanks in advance.

I've tried doing a check every 0.1 (or lower) seconds but that would result in unnecessary enlargement with regards to the size of the code. I'm wondering if there's any way of making this look cleaner.

0

There are 0 answers