digitalRead not getting real-time feed.

Why is this example not getting the real-time information it should display.

/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor 
 
 This example code is in the public domain.
 */

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
}

Why is this example not getting the real-time information it should display.

What is it not doing right?

Aside from the missing internal pullup resistor, which suggests a possible floating pin, the code looks fine.

Pull-up resistor? What value of resistor should i use? And where am i going to place it.

It keeps on displaying "1" even if the button is normally open.

Pull-up resistor? What value of resistor should i use? And where am i going to place it.

Yes. Whatever size you want. Back in the package.

The Arduino has internal pullup resistors that are easier to use. After the pinMode() call, add

digitalWrite(pushButton, HIGH);

to turn the pullup resistor on.

Now its working. Thank you.
But what does this pull up resistor do? Can you discuss it? Its okay if it is not very detailed. I need it so i could apply it to the whole project i'm working on.

Worth a read: Pull-up resistor - Wikipedia

-br

http://www.cmiyc.com/tutorials/arduino-pull-ups/

Explanation with videos.

Thanks a lot!

So how many pins can i read at a time? Is there any way on how i can read 4 pins at time? Thanks!

Is there any way on how i can read 4 pins at time?

Yes, but the method is not portable. Reading a pin takes hardly any time. Why do you think you need to read 4 of them at once? The time needed to read 4 pins one at a time is dwarfed by the time needed to send the info to the serial port.

Sorry, my question should have been:
Is there a way on how can i read 4 pins using only one function call? Because the arduino can only read the first digitalRead and cannot read the rest.

horikitamaki:
Because the arduino can only read the first digitalRead and cannot read the rest.

This does not make sense. I suspect you may be trying to poll multiple inputs concurrently, or something. Perhaps it would be best if you explained what you are trying to achieve, rather than why you think your existing solution doesn't work.

I am trying to light up LEDs using these pushbuttons.
The problem is, one the first function is being read. Even if didnt use function calling and put all of the if else condition under the loop function, it still read the first one.
Here is the code:

  int buttonState;
  int pinone = 4;
  int pintwo = 5;
  int pinthree = 6;
  int pinfour = 7;
  
  int swone = 8;
  int swtwo = 9;
  int swthree = 10;
  int swfour = 11;

  boolean one = false;
  boolean two = false;
  boolean three = false;
  boolean four = false;



void setup()
{
  pinMode(pinone, OUTPUT);
  pinMode(pintwo, OUTPUT);
  pinMode(pinthree, OUTPUT);
  pinMode(pinfour, OUTPUT);
  pinMode(swone, INPUT);
  digitalWrite(swone, HIGH);
  pinMode(swtwo, INPUT);
  digitalWrite(swtwo, HIGH);
  pinMode(swthree, INPUT);
  digitalWrite(swthree, HIGH);
  pinMode(swfour, INPUT);
  digitalWrite(swfour, HIGH);
}


void loop()
{
  button_one();
  button_two();
  button_three();
  button_four();

}  
  
void button_one()
{
  if (!one)
  {
    buttonState = digitalRead(swone);
    if (buttonState == 1)
    {
      digitalWrite(pinone,HIGH);
      delay(2000);
      one = true;
    } 
  
    else
    {
    }
  }
  
  else if(one)
  {
    buttonState = digitalRead(swone);
    if (buttonState == 1)
    {
      digitalWrite(pinone,LOW);
      delay(2000);
      one = false;
    }
    
    else
    {
    }
  }
}

void button_two()
{
  if (!two)
  {
    buttonState = digitalRead(swtwo);
    if (buttonState == 1)
    {
      digitalWrite(pintwo,HIGH);
      delay(2000);
      two = true;
    } 
  
    else
    {
    }
  }
  
  else if (two)
  {
    buttonState = digitalRead(swtwo);
    if (buttonState == 1)
    {
      digitalWrite(pintwo,LOW);
      delay(2000);
      two = false;
    }
    
    else
    {
    }
  }
}

void button_three()
{
  if (!three)
  {
    buttonState = digitalRead(swthree);
    if (buttonState == 1)
    {
      digitalWrite(pinthree,HIGH);
      delay(2000);
      three = true;
    } 
  
    else
    {
    }
  }
  
  else if (three)
  {
    buttonState = digitalRead(swthree);
    if (buttonState == 1)
    {
      digitalWrite(pinthree,LOW);
      delay(2000);
      three = false;
    }
    
    else
    {
    }
  }
}

void button_four()
{
  if (!four)
  {
    buttonState = digitalRead(swfour);
    if (buttonState == 1)
    {
      digitalWrite(pinfour,HIGH);
      delay(2000);
      four = true;
    } 
  
    else
    {
    }
  }
  
  else if (four)
  {
    buttonState = digitalRead(swfour);
    if (buttonState == 1)
    {
      digitalWrite(pinfour,LOW);
      delay(2000);
      four = false;
    }
    
    else
    {
    }
  }
}

If !one is true, then one will not be. There is no reason to test that in the else clause(s).

You can have multiple things appear to happen simultaneously until you code d, e, l, a, y, (, some value, ), and ; in a row. Then, nothing happens simultaneously, like reading switches and flashing LEDs.

So, don't do it.

Maybe this could help.
But i dont know if the pull-up resistors will work here.

void button_one()
{
  if (!one)
  {
    buttonState = digitalRead(swone);
    if (buttonState == 1)
    {
      digitalWrite(pinone,HIGH);
      delay(2000);
      one = true;
    } 
  
    else
    {
    }
  }
  
  else if(one)
  {
    buttonState = digitalRead(swone);
    if (buttonState == 1)
    {
      digitalWrite(pinone,LOW);
      delay(2000);
      one = false;
    }
    
    else
    {
    }
  }
}

This is pretty wierd code. I haven't tested it, but it looks as if it would toggle the output and then delay for two seconds every time it notices the input is high. Since you have pull-up resistors enabled, I expect the inputs will normally be high which might cause each output to toggle in turn at two second intervals. I can't imagine why that would be useful and in any case this looks like a very complicated way to achieve it.

What are you actually aiming for this sketch to do?

I am trying to light up my LED by using the pushbuttons.

And yes you are right, they are blinking alternately. What should i do/add/remove to my sketch?

This is my new code:

  int pinone = 4;
  int pintwo = 5;
  int pinthree = 6;
  int pinfour = 7;

  boolean one = false;
  boolean two = false;
  boolean three = false;
  boolean four = false;



void setup()
{
  pinMode(pinone, OUTPUT);
  pinMode(pintwo, OUTPUT);
  pinMode(pinthree, OUTPUT);
  pinMode(pinfour, OUTPUT);
  DDRB = B00000000;
  
  
}


void loop()
{
  
  unsigned char pins = PINB & 0x7;  // read pins 8/9/10
  switch (pins)
  {
  case 0:
     switch (one)
     {
     case false:
       digitalWrite(pinone,HIGH);
       delay(2000);
       one = true;
       break; 
     case true:
       digitalWrite(pinone,LOW);
       delay(2000);
       one = false;
       break;
     default: break;
     }

  case 1:
    switch (two)
    {
    case false:
      digitalWrite(pintwo,HIGH);
      delay(2000);
      two = true;
      break; 
    case true:
      digitalWrite(pintwo,LOW);
      delay(2000);
      two = false;
      break;
    default: break;
    }

  case 2:
    switch (three)
    {
    case false:
      digitalWrite(pinthree,HIGH);
      delay(2000);
      three = true;
      break; 
    case true:
      digitalWrite(pinthree,LOW);
      delay(2000);
      three = false;
      break;
   default: break;
   }

   case 3:
    switch (four)
    {
    case false:
      digitalWrite(pinfour,HIGH);
      delay(2000);
      four = true;
      break; 
    case true:
      digitalWrite(pinfour,LOW);
      delay(2000);
      four = false;
      break;
   default: break;
    }
   case 4:
   case 5:
   case 6:
   case 7:
     break;
   }
}

horikitamaki:
I am trying to light up my LED by using the pushbuttons.

And yes you are right, they are blinking alternately. What should i do/add/remove to my sketch?

I really don't like the way you're using a switch statement instead of a simple if/else, and the code looks far more complicated than necessary for such a simple problem, and you have not processed the value read from the port to decide which buttons are pressed correctly.

To be honest I think you are working on completely the wrong lines and would be better off starting again.

You have a set of switch inputs and a corresponding set of LED outputs.

Put the input pin numbers in an array.
Put the LED pin numbers in the array.
Declare an array to hold the previous state of each input.

Process the switch array in a FOR loop. For each input, read the switch state. If the state is LOW and the previous state was HIGH then the switch has just been pressed; toggle the corresponding LED. The main logic would be about half a dozen lines of code.