NOOB with serial print problem

Hi all,

writing my first piece of code and already got stuck on something probably really stupid. just try to print status of the digital inputs. I got it working fine with this code:

int OptoPin=5;
int OptoRead = digitalRead(OptoPin);
int dt=1000;
int StartPin=6;
int StartButton = digitalRead(StartPin);


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
//start button
pinMode (StartPin, INPUT_PULLUP);
//digitalWrite (StartPin,HIGH);

 //Opto Endstop
pinMode (OptoPin, INPUT_PULLUP);
//digitalWrite(OptoPin,HIGH);


  //Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin

  

  //Setup Channel B
pinMode(13, OUTPUT); //Initiates LED Channel B pin & spray valve

}
void loop() {
  // put your main code here, to run repeatedly:
 //start with button
  Serial.print("StartPin");
  Serial.println(digitalRead(StartPin));
  Serial.print("OptoPin");
  Serial.println(digitalRead(OptoPin));
  

delay(dt);

}

no problem here

but when I tried to change: "Serial.println(digitalRead(StartPin));" to "Serial.println(StartButton);" I always get 0 states.

new code here:

int OptoPin=5;
int OptoRead=digitalRead(OptoPin);
int dt=1000;
int StartPin=6;
int StartButton=digitalRead(StartPin);


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
//start button
pinMode (StartPin, INPUT_PULLUP);
//digitalWrite (StartPin,HIGH);

 //Opto Endstop
pinMode (OptoPin, INPUT_PULLUP);
//digitalWrite(OptoPin,HIGH);


  //Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin

  

  //Setup Channel B
pinMode(13, OUTPUT); //Initiates LED Channel B pin & spray valve

}
void loop() {
  // put your main code here, to run repeatedly:
 //start with button
  Serial.print("StartPin");
  Serial.println(StartButton);
  Serial.print("OptoPin");
  Serial.println(OptoRead);
   

delay(dt);

}

must be something really silly, but I'm not seeing it.

thanks for your help

What do you have connected to pin 6 ?

Your code only does digitalRead() once. Put that in loop(), not in the initialization.

vaj4088: What do you have connected to pin 6 ?

Pin 6 is a pushbutton

I suspect that you have the pushbutton turned around the wrong way so that pin 6 is always grounded. Do you have a voltmeter or an oscilloscope or a logic analyzer?

vaj4088: I suspect that you have the pushbutton turned around the wrong way so that pin 6 is always grounded. Do you have a voltmeter or an oscilloscope or a logic analyzer?

I checked with a multimeter and The puscbutton was indeed the wrong way round.