Hey all, so I'm having a small issue with some of the code for my temperature sensor (which lives on after a full re-wiring, and is looking better too)
Now, I had this issue before, which made me change from a momentary button to a toggle, and then re-wire it. For testing I am using the script as basic as it comes, basically if the toggle is high, set that to the led output pin, if low, the same.
int val = 0; // variable to store the read value
void setup()
{
pinMode(A1, OUTPUT); // sets the digital pin 13 as output
pinMode(2, INPUT); // sets the digital pin 7 as input
}
void loop()
{
val = digitalRead(2); // read the input pin
digitalWrite(A1, val); // sets the LED to the button's value
}
Now, the issue is, it doesn't work... on battery, mostly.
When plugged into my laptop or a grounded power supply it works, sort of, but if my hand gets near the right wire, or near the switch body (or sometimes touching the metal on the switch itself) it will randomly turn the light on or off, or make it flicker/dim.
This only happens when it's on laptop or psu power, but when I have it on battery power, it doesn't work at all. Nada. No switch, no wire touching, no magic invisible barrier around certain parts to trigger it.
Except, as I wrote this and tried it again I did get a little bit of activity out of it, but it doesn't seem to react to the toggle at all. I can't figure out for the life of me what's causing this.
Circuit is basically like so
GND ---/\/\/\--/\/\/\---| |--- arduino pin 2
GND----/\/\/\--/\/\/\---LED---arduino pin A1
The /\/\/\'s are resistors, the switch being -| |-. The switch has two 5.1k resistors since I didn't have a 10k handy (and they are around 10.1 combined), the LED has whatever values it needs, can't recall, but it works fine when turned on/off without depending on the switch.
I could really use some insight on this, perhaps I could use .1uf ceramic caps to ground to get rid of the noise that is causing this, although I don't think that would explain why it pretty much ignores the switch entirely.
I mean sure, I have made a super low cost proximity sensor, but that's the opposite of what I need.
edit: sitting down with other wires being the problem eliminated (I figured it was the lcd wires), I realize it only happens with the switch open, and I need to flip the variable to turn the LED on, be back after some quick tweaking. Hopefully it should turn the LED on when I have the switch on, and then when it's off it will act up, then I just need to know how to stop that noise from getting to the arduino.