button state latch. i think?

i wrote this code to shift my 6 speed and need it to hold onto the last gear selected. the shifter has 6 micro switch to select a gear (1 each)and when shifting from one to a new one there is no switch pressed.

i think a something like ....

int gearnow = 0
int gearnext = 0

not sure how to insert this though :frowning:

if gearnow=gearnext ()

int gearin1 = A0;                            // six SHIFT INPUTS LOW/ACTIVCE  
int gearin2 = A1;
int gearin3 = A2;
int gearin4 = A3;
int gearin5 = A4;
int gearin6 = A5;

int EDS1= 3;                             // solenoid  outputS
int EDS2= 5;    
int EDS3= 6;    
int EDS4= 9;    
int EDS5= 10; 
int MV1 = 2;   
 
void setup() 
{
 pinMode(gearin1, INPUT_PULLUP);              // shift switch inputs
 pinMode(gearin2, INPUT_PULLUP);  
 pinMode(gearin3, INPUT_PULLUP);  
 pinMode(gearin4, INPUT_PULLUP);
 pinMode(gearin5, INPUT_PULLUP);  
 pinMode(gearin6, INPUT_PULLUP); 
 
 pinMode(EDS1, OUTPUT);                   // solenoid outputs
 pinMode(EDS2, OUTPUT);
 pinMode(EDS3, OUTPUT);
 pinMode(EDS4, OUTPUT);
 pinMode(EDS5, OUTPUT); 
 pinMode(MV1,      OUTPUT); 
}

void loop(){
                                              
                                         // FIRST GEAR
if  (digitalRead(gearin1) == LOW  &&          
     digitalRead(gearin2) == HIGH &&
     digitalRead(gearin3) == HIGH &&
     digitalRead(gearin4) == HIGH &&
     digitalRead(gearin5) == HIGH &&
     digitalRead(gearin6) == HIGH )
    {
    analogWrite(EDS1, 240);
    analogWrite(EDS2, 240);
    analogWrite(EDS3, 0);  
    analogWrite(EDS4, 0);
    analogWrite(EDS5, 0);
    digitalWrite(  MV1,  HIGH);
    }                                             // SECOND GEAR 
if (digitalRead(gearin1) == HIGH  &&          
     digitalRead(gearin2) == LOW  &&
     digitalRead(gearin3) == HIGH &&
     digitalRead(gearin4) == HIGH &&
     digitalRead(gearin5) == HIGH &&
     digitalRead(gearin6) == HIGH )
    {  
    analogWrite(EDS1, 240);
    analogWrite(EDS2, 240);
    analogWrite(EDS3, 240);
    analogWrite(EDS4, 240);
    analogWrite(EDS5, 0);
    digitalWrite(  MV1,  HIGH);
    }                                           // THIRD GEAR
if  (digitalRead(gearin1) == HIGH &&          
     digitalRead(gearin2) == HIGH &&
     digitalRead(gearin3) == LOW  &&
     digitalRead(gearin4) == HIGH &&
     digitalRead(gearin5) == HIGH &&
     digitalRead(gearin6) == HIGH )                 
    { 
    analogWrite(EDS1, 0);
    analogWrite(EDS2, 0);
    analogWrite(EDS3, 0);  
    analogWrite(EDS4, 240);
    analogWrite(EDS5, 0);
    digitalWrite(  MV1,  LOW);
    }                                              // FOURTH GEAR
if (digitalRead(gearin1) == HIGH  &&          
     digitalRead(gearin2) == HIGH &&
     digitalRead(gearin3) == HIGH &&
     digitalRead(gearin4) == LOW  &&
     digitalRead(gearin5) == HIGH &&
     digitalRead(gearin6) == HIGH )                
    { 
    analogWrite(EDS1, 240);
    analogWrite(EDS2, 240);
    analogWrite(EDS3, 0);  
    analogWrite(EDS4, 0);
    analogWrite(EDS5, 0);
    digitalWrite(  MV1,  HIGH);
     
    }                                            // FIFTH GEAR
if (digitalRead(gearin1) == HIGH  &&          
     digitalRead(gearin2) == HIGH &&
     digitalRead(gearin3) == HIGH &&
     digitalRead(gearin4) == HIGH &&
     digitalRead(gearin5) == LOW  &&
     digitalRead(gearin6) == HIGH )                  
    {   
    analogWrite(EDS1, 0);
    analogWrite(EDS2, 0);
    analogWrite(EDS3, 0);  
    analogWrite(EDS4, 0);
    analogWrite(EDS5, 0);
    digitalWrite(  MV1,  HIGH);
    }                                                 // SIXTH GEAR
if (digitalRead(gearin1) == HIGH  &&          
     digitalRead(gearin2) == HIGH &&
     digitalRead(gearin3) == HIGH &&
     digitalRead(gearin4) == HIGH &&
     digitalRead(gearin5) == HIGH &&
     digitalRead(gearin6) == LOW  )    
    { 
    analogWrite(EDS1, 0);
    analogWrite(EDS2, 240);
    analogWrite(EDS3, 240);
    analogWrite(EDS4, 0);
    analogWrite(EDS5, 0);
    digitalWrite(  MV1,  HIGH);
    } 
}

Looking briefly it looks good. Valid inputs result in a determinate action and illegal inputs like 2 or more cctive inputs will do nothing.
However, think about fast switching from gear a to gear b, especially from gear 6 to 1..... Concider the responce time of the mechanics and protect it in the software. After one change, block switches for the time needed... My thinking.....

i have no idea what you are saying but thanks for the reply

like adding a second (or sometime) delay after each shift? then there would be a window to mechanically move without any new gear selected?

I try again. Your code looks okej. Actvivating a gear takes some little time to get in full operartion as well as deactivating the gear takes some time to be done. Suppose You run in gear 1 and You press for gear 2.
Gear 1 needs to be released before gear 2 is activated. Otherwise both gear 1 and 2 will be active due to the release time of gear 1. Gearbox will blow up.

sorry it is an automatic transmission and likes gear overlap

Okey. Then all is well. Any conciderations, limits, switching gears quickly like several times per second? If not, just go on, test it out being careful.

it works fine other than when there is no gear input ( between gears). it will kinda act like there is noise and get random gear selected until a new gear IS selected

Your inputs, gear selectors, are all INPUT_PULLUP and that’s good.
What is the noice You write about? Electronical or from the gearbox?
Use the possibility to use Serial.print in Seriial monitor. There You can print out like when an output is activated and at what millisecond, using mllis().
Does that help You? I’ve got a bed to visit now…

while driving if i come out of a gear selection and do not enter another it will grab some random gear in and out. like a high speed random on the output config.
i got a 12v smoother piece on the supply power so i think it is fine
i dont know how to code in the serial stuff
thanks though!

dam, my little mosfet boards died magically tonight! they are stuck passing current......
now i have no parts to runt he program with.