Press 2 buttons

Hi

I’m trying to make a gear shift.

The gear shift works fine up and down.
but I try to make it go to 0 when I press both buttons with this code:

//--- gear nutral

 if(gearUp == true && gearDn == true){
  Gear = 0;
  gearUp  = false;
  gearDn = false;}

but it does not work, it just changes 1 up and 1 quickly.

Can you help?

here is all the code.

boolean gearUp = false;
boolean gearDn = false;


int lastUpdate;
// --------- Delay

int shiftDelay = 50;

// --------- Button
int Buttonup = 10;
boolean ButtonUpPressed = false;
int Buttondown = 11;
boolean ButtonDownPressed = false;
boolean Toggle = false;
boolean ToggleDown = false;
int Gear = 0;

int DebounceUp = 0;
int DebounceDown = 0;

// --------------------- Outputs

int valve1 = 2;
int valve2 = 3;
int valve3 = 4;
int valve4 = 5;
int valve5 = 6;
int valve6 = 7;
int valve7 = 8;

void setup() {
  pinMode(12, OUTPUT); digitalWrite(12, HIGH);
  pinMode(Buttonup, INPUT);
  pinMode(Buttondown, INPUT);
  
  pinMode(valve1, OUTPUT);digitalWrite(valve1, HIGH);
  pinMode(valve2, OUTPUT);digitalWrite(valve2, HIGH);
  pinMode(valve3, OUTPUT);digitalWrite(valve3, HIGH);
  pinMode(valve4, OUTPUT);digitalWrite(valve4, HIGH);
  pinMode(valve5, OUTPUT);digitalWrite(valve5, HIGH);
  pinMode(valve6, OUTPUT);digitalWrite(valve6, HIGH);
  pinMode(valve7, OUTPUT);digitalWrite(valve7, HIGH);
  Serial.begin(9600);

}

void loop() {
  

// UP----------------------------------------------

if(digitalRead(Buttonup) == LOW)
  {
    DebounceUp++;
  }else 
   {
    DebounceUp = 0;
    ButtonUpPressed = false;
    Toggle = false;
   }
   
   if(DebounceUp >= 5000)
   {
    ButtonUpPressed = true;
   }
   
   if(ButtonUpPressed == true && Toggle == false)
  {
    Toggle = true;

    gearUp = true;
    
    
    
    
 }

// DOWN---------------------------------------------------------

if(digitalRead(Buttondown) == LOW)
  {
    DebounceDown++;
  }else 
   {
    DebounceDown = 0;
    ButtonDownPressed = false;
    ToggleDown = false;
   }
   if(DebounceDown >= 5000)
   {
    ButtonDownPressed = true;
   }
   
   if(ButtonDownPressed == true && ToggleDown == false)
  {
    ToggleDown = true;

    gearDn = true;

    
    
    
    
    
}
//--- gear nutral

 if(gearUp == true && gearDn == true){
  Gear = 0;
  gearUp  = false;
  gearDn = false;
}

//--- gear up

  if (gearUp == true && Gear<6)
       Gear++;
       gearUp  = false;

//--- gear dn

if (gearDn == true && Gear>0){
       Gear--;
       gearDn = false;
}

if(lastUpdate != Gear){
  lastUpdate = Gear;
  
  Serial.println(Gear);
  
switch (Gear) {
    case 0:
      digitalWrite(valve1, LOW);
      delay(shiftDelay);
      digitalWrite(valve1, HIGH);
     
      
      digitalWrite(valve2, HIGH);
      digitalWrite(valve3, HIGH);
      digitalWrite(valve4, HIGH);
      digitalWrite(valve5, HIGH);
      digitalWrite(valve6, HIGH);
      digitalWrite(valve7, HIGH);
      
      break;
    case 1:
      digitalWrite(valve1, HIGH);
      
      digitalWrite(valve2, LOW);
      delay(shiftDelay);
      digitalWrite(valve2, HIGH);
      
      digitalWrite(valve3, HIGH);
      digitalWrite(valve4, HIGH);
      digitalWrite(valve5, HIGH);
      digitalWrite(valve6, HIGH);
      digitalWrite(valve7, HIGH);
      
      break;
    case 2:
      digitalWrite(valve1, HIGH);
      digitalWrite(valve2, HIGH);
      
      digitalWrite(valve3, LOW);
      delay(shiftDelay);
      digitalWrite(valve3, HIGH);
      
      digitalWrite(valve4, HIGH);
      digitalWrite(valve5, HIGH);
      digitalWrite(valve6, HIGH);
      digitalWrite(valve7, HIGH);
      break;
    case 3:

    
      digitalWrite(valve1, HIGH);
      digitalWrite(valve2, HIGH);
      digitalWrite(valve3, HIGH);
      
      digitalWrite(valve4, LOW);
      delay(shiftDelay);
      digitalWrite(valve4, HIGH);
      
      digitalWrite(valve5, HIGH);
      digitalWrite(valve6, HIGH);
      digitalWrite(valve7, HIGH);
      break;
    case 4:
      digitalWrite(valve1, HIGH);
      digitalWrite(valve2, HIGH);
      digitalWrite(valve3, HIGH);
      digitalWrite(valve4, HIGH);
      
      digitalWrite(valve5, LOW);
      delay(shiftDelay);
      digitalWrite(valve5, HIGH);
      
      digitalWrite(valve6, HIGH);
      digitalWrite(valve7, HIGH);
      break;
    case 5:
      digitalWrite(valve1, HIGH);
      digitalWrite(valve2, HIGH);
      digitalWrite(valve3, HIGH);
      digitalWrite(valve4, HIGH);
      digitalWrite(valve5, HIGH);
      
      digitalWrite(valve6, LOW);
      delay(shiftDelay);
      digitalWrite(valve6, HIGH);
      
      digitalWrite(valve7, HIGH);
      break;
    case 6:
      digitalWrite(valve1, HIGH);
      digitalWrite(valve2, HIGH);
      digitalWrite(valve3, HIGH);
      digitalWrite(valve4, HIGH);
      digitalWrite(valve5, HIGH);
      digitalWrite(valve6, HIGH);
      
      digitalWrite(valve7, LOW);
      delay(shiftDelay);
      digitalWrite(valve7, HIGH);
      break;
  }

}
}

How are the inputs wired ?

Have you got pullup resistors in the circuit to ensure that the inputs are always kept at a known voltage and not floating at an unknown voltage ?

You should allow a little time for the button presses. You're not going to be able to press both buttons at exactly the same nanosecond so the controller will always see one before the other.

UKHeliBob:
How are the inputs wired ?

Have you got pullup resistors in the circuit to ensure that the inputs are always kept at a known voltage and not floating at an unknown voltage ?

yes i have a 10k ohm resistor as puldown on both buttons.

Delta_G:
You should allow a little time for the button presses. You're not going to be able to press both buttons at exactly the same nanosecond so the controller will always see one before the other.

where do you want that delay?

Delta_G:
You should allow a little time for the button presses.

That will delay the shift associated with each button though, since it's only after that "little time" that it would know the second button's not coming, and it must shift. That may or may not be a problem to OP.

I'd go with a third button.

okay i need to go fast.
but there must be a way to get i to work a still be fast... i hope.

brunokc:
yes i have a 10k ohm resistor as puldown on both buttons.

If you have pulldown resistors in place then

if(digitalRead(Buttonup) == LOW)
  {
    DebounceUp++;
  }else 
   {
    DebounceUp = 0;
    ButtonUpPressed = false;
    Toggle = false;
   }

does not make sense to me as the Buttonup pin will be LOW most of the time.

Maybe I have misunderstood what you are doing.

brunokc:
okay i need to go fast.
but there must be a way to get i to work a still be fast... i hope.

The wait time needn't be huge. You think the buttons are pressed at the same time, the controller knows differently. You at least need to allow loop() to whiz round a bunch of times to see each time if the second button is pressed, before it decides to shift.

Have you thought of a third, "go to neutral", button? Way simpler.

kenwood120s:
The wait time needn't be huge. You think the buttons are pressed at the same time, the controller knows differently. You at least need to allow loop() to whiz round a bunch of times to see each time if the second button is pressed, before it decides to shift.

Have you thought of a third, "go to neutral", button? Way simpler.

I would make it as simple as I could, so there were no extra buttons.

UKHeliBob:
If you have pulldown resistors in place then

if(digitalRead(Buttonup) == LOW)

{
   DebounceUp++;
 }else
  {
   DebounceUp = 0;
   ButtonUpPressed = false;
   Toggle = false;
  }



does not make sense to me as the Buttonup pin will be LOW most of the time.

Maybe I have misunderstood what you are doing.

i just used the debounc a nother have made. i dont really know all that much about it.

I would make it as simple as I could, so there were no extra buttons.

Pressing 2 buttons at the same time sounds to me more complicated to operate that pressing one button with a single purpose.

i just used the debounc a nother have made. i dont really know all that much about it.

Can you see what I meant ?

if(digitalRead(Buttonup) == LOW)
  {
    DebounceUp++;
  }else 
   {
    DebounceUp = 0;
    ButtonUpPressed = false;
    Toggle = false;
   }

If you have pulldown resistors then the Buttonup pin will be LOW most of the time and DebounceUp will increment rapidly and in the else clause of the if/else setting ButtonUpPressed to false when the pin is HIGH does not make logical sense. Are you sure that you have pulldown resistors and not pullup resistors ?

UKHeliBob:
Can you see what I meant ?

if(digitalRead(Buttonup) == LOW)

{
DebounceUp++;
}else
{
DebounceUp = 0;
ButtonUpPressed = false;
Toggle = false;
}




If you have pulldown resistors then the Buttonup pin will be LOW most of the time and DebounceUp will increment rapidly and in the else clause of the if/else setting ButtonUpPressed to false when the pin is HIGH does not make logical sense. Are you sure that you have pulldown resistors and not pullup resistors ?

i have a 10k ohm from 0v to the input on pin 10 and 11 (the input pins of up and down shift button.)
like this

brunokc:
I would make it as simple as I could, so there were no extra buttons.

I've worked on many machines where the engineer cut the hardware to the bone. That's okay if the item still meets spec. The problem comes about a year later when we 'change the way we do business'. This generally involves logical gymnastics to accomplish something that would be a snap if there were another sensor/input available. Sometimes just adding another switch IS the simpler way.

What is output 12?
Always stay high?

Its just to make a extra 5v :slight_smile:

UKHeliBob:
Pressing 2 buttons at the same time sounds to me more complicated to operate that pressing one button with a single purpose.

Totally. And given it has to have the built in delay (not a delay() delay :wink: , but a lag let's call it) to see if the second button is still going to be pressed, there will ALWAYS be the chance of a false change up or down if the operator's a split second late on the second button, like a finger slips and misses the press. Also going to neutral quickly, with an unambiguous operation, may well be an added safety feature, to disengage in an emergency.

I think anyone who uses their Arduino to build any part of the safety critical components of their vehicle and drive it on the street in total defiance of the licence of both the Arduino and the chip in it, should have to put a BIG decal on their hood so I can know to take evasive action when I see them coming. Something based on a dim lightbulb might be appropriate.