Serial Monitor Not working as Expected.

Hi all,

I just started with the Arduino controller, some weeks ago, and planning to do my Garduino . . .

Am still at the learning step of the Arduino language, Communication and Response.

I met with a prob about the Serial Monitor.

In fact am monitoring Pin 2 which is set as INPUT.

I used the following code:

/*
* Switch Test Program
*/

int switchPin = 2; // Assign Switch button to pin 2. 

int var = digitalRead (switchPin); // Read Pin and save into var

void setup()      // Runs sketch once at start.
{
  Serial.begin(9600);// Set communication with UNO at 9600 bps.
  
  pinMode(switchPin, INPUT); // Sets digital pin as input to read switch
}

void loop()           // Runs sketch over and over
{
  Serial.print("Read Switch Input");  //print line on serial monitor

  Serial.println(var);  //print out value stored in var 
  delay(100);           //Time delay of 100 ms
}

At first it was working perfectly, using pull up or pull down resistor via a mechanical switch. i.e. the serial monitor reads the pin status when it is high or low, it gives the result momentarily.

After i tried this sketch :

/*
* Switch Test Program
*/

int switchPin = 2; // Assign Switch button to pin 2. 
int Led1 = 12; // Assign Led to pin 12
int var = digitalRead (switchPin); // Read Pin and save into var
int var1 = digitalRead (Led1); // Read pin and save into var1

void setup()      // Runs sketch once at start.
{
  Serial.begin(9600);// Set communication with UNO at 9600 bps.
  
  pinMode(switchPin, INPUT); // Sets digital pin as input to read switch
  pinMode(Led1, OUTPUT); // Sets digital pin as output to light led
}

void loop()           // Runs sketch over and over
{
  Serial.print("Read Switch Input");  //print line on serial monitor
  Serial.println(var);  //print out value stored in var 
delay(100);           //Time delay of 100 ms
  Serial.print("Read Switch Output"); // print line on serial monitor
  Serial.println(var1); //printout value stored in var1
delay(1000);           //Time delay of 100 ms
}

Im unable to have my status display corectly on my serial monitor, since then, even when trying my first simple sketch, the serial monitor do display the actual status of the pin, but when changing state, the value would not change momentarily. I need to close the serial monitor first and re-open, then status change.

This is not how it should work, because when i press my switch, i should get a change from high to low or vice versa, ...

I do not really know what happened, i tried several troubleshooting to come to this point, i thought my switch was not good, 1 by 1 i tested all my components, but still NO SOULUTION . . :frowning:

Any help please . . .

Thanks

Taz . .

Moderator edit: Now with added tagstm

I dont see how it ever worked, your var is a digital read that only happens once on init, and is never updated

you need to do something like

int var;

void loop()
{
var = digitalRead();
}

Now it works as it should be . .

OOps it should have been in the void loop,

It worked before as my digital read was in the loop, but did not pay attention when changing the codes . . :wink: :grin:

Anyway . . thanks a lot . .


taz ..