Switch Values Not Changing

Below is part of a much bigger sketch that I've got all working except for the logic below that seems to disregard whether the value of the switch is high or low. The serial monitor only display regardless if the switch is open or closed

int flag = 0;
const int brksw =  11;
int brkswState = 0;

void setup() {
  Serial.begin(9600);
   pinMode(brksw, INPUT);
 
}
void loop() {
   brkswState = digitalRead(brksw);
 if (brkswState == LOW) {
  flag = 1;}
  if (brkswState == HIGH) {
  flag = 9;}
   Serial.println(flag);
}

sorry displays only a 1

How is the switch wired?

  • Switch inputs must not float.

Try:
pinMode(brksw, INPUT_PULLUP);


FYI

it is a bosch relay that uses an external 12v power supply on pins 86 and 86, Pins 30 goes to Uno ground and Pin 87 goes to Uno 11

With INPUT_PULLUP is now only display 9

Imagine I've no idea what any of those numbers mean and that I've never seen this relay.

1 Like

Untitled

Untitled

A clear photo of what you have made might help.

Is this your brake shifter issue with a different title?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.