arduino project almost working

I am building a controller for the automatic transmission in my rock crawler. the intention is to provide full manual control of the 4 forward gears. I have the controller shifting the gears as it needs to. i just need to add a neutral feature using an input pin. I have a bit of code written but it is not working correctly.

int gear = 1;
// output pins for transistor controlled solenoids
int solA = 7;
int solB = 6;
// input pins for up down /  with pullup resistors
int gearup = 9;
int geardown = 10;
// input pin for neutral safety switch(nss)
int nss = 8;



void setup() {
	pinMode(solA, OUTPUT);
	pinMode(solB, OUTPUT);
	pinMode(gearup, INPUT);
	pinMode(geardown, INPUT);
	pinMode(nss, INPUT_PULLUP);
}

void loop() {
	
	
	// intention is when nss pin 8 is high it will show GEAR 4 when the shift lever is in park, reverse or neutral the soleniod state for gear 4 serves as a neutral.
 //I cannot make this feature work!!!
	if (nss == HIGH) {
		digitalWrite(solA, 0);
		digitalWrite(solB, 0);
	}	
//intention is when nss is pulled low by placing the shift leever into drive thus grounding pin 8 it will start in first gear
// i cannot make this work!!
  if (nss == LOW) {
    digitalWrite(solA, 1);
    digitalWrite(solB, 0);
  }
	
	
	//this section works as it should
	if (gear == 1) {
		digitalWrite(solA, 1);
		digitalWrite(solB, 0);
		
	}
	if (gear == 2) {
		digitalWrite(solA, 1);
		digitalWrite(solB, 1);
		
		
	}
	if (gear == 3) {
		digitalWrite(solA, 0);
		digitalWrite(solB, 1);
	}
	if (gear == 4) {
		digitalWrite(solA, 0);
		digitalWrite(solB, 0);
	}
	
	//takes care of gear switching
	
	delay(180);//delay to prevent going through gears too quick from holding the button or pressing too long
	
	
	gear += digitalRead(geardown) - digitalRead(gearup); // non debounced! But may not be a problem because of the delay by gear change
	if (gear < 1) gear = 1;
		if (gear > 4) gear = 4;
			//limits to actual gearset
	
	
}

please advise me on how to make the neutral safety switch active in this code.

First, a few comments on structure. I would make the pin definitions either #define or const int to indicate that they should never change. I’d also make them all uppercase as this is pretty much a de facto indication of a constant in the C/C++ world.

Next, the bug is that you are never updating “nss” You check it, but it’s never initialized to anything. i.e., this

if (nss == LOW) {
    digitalWrite(solA, 1);
    digitalWrite(solB, 0);
  }

Should be

if (digitalRead(nss) == LOW)
{
    digitalWrite(solA, HIGH);
    digitalWrite(solB, LOW);
}
else
{
    ????
    Probably want your gear switching code here
}

Next, the bug is that you are never updating “nss” You check it, but it’s never initialized to anything. i.e., this

I’m agreeing on how the code should be different. But “the bug” is of a different kind then you explain. Imho, the bug is a misunderstanding of how to read GPIO pins and comparing it with low or high states. “nss” is defined as a variable, and according to the comments meant as a pin number (constant). But his error is in thinking that nss==LOW means the same as ‘is the pin-state of nss equal to LOW?’. It actually means “is 8 equal to LOW” or “is 8 equal to 0”. 8 is never equal to 0. Never equal to HIGH (1) either. The same goes for “gear”.

Jeeptronics, please consult the documentation on digitalRead how to read digital pin states and assign a variable to that.

https://www.arduino.cc/en/Reference/digitalRead

Thank you for the suggestions! I was able to get the controller working correctly.

Valen:

Next, the bug is that you are never updating “nss” You check it, but it’s never initialized to anything. i.e., this

I’m agreeing on how the code should be different. But “the bug” is of a different kind then you explain. Imho, the bug is a misunderstanding of how to read GPIO pins and comparing it with low or high states. “nss” is defined as a variable, and according to the comments meant as a pin number (constant). But his error is in thinking that nss==LOW means the same as ‘is the pin-state of nss equal to LOW?’. It actually means “is 8 equal to LOW” or “is 8 equal to 0”. 8 is never equal to 0. Never equal to HIGH (1) either. The same goes for “gear”.

Jeeptronics, please consult the documentation on digitalRead how to read digital pin states and assign a variable to that.

https://www.arduino.cc/en/Reference/digitalRead

Yes, that’s a much clearer way of explaining what I was trying to say :slight_smile: