OiO.lk Blog C++ Problem to stop a function and restart it at the same location
C++

Problem to stop a function and restart it at the same location


I have a problem with my Arduino code. I’m building a small traffic-light system which I need for my base project.

Now I have a problem, I can’t get the program to stop when the button is clicked twice and to continue running in the same place when it is clicked again.

#define one_Red 3
#define one_Yellow 4
#define one_Green 5
#define two_Red 6
#define two_Yellow 7
#define two_Green 8
#define redLed 10
#define greenLed 9
#define button 2

int programRunning = false;
int brightness = 0;
int fadeAmount = 5;
unsigned long previousMillis = 0;
const long interval = 30;

unsigned long taskStartTime = 0;
const unsigned long firstTaskDuration[] = {1000, 2000, 3000, 2000, 2000};
const unsigned long resetTaskDuration[] = {1000, 2000, 3000, 2000, 2000};
int currentStep = 0;

unsigned long lastButtonPress = 0;
int buttonPressCount = 0;

void setup()
{
  pinMode(one_Red, OUTPUT);
  pinMode(one_Yellow, OUTPUT);
  pinMode(one_Green, OUTPUT);
  pinMode(two_Red, OUTPUT);
  pinMode(two_Yellow, OUTPUT);
  pinMode(two_Green, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(button, INPUT_PULLUP);

  digitalWrite(one_Red, HIGH);
  digitalWrite(one_Yellow, HIGH);
  digitalWrite(one_Green, LOW);
  digitalWrite(two_Red, LOW);
  digitalWrite(two_Yellow, HIGH);
  digitalWrite(two_Green, HIGH);
  digitalWrite(redLed, HIGH);
}

void loop()
{
  pulseRedLed();
  handleButtonPress();

  if (programRunning)
  {
    runResetTask();
  }
  else
  {
    runFirstTask();
  }
}

void handleButtonPress()
{
  if (digitalRead(button) == LOW)
  {
    unsigned long currentMillis = millis();
    
    if (currentMillis - lastButtonPress > 800) 
    {
      lastButtonPress = currentMillis;
      buttonPressCount++;

      if (buttonPressCount == 2)
      {
        programRunning = !programRunning;
        buttonPressCount = 0; 
      }
    }
  }
  else
  {
    buttonPressCount = 0;
  }
}

void runFirstTask()
{
  if (digitalRead(button) == LOW && currentStep == 0)
  {
    taskStartTime = millis();
    currentStep = 1;
  }

  if (currentStep > 0)
  {
    unsigned long currentMillis = millis();
    if (currentMillis - taskStartTime >= firstTaskDuration[currentStep - 1])
    {
      taskStartTime = currentMillis;
      switch (currentStep)
      {
      case 1:
        digitalWrite(one_Green, HIGH);
        digitalWrite(one_Yellow, LOW);
        break;
      case 2:
        digitalWrite(one_Yellow, HIGH);
        digitalWrite(one_Red, LOW);
        break;
      case 3:
        digitalWrite(two_Red, HIGH);
        digitalWrite(two_Yellow, LOW);
        break;
      case 4:
        digitalWrite(two_Yellow, HIGH);
        digitalWrite(two_Green, LOW);
        programRunning = true;
        currentStep = 0;
        return;
      }
      currentStep++;
    }
  }
}

void runResetTask()
{
  if (digitalRead(button) == LOW && currentStep == 0)
  {
    taskStartTime = millis();
    currentStep = 1;
  }

  if (currentStep > 0)
  {
    unsigned long currentMillis = millis();
    if (currentMillis - taskStartTime >= resetTaskDuration[currentStep - 1])
    {
      taskStartTime = currentMillis;
      switch (currentStep)
      {
      case 1:
        digitalWrite(two_Green, HIGH);
        digitalWrite(two_Yellow, LOW);
        break;
      case 2:
        digitalWrite(two_Yellow, HIGH);
        digitalWrite(two_Red, LOW);
        break;
      case 3:
        digitalWrite(one_Red, HIGH);
        digitalWrite(one_Yellow, LOW);
        break;
      case 4:
        digitalWrite(one_Yellow, HIGH);
        digitalWrite(one_Green, LOW);
        programRunning = false;
        currentStep = 0;
        return;
      }
      currentStep++;
    }
  }
}

void pulseRedLed()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    analogWrite(redLed, brightness);
    brightness += fadeAmount;

    if (brightness <= 0 || brightness >= 255)
    {
      fadeAmount = -fadeAmount;
    }
  }
}

I have tried to revise the method “handleButtonPress” with no success at the moment.



You need to sign in to view this answers

Exit mobile version