Sendig reedswitch states via hc-05

hi,

the problem is, i get 4 lines in serial monitor for android: 5;6;7;13. (no matter where the magnets are)
the bright side is the leds light up correctly.
i am not so good at asking questions, but here is my code:

byte reed[4] = {4,5,6,7};
void setup() {
for (byte i = 1; i <= 4; i++) {
  pinMode(reed[i], INPUT);}
Serial.begin(38400);}
void loop(){
for (byte i = 1; i <= 4; i++) {
  digitalRead(reed[i]);
  Serial.println(reed[i]);
}
delay(2000);}

thanks

Arrays are "zero indexed". You are addressing locations in memory which are not reserved for you variables.

The values if i in your for( ) loops should be

for (byte i = 0; i < 4; i++)

You also want to be printing(and working with) the digitalRead value of each pin and not the pin number.

for (byte i = 0; i <=4; i++) {
Serial.println( digitalRead(reed[i]));
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.