I am a total noob with programming. I am planning on controlling some LED's depending on the position of 7 switches. Long story short all I can get out is a slightly jumpy waveform so input HIGH is slightly floating.
It is not important if it takes 1-2 seconds for the arduino to detect the position of the switches. My first idea is to use some kind of median to see the position but apparently it has t be sorted which seems to be a lot for the Arduino. Some kind of average where higher than 0.2 is classified as high seems ok. I am just totally unable to get any result in the arduino. Is there an easy way to do this request?
Noisy signals may mean your wiring is loose, or you're missing a resistor and the pin is simply floating, so the state is undetermined.
Connect switch between pin and GND, then set pin to INPUT_PULLUP to enable the internal pull-up resistor. This guarantees your pin to be HIGH when the switch is open, and the switch pulls it LOW when pushed.
Besides you'll have to look into debouncing. That can be done in hardware (connect a small capacitor across your switch, 100 nF is a good value), or do it in software.
The supply is shit and I do not have hardware smoothing devices. The average is high so it should give me a positive answer over time. It doesn't matter if it is a few seconds delayed.
if your power supply is shit you're going to have more problems than an unstable input signal. The processor also needs decent, clean power.
Without circuit diagram not much else I can say about this. It looks like you really have to place some capacitors on that pin to stablilise things (and if you don't have any, time to get them now - just get some of those many values kits).
This is the electric layout: https://i.imgur.com/eUaEPBy.jpg.
On the left you have the system made by the guy who built the model train. On the right are the signals that I plan to implement, but those I do not have a problem with.
The voltages shown are from the multimeter.
The Arduino is powered by the USB so it has good and clean power.
The power supply I can not find any info on it but it seems to be from 1970ish.
I am in the middle of nowhere in Sweden (my parents) and I am only here seldomly and I suck at electrical stuff. If I can solve this post with programming that would be nice. Hardware wise I only have 12k, 4.2k, 330 and 270 ohm resistors available.
Somehow this thread got moved from programming and I do not know why.
I did check out debounce but that is only when changing switch position, here it is bouncing even when on. I have searched filters but I can not find a good one. It would be nice if I could be pushed in the correct direction on what to google, am not familiar with the expressions to google either.
Sorry for the weird request
Anyway, the simple solution for this is with a capacitor.
Or if you really want a software solution: take a bunch of samples, take the average, based on that decide whether it's on or off, but that's rather slow, as you have to sample for at least 1/100th of a second (half a wave).
I was more thinking of adding a capacitor to the digital pin - iirc OP mentioned measuring 3.3V there, which would mean a peak of 5.2V - which is within spec but only just; the resistor network however should be enough to keep the current in check and have the clamping diodes do their job safely if it goes even higher.
If you add a capacitor at the 17V line you'd need a much bigger one, due to the high current of the train motors.
wvmarle:
I was more thinking of adding a capacitor to the digital pin - iirc OP mentioned measuring 3.3V there, which would mean a peak of 5.2V - which is within spec but only just; the resistor network however should be enough to keep the current in check and have the clamping diodes do their job safely if it goes even higher.
If you add a capacitor at the 17V line you'd need a much bigger one, due to the high current of the train motors.
You could feed the indicator circuit via a diode and put a cap on the indicator side.
We shall see what to Op comes back with.
Tom...
PS. I'm off to bed...
I just ended up with a very simple average calculator. Though to avoid float I just summed all together and used a threshold which works very well. It took me way too long to do this I must admit but programming became much easier after this so learned a lot.
I added the part of the code below for the interested, it might not be the best but it is working just fine.
boolean turnoutA = 0;
int average = 0;
const int samples = 1200;
const int threshold = 140;
void checkswitchpositions() {
average = 0; //TurnoutA
for (int i = 0; i < samples; i++) {
average = average + digitalRead(5);
}
if (average < threshold) {
turnoutA = 1;
}
else {
turnoutA = 0;
}
}