Calling a specific subroutine

I'm trying to set a condition to every subroutine. And to run only the subroutine whereof the condition is true.

Now is running all of the subroutines constantly.

How do i need to set the conditions? or how can i let run that one specific subroutine and skip the others?

Who can help me?

int sensePin =A0;
int sensorValue = 0;
int Button1 = 13;
int Button2 = 12;
int Button3 = 11;
int Buttonstate1 = 0;
int Buttonstate2 = 0;
int Buttonstate3 = 0;
void setup() {
  Serial.begin(9600);
  pinMode(13,INPUT);
  pinMode(12,INPUT);
  pinMode(11,INPUT);
 
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(2, OUTPUT);
  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(3, OUTPUT);
  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(4, OUTPUT);
  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(5, OUTPUT);
  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(6, OUTPUT);
  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(7, OUTPUT);
  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(8, OUTPUT);
  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(9, OUTPUT);
  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(10, OUTPUT);
  Serial.begin(9600);
}
// the loop function runs over and over again forever
void loop() {
  Buttonstate1 = digitalRead(Button1);
  Buttonstate2 = digitalRead(Button2);
  Buttonstate3 = digitalRead(Button3); 
if (Buttonstate1 == HIGH)
{
  LooplichtL(); // action A
  Serial.println("LooplichtL ON");
}
if (digitalRead (Button2) == HIGH)
{
  LooplichtR();  // action B
  Serial.println("LooplichtR ON");
}

sensorValue = analogRead (A0);
}
void LooplichtL () 
{
  digitalWrite(10, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(9, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(9, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(6, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(6, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(5, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(5, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(4, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(4, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(3, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(3, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);     // wait for a second
  digitalWrite(2, LOW);    // turn the LED off by making the voltage LOW
     digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);     // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
}
void LooplichtR ()
{
    digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);     // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(2, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(3, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(3, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(4, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(4, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(5, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(5, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(6, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(6, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(9, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);                       // wait for a second
  digitalWrite(9, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(10, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);     // wait for a second
  digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
}

What value do you get back from your digitalRead of the buttons?
Does that value match your expectations when you press and release the buttons?

Why the heck are you calling Serial.begin(9600); a billion times? You only need to call it once.

How are the inputs wired ? Do you have pulldown resistors on them or are they floating at an uncertain voltage ?

  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN as an output.

Be more careful when you copy/paste

You would do well to look at using an array of pin numbers to avoid the ugly, repetitive code. Names for the pins would also help make the code easier to read, debug and maintain.

How are your buttons wired?
Is digitalRead(Button1), digitalRead(Button2) and digitalRead(Button3) returning HIGH all the time?
Then maybe you have the logic upside down or the buttons wired wrong.

Usually the inputs are configured to pinMode(button, INPUT_PULLUP) and then wire button to ground and into input.
Then your buttons activate when digitalRead(button) is LOW.

I think I would create one function to handle the lights, which worked with a variable called direction, set by the buttons.

The lamp function should then either increase or decrease a variable indicating which lamps to turn on and off.
That way you could reverse the lights on the fly. Your implementation could have both functions running at the same time, i.e. fighting for the lamps.

/Mogens

Something like this:

#define NUMBER_OF_LAMPS 9
int lampOutputs[NUMBER_OF_LAMPS] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
int Button1 = 13;
int Button2 = 12;
int Buttonstate1 = 1;
int Buttonstate2 = 1;

void setup() {
  pinMode(Button1, INPUT_PULLUP);
  pinMode(Button2, INPUT_PULLUP);

  // Configure lamp outputs and set them LOW
  for (int i = 0; i < NUMBER_OF_LAMPS; i++) {
    pinMode(lampOutputs[i], OUTPUT);
    digitalWrite(lampOutputs[i], LOW);
  }
  // Set first lamp HIGH
  digitalWrite(lampOutputs[0], HIGH);
}

int oldDirection = 0;

void loop() {
  int direction = 0;

  Buttonstate1 = digitalRead(Button1);
  Buttonstate2 = digitalRead(Button2);

  if (!Buttonstate1) {
    direction = 1;
  }
  else if (!Buttonstate2) {
    direction = 2;
  }

  lampControl(direction);
}

void lampControl(int direction) {
  static int lamp = 0;
  
  if (direction == 1 && lamp < (NUMBER_OF_LAMPS - 1)) {
    digitalWrite(lampOutputs[lamp++], LOW);
    delay(10);
    digitalWrite(lampOutputs[lamp], HIGH);
  }
  else if (direction == 2 && lamp > 0) {
    digitalWrite(lampOutputs[lamp--], LOW);
    delay(10);
    digitalWrite(lampOutputs[lamp], HIGH);
  }
}
int lampOutputs[NUMBER_OF_LAMPS] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
int Button1 = 13;
int Button2 = 12;
int Buttonstate1 = 1;
int Buttonstate2 = 1;

Why do people persist in using int variables when byte is more than enough. To compound the problem, variables that will never change, such as pin numbers, are not declared const. Note other comments in the code snippet below

const byte lampOutputs[NUMBER_OF_LAMPS] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
const byte Button1 = 13;  //a better name would be an improvement too
const byte Button2 = 12;
byte Buttonstate1 = HIGH;  //why not use the proper name for the state ?
byte Buttonstate2 = HIGH;

The odd byte wasted here and there may not seem important but in extreme cases I have seen arrays that hold only 0 or 1 declared as int.

UKHeliBob:
Why do people persist in using int variables when byte is more than enough.

It's not a reason, but there is a potential trap in the fact that byte is unsigned. For example:

for (byte i = 8; i>=0; i--)

won't stop at zero

for (int i = 8; i>=0; i--)

or

for (char i = 8; i>=0; i--)

will work

UKHeliBob:

int lampOutputs[NUMBER_OF_LAMPS] = {2, 3, 4, 5, 6, 7, 8, 9, 10};

int Button1 = 13;
int Button2 = 12;
int Buttonstate1 = 1;
int Buttonstate2 = 1;



Why do people persist in using int variables when byte is more than enough. To compound the problem, variables that will never change, such as pin numbers, are not declared const. Note other comments in the code snippet below



const byte lampOutputs[NUMBER_OF_LAMPS] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
const byte Button1 = 13;  //a better name would be an improvement too
const byte Button2 = 12;
byte Buttonstate1 = HIGH;  //why not use the proper name for the state ?
byte Buttonstate2 = HIGH;




The odd byte wasted here and there may not seem important but in extreme cases I have seen arrays that hold only 0 or 1 declared as int.

I'm sorry - I'm learning too.
I'm used to platforms with a bit more space :slight_smile:

/Mogens