Hi - I am a wrinkly newbie, used to only writing very basic sketches for the last few years. Now I am trying to write a sketch which activates external devices when input is received through 4 reed switches running 5v current. However, my test sketch is not producing the result required as the output pins light up the test LED (in place of the external device) with the input pins disconnected.
So, I looked this up and used "input_pullup" in the sketch but this still gave a lit up LED. In fact, if anything, the problem looked deeper as with the code written in original form (as below) when I connected the input pin to ground, the test LED went out but with "input" replaced with "input_pullup", connecting the input to ground did not put out the LED.
I've also tried the older "digitalWrite inputPin" instead of "input_pullup" but the result was the same. As I'm not using any external devices - just a LED - and a bog standard Arduino clone, I guess I've done something silly in the code! I'd be really chuffed if someone wiser than me could tell me what I've done wrong...my code is below:
int Outpin3 = 13;
int Outpin2 = 12;
int Outpin1 = 11;
int Outpin0 = 10;
int inputPin2 = 2;
int inputPin3 = 3;
int inputPin4 = 4;
int inputPin5 = 5;
int val = 0;
void setup() {
pinMode(Outpin3, OUTPUT);
pinMode(Outpin2, OUTPUT);
pinMode(Outpin1, OUTPUT);
pinMode(Outpin0, OUTPUT);
pinMode(inputPin2, INPUT);
pinMode(inputPin3, INPUT);
pinMode(inputPin4, INPUT);
pinMode(inputPin5, INPUT);
}
void loop(){
if (val = digitalRead(inputPin2) == HIGH) {
digitalWrite (Outpin3, HIGH);
}
else if (val = digitalRead(inputPin3) == HIGH) {
digitalWrite (Outpin2, HIGH);
}
else if (val = digitalRead(inputPin4) == HIGH) {
digitalWrite (Outpin1, HIGH);
}
else if (val = digitalRead(inputPin5) == HIGH) {
digitalWrite (Outpin0, HIGH);
}
else {
digitalWrite (Outpin3, LOW);
digitalWrite (Outpin2, LOW);
digitalWrite (Outpin1, LOW);
digitalWrite (Outpin0, LOW);
}}
Thanks!
Andrew