if/else statements and Serial monitor return

Hello, i ran into a problem trying to make a simple switch system of no particular purpose.

int startswitch = 0;
int setswitch = 1;
int startval;
int setval;
void setup() {
  Serial.begin(9600); 
  Serial.println("Super Serial Work Initiated");
  pinMode(startswitch, INPUT);
  pinMode(setswitch, INPUT);

}


void loop() {
  
setval = digitalRead(setswitch);
startval = digitalRead(startswitch);

  if (setval != 0) {
    Serial.println("set");
  }
  else{
    Serial.println("B");
  }

  if (startval != 0) {
    Serial.println("Start");
  }
  else{
    Serial.println("A");
  }

}

Basically, i hooked up 2 switches to my arduino and tried to program it so that the Serial Monitor prints something in indication of button detected.

however, it only detects whatever switch is on pin 0, and returns a corrupted ACII character when switch corresponding to pin 1 is pressed (even when the if/else statements for the switch is disabled)

i checked online to make sure my hardware wasn't flawed (wire from pin to switch to gnd), and everybody i have consulted so far don't see any problems in the code. there are no error messages when uploading, either.

help would be very much appreciated :slight_smile:

everybody i have consulted so far don't see any problems in the code

Well, I do. :slight_smile:

Hardware serial uses pins 0 and 1, so you can hardly also use them for your switches.

There is more:

  • Please use the term 'pin' in the name for a pin, and make those 'const'. For example: const int startswitchPin = 0;
  • The return value of digitalRead() is HIGH or LOW. Please compare them to HIGH and LOW, not 0 or 1.
  • You probably want to detect the moment a key is pressed or released. This example shows how : https://www.arduino.cc/en/Tutorial/StateChangeDetection

HIGH and LOW are defined as 1 and 0

...R

const int startpin = 2;
const int setpin = 3;
int startval=0;
int setval=0;
int laststartval=0;
int lastsetval=0;
int startcounter=0;
int setcounter=0;

void setup() {
  Serial.begin(9600); 
  Serial.println("Super Serial Work Initiated");
  pinMode(startpin, INPUT);
  pinMode(setpin, INPUT);

}


void loop() {
  
setval = digitalRead(setpin);
startval = digitalRead(startpin);

  if (setval != lastsetval) {
    if(setval == HIGH){
      setcounter++;
      Serial.println("set counter now equals");
      Serial.println(setcounter);
    } else{
      Serial.println("off");
    }
    delay(50);
    }
  lastsetval = setval;

//---------------------------------------------

  if (startval != laststartval) {
    if(startval == HIGH){
      startcounter++;
      Serial.println("start counter now equals");
      Serial.println(startcounter);
    } else{
      Serial.println("off");
    }
    delay(50);
    }
  laststartval = startval;

}

i noted your recommendations, and overhauled my script.
it works now!

thanks.

Looking good :slight_smile:
You might add a few comments, for example the title and date at the top.
Do you know the auto-text-layout-format (Ctrl+T) ? It is in the 'Tools' menu. That will rearrange the indents and so, to make it look better. Also when a '}' is missing, the Ctrl+T will show almost immediate where it did go wrong.