Switch between digitalRead

Hey guys!

I'm new here and using arduino.
I'm having troubles with my code, I have to switches, one does two thing and the other those just one thing. And what I want is that when I'm using the second switch, the first one stops....
I really have no clue how to do it...

Can someone help? :slight_smile:

void setup()
{

pinMode (15, INPUT_PULLUP); //external switch baro/differential
lcd.begin(20, 4);
Wire.begin();
Serial.begin(115200);
if (digitalRead(15)){ // if switch in Baro mode
lcd.print("Sensor+Laser Tester ");
lcd.setCursor(0, 1);
lcd.print("Baro Sensor Init");
lcd.setCursor(0, 2);
lcd.print(" in progress");
lcd.setCursor(0, 3);
lcd.print("LM Design ");
delay (2000);
//Serial.println();

if (barometricSensor.init()) {
//Serial.println("Barometric sensor initialization succeeded!");
barometricSensor.enableSensor(Sodaq_LPS22HB::OdrOneShot);
lcd.setCursor(0, 2);
lcd.print(" Success");
delay (2000);
}
else {
//Serial.println("Barometric sensor initialization failed!");
lcd.setCursor(0, 2);
lcd.print("Sensor init failed ");
delay (1000);
lcd.clear();
lcd.setCursor(0, 2);
lcd.print("Going to laser mode ");
Laser = 1;
delay (2000);
LaserSetup();
}

//Serial.println("Done with setup!");
}else{ // if switch in diff sensor mode
lcd.print("Sensor+Laser Tester ");
lcd.setCursor(0, 1);
lcd.print("Diff Sensor Init");
lcd.setCursor(0, 2);
lcd.print(" in progress");
lcd.setCursor(0, 3);
lcd.print("LM Design ");
delay (1000);
//DiffPressSetup();
//Serial.println();

}

Serial.begin(9600);
pinMode(button, INPUT_PULLDOWN);
Wire.begin();
Serial.begin(9600);
if(digitalRead(9)){
lcd.print("Accel/Mag Test");
lcd.setCursor(0, 1);
lcd.print("");
lcd.print("Use Mag Sensor on X/Y axes");
lcd.setCursor(0, 3);
delay(2000);
LSM303();

} else {
lcd.clear();
}

}

void loop()
{
if (Laser == 1){
ReadLaser();
}
else {

if (digitalRead(15)) {
Serial.println ("EnteredBaro");
BaroPress();

}
else{
Serial.println ("EnteredDiff");
DiffPress();

}

if (digitalRead(9)){
Serial.println("EnteredAcc/Mag");
Accelsensor();

}
}

delay(900);

}

This is the code

"using the second switch the first one stops". Permanently, or just until you stop pressing the second one?
Which is the "first" and which is the "second" button. You have digital reads of pins 15 and 9. Are these your buttons?

 if (digitalRead(9) && digitalRead(15) == LOW){
// Button "9" must be pressed AND button "15" must NOT be pressed

Edit: Also your code won't compile. Please provide complete sketches.
What is "button"? Is it 9?
Otherwise you are missing a pinmode setting for pin 9.
If it is 9, why aren't you using "button" everywhere?

 if (digitalRead(button)){

Doesn't this conflict with your "positive logic" for digitalRead?

pinMode (15, INPUT_PULLUP);

AND Code tags for posting code on the forum look like this: [​code]put your code here[​/code]

Just until I stop pressing the second one. The "first" one is pin 15 and the "second" one is 9.

jonidohc16v:
Just until I stop pressing the second one. The "first" one is pin 15 and the "second" one is 9.

I would suggest using variables with meaningful names.
e.g. "button1" and "button2", or better still something like "startButton" and "stopButton" (if that is the function they perform).
Then use those names throughout your sketch.

Are they positive logic (high for a press) or negative logic (low for a press)?

pinMode (15, INPUT_PULLUP);

Where is the pinMode for 9? Is this it...

pinMode(button, INPUT_PULLDOWN);

If so, why a different pullup mode for each button?

pinMode(button, INPUT_PULLDOWN);

yes, that's the pinMode for 9

Well actually what I want is, that when I'm using 9, the pinMode 15 stops. But I don't know, because the 15 has two functions, one when switched up and another when switched down.

jonidohc16v:
pinMode(button, INPUT_PULLDOWN);

yes, that's the pinMode for 9

So one of your buttons is pull up and the other is pull down?
Okay - But why????
How do you have them wired?
Post a circuit diagram.

if digitalRead(button2) == LOW)
{
   //only when button2 is NOT pressed
   if (digitalRead(button1) == HIGH)
   {
      //do button1 function 1
   }
   else
   {
     //do button1 function 2
   }
}
else
{
  //do button2 function
}

Maybe you will need to change a HIGH to a LOW or vice versa depending on your wiring.

jonidohc16v:
I have to switches, one does two thing and the other those just one thing. And what I want is that when I'm using the second switch, the first one stops....

presumably these are not momentary switches since to test for there state in setup().

presumably if the 2nd switch is closed, you want to ignore the code processing the first switch. if that's true, simply check the condition of the 2nd switch before doing any of the 1st switch processing, both in setup or loop().

there are many other issues with your code. here are just a few

  • you set the serial bit rate (e.g. Serial.begin(9600)) three times and to 2 different rates
  • there's no INPUT_PULLDOWN -- INPUT or INPUT_PULLUP
Tst:55: error: 'INPUT_PULLDOWN' was not declared in this scope

     pinMode(button, INPUT_PULLDOWN);
  • you use "button" in a call to pinMode(). You should use the variable name in digitalRead() as well

  • i don't see the code for BaroPress(), DiffPress() or Accelsensor(). I assume it's similar to the code in setup() and the same functions should be called in setup() as in loop(), avoiding redundant code

next time you post code, click the "</>" (top left) and post inside the code tags

the code doesn't compile. you should post code that compiles unless there's a error you can't figure out

gcjr:

  • there's no INPUT_PULLDOWN -- INPUT or INPUT_PULLUP

Tst:55: error: 'INPUT_PULLDOWN' was not declared in this scope

On some non ATMega328/168 based platforms there is. Arduino Zero?

As the OP didn't state their platform (as far as I can see) and aren't complaining about the above error, my assumption (rightly or wrongly) is they are using a platform which does have pulldowns.

Ok guys, I did it using your recomendations

void loop()
{
 if (Laser == 1){
    ReadLaser(); 
    }
 else {

     if (digitalRead(9) == LOW) {

      if (digitalRead(15) == HIGH) {
     
      
     Serial.println ("EnteredBaro"); 
          BaroPress();
      }
  
       else{
        Serial.println ("EnteredDiff");
          DiffPress();

      }
 }  
   else {
      
      


    
        Serial.println("EnteredAcc/Mag");
           Accelsensor(); 
       }

     

    
      
     

     delay(900);
   
       
      
}

}

I change the loop and now it works. I feel so noob right now xD
But thank you guys

I did it using your recomendations

I note that you did not use sensible names for the pins though

It always amazes me that people work with sketches in such a mess, particularly when the IDE has an Auto Format command. Using it makes your code, or at least the part you posted, look like this

void loop()
{
  if (Laser == 1)
  {
    ReadLaser();
  }
  else
  {
    if (digitalRead(9) == LOW)
    {
      if (digitalRead(15) == HIGH)
      {
        Serial.println ("EnteredBaro");
        BaroPress();
      }
      else
      {
        Serial.println ("EnteredDiff");
        DiffPress();
      }
    }
    else
    {
      Serial.println("EnteredAcc/Mag");
      Accelsensor();
    }
    delay(900);
  }
}

So much easier to read without the multiple blank lines and with code blocks obvious from the tab levels. Do yourself a favour and Auto Format your code every now and again

UKHeliBob:
So much easier to read without the multiple blank lines and with code blocks obvious from the tab levels. Do yourself a favour and Auto Format your code every now and again

I will :slight_smile: