digitalRead() Problems

Okay. Just a simple question, say if i set pinMode(1, OUTPUT). And digitalRead(1), will it work?

Because if i do it like this in my code(Which is the somewhat the same as the example i given)

int led = 22;
int led1 = 24;
int led2 = 26;
int led3 = 28;

boolean DEBUG = true;

void setup() {
pinMode(led, OUTPUT); //pin selected to control
pinMode(led1, OUTPUT); //pin selected to control
pinMode(led2, OUTPUT); //pin selected to control
pinMode(led3, OUTPUT); //pin selected to control
pinMode(30, OUTPUT);
pinMode(32, OUTPUT);
pinMode(34, OUTPUT);
pinMode(36, OUTPUT);
pinMode(38, OUTPUT);
pinMode(40, OUTPUT);
pinMode(42, OUTPUT);
pinMode(44, OUTPUT);
pinMode(46, OUTPUT);
pinMode(48, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);

pinMode(A2, INPUT); //analog
  pinMode(A3, INPUT); //analog
  pinMode(A4, INPUT); //analog
  pinMode(A5, INPUT); //analo
  pinMode(A6, INPUT); //analog
  pinMode(A7, INPUT); //analog
  pinMode(A8, INPUT); //analog
  pinMode(A9, INPUT); //analog

  pinMode (2, INPUT); // For switch
  pinMode (3, INPUT); // For switch
  pinMode (5, INPUT); // For switch
  pinMode (6, INPUT); // For switch

  Serial.begin(9600);
}

void loop() {
  Station1LED();
  Station2LED();
  Station3LED();
  Station4LED();
  if (DEBUG)
 {
  Serial.println("");
  Serial.print("PIN 22 : ");
  Serial.println(digitalRead(22));
  Serial.print("PIN 24 : ");
  Serial.println(digitalRead(24));
  Serial.print("PIN 26 : ");
  Serial.println(digitalRead(26));
  Serial.print("PIN 28 : ");
  Serial.println(digitalRead(28));
  Serial.println("");
  Serial.print("PIN 30 : ");
  Serial.println(digitalRead(30));
  Serial.print("PIN 32 : ");
  Serial.println(digitalRead(32));
  Serial.print("PIN 34 : ");
  Serial.println(digitalRead(34));
  Serial.print("PIN 36 : ");
  Serial.println(digitalRead(36));
  Serial.println("");
  Serial.print("PIN 38 : ");
  Serial.println(digitalRead(38));
  Serial.print("PIN 40 : ");
  Serial.println(digitalRead(40));
  Serial.print("PIN 42 : ");
  Serial.println(digitalRead(42));
  Serial.print("PIN 44 : ");
  Serial.println(digitalRead(44));
  Serial.println("");
  Serial.print("PIN 46 : ");
  Serial.println(digitalRead(46));
  Serial.print("PIN 48 : ");
  Serial.println(digitalRead(48));
  Serial.print("PIN 11 : ");
  Serial.println(digitalRead(11));
  Serial.print("PIN 12 : ");
  Serial.println(digitalRead(12));
  Serial.println("");
  delay(1000);
 }
}
void Station1LED()
{
if (digitalRead(2) == 0)
  {
    if (analogRead(A2) > 1 && analogRead(A3) > 900)
    {
      digitalWrite ( 22 , HIGH);
      digitalWrite ( 24 , LOW);
      digitalWrite ( 26 , LOW);
      digitalWrite ( 28 , LOW);  
    } 
    else if(analogRead(A2) >= 100)
    {
      digitalWrite ( 22 , LOW);
      digitalWrite ( 24 , LOW);
      digitalWrite ( 26 , HIGH);
      digitalWrite ( 28 , LOW);
    }
    else 
    {
      digitalWrite ( 22 , LOW);
      digitalWrite ( 24 , HIGH);
      digitalWrite ( 26 , LOW);
      digitalWrite ( 28 , LOW);    
    }
  
  }
    else 
    {
      digitalWrite ( 22 , LOW);
      digitalWrite ( 24 , LOW);
      digitalWrite ( 26 , LOW);
      digitalWrite ( 28 , HIGH);
    }

    
}
void Station2LED()
{
if (digitalRead(3) == 0)
  {
    if (analogRead(A4) > 1 && analogRead(A5) > 900   )
    {
      digitalWrite ( 30 , HIGH);
      digitalWrite ( 32 , LOW);
      digitalWrite ( 34 , LOW);
      digitalWrite ( 36 , LOW);  
    }
    else if(analogRead(A4) >= 100)
    {
      digitalWrite ( 30 , LOW);
      digitalWrite ( 32 , LOW);
      digitalWrite ( 34 , HIGH);
      digitalWrite ( 36 , LOW);
    }
    else
    {
      digitalWrite ( 30 , LOW);
      digitalWrite ( 32 , HIGH);
      digitalWrite ( 34 , LOW);
      digitalWrite ( 36 , LOW);    
    }
  }
 
    else 
    {
      digitalWrite ( 30 , LOW);
      digitalWrite ( 32 , LOW);
      digitalWrite ( 34 , LOW);
      digitalWrite ( 36 , HIGH);
    }
 
    
}
void Station3LED()
{
if (digitalRead(5) == 0)
  {
    if (analogRead(A6) > 1 && analogRead(A7) > 900   )
    {
      digitalWrite ( 38 , HIGH);
      digitalWrite ( 40 , LOW);
      digitalWrite ( 42 , LOW);
      digitalWrite ( 44 , LOW);  
    }
    else  if(analogRead(A6) >= 100)
    {
      digitalWrite ( 38 , LOW);
      digitalWrite ( 40 , LOW);
      digitalWrite ( 42 , HIGH);
      digitalWrite ( 44 , LOW);
    }
    else 
    {
      digitalWrite ( 38 , LOW);
      digitalWrite ( 40 , HIGH);
      digitalWrite ( 42 , LOW);
      digitalWrite ( 44 , LOW);    
    }
  }
    

    else 
    {
      digitalWrite ( 38 , LOW);
      digitalWrite ( 40 , LOW);
      digitalWrite ( 42 , LOW);
      digitalWrite ( 44 , HIGH);
    }
  
}
void Station4LED()
{
if (digitalRead(6) == 0)
  {
    if (analogRead(A8) > 1 && analogRead(A9) > 900   )
    {
      digitalWrite ( 46 , HIGH);
      digitalWrite ( 48 , LOW);
      digitalWrite ( 11 , LOW);
      digitalWrite ( 12 , LOW);  
    }
    else if(analogRead(A8) >= 100)
    {
      digitalWrite ( 46 , LOW);
      digitalWrite ( 48 , LOW);
      digitalWrite ( 11 , HIGH);
      digitalWrite ( 12 , LOW);
    }
    else 
    {
      digitalWrite ( 46 , LOW);
      digitalWrite ( 48 , HIGH);
      digitalWrite ( 11 , LOW);
      digitalWrite ( 12 , LOW);    
    }
  }
    

    else 
    {
      digitalWrite ( 46 , LOW);
      digitalWrite ( 48 , LOW);
      digitalWrite ( 11 , LOW);
      digitalWrite ( 12 , HIGH);
    }  
}

it doesnt actually work properly, some digitalRead() doesnt come out properly. For example LED connected to pin 1 is ON but the digitalRead(1) says it should be off.

Pins 0 and 1 are used by Serial. Don't try to use them for other stuff.

...R

Digital pin 1 is the TX pin. So if you're printing something on the serial port, there is a chance that it detects LOW.

Just my 2 cents.

Right... Thanks for the info, but i was using pin 1 as an example... Perhaps i should have mentioned the following info first. Sorry!

What i really mean is if i do it on like digital pins 22 to 48(Using the even numbered pins, doing this on a Arduino MEGA 2560) and 11,12 as seen in my code. With the setup of it as output and digital reading it, is there the chance of it reading as LOW? Because i have it all of the pins connected to LEDs, and even when the LEDs are on, the Serial monitor says its low.

To make it easier to understand what you are asking can you please create a simple program ?

You can digitalRead() the state of a pin set to output, if that helps.

That kinda helps. Thanks.

Hmm... But the thing is if what you say is true then sometimes it just doesnt work on the code im using then. Gotta go fix that then...

Well if your interested, the simplest form of code i could provide is in my first post in which it happens to me.

To explain my code, as its kinda long, but is very repetitive.

Setup contains all the pins Im using, loop contains 4 functions and 1 if statment meant for debugging, and lastly the functions are all the same (unless typo) its just to control the LEDs, it contains all the digitalWrite.

digitalRead should reflect the state of the pins. But I don't have a Mega to test.

Instead of 200 lines of code that you gave us, you can use something based on the below sketch to test

void setup()
{
  Serial.begin(9600);

  // setup pins a outputs defaulting to low
  for(int pinNo=22;pinNo<49;pinNo+=2)
  {
    pinMode(pinNo, OUTPUT);
    digitalWrite(pinNo, LOW);
  }

  // buffer for easy printing to serial port
  char txBuffer[64];

  // blink a led, read back the state and send to serial port
  for(int pinNo=22;pinNo<49;pinNo+=2)
  {
    digitalWrite(pinNo, HIGH);
    sprintf(txBuffer, "pin %2d set HIGH, read back %d", pinNo, digitalRead(pinNo));
    Serial.println(txBuffer);

    delay(500);

    digitalWrite(pinNo, LOW);
    sprintf(txBuffer, "pin %2d set LOW, read back %d", pinNo, digitalRead(pinNo));
    Serial.println(txBuffer);

    delay(500);
  }

}


void loop()
{
}

The delays are implemented to you can see if multiple LEDs switch on or off.

Note:
With 24 LEDs connected (I hope you're using resistors as well to limit the current), the question is how you power your Mega?

PS

Well if your interested, the simplest form of code i could provide is in my first post in which it happens to me

Are you sure about that :smiley:

sterretje:
Instead of 200 lines of code that you gave us, you can use something based on the below sketch to test

I agree wholeheartedly with the idea of simplification. However I would go further and just do the testing on a single I/O pin.

...R

A little for loop does not take much more time to write. I suspect that the OP would not have asked the question if there is no problem, so this will test a bit more.

@sterretje Thanks for the code! It shows there is no problem with the pins Im using.

So I only can conclude that its something to with the hardware Im using... Not enough power I guess, because im using the USB port to power my mega with Ethernet shield stack. And my computer was screaming at me saying not enough power.(I dont have a 5v adaptor, maybe its time i got one...)

About the LEDs, i like to live dangerously :smiling_imp: currently not using any resistors, yes i know its kinda bad to do that.

PS Okay good sir you win :smiley: . I only know the basics, didnt know you could do that (o.o) .

Prinny:
About the LEDs, i like to live dangerously :smiling_imp: currently not using any resistors, yes i know its kinda bad to do that.

That's not 'living dangerously'; that is just suicidal. Question is what will die first? One or more of the leds or the pin on the micro or the complete micro.

my computer was screaming at me saying not enough power

That could be a bit of a clue, don't you think ?

Oh... Okay... Ill go get me some resistors now...