Smoothing for digitalInput

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.

My input waveforms looks like this:

If grounded it looks like this:

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?

Please post your code too

To be honest, I don't really have a code, but I tried fiddling something from this homepage: http://damienclarke.me/code/posts/writing-a-better-noise-reducing-analogread

What I tried is to change analogRead to digitalRead

int inputPin = 2;


void setup() {
  pinMode(inputPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  float output = 0;
  void loop() {
    output += (digitalRead(inputPin) - output) * 0.3;
    Serial.println(output);
    millis(100);
  }

}

I am not even able to get the loop function to work... :confused:

The page you linked to is about a noise reduction scheme for analogue measurements.

analogRead(inputPin) will return a number from 0 to 1023, which you can do maths on as mentioned in the article, and get meaningful results.

digitalRead(inputPin) can only return a 0 or 1.
If you multiply 0 or 1 by 0.3, you get either 0 or 0.3 (which is what your serial plot shows).

You can't simply take something that was meant for noise reduction on analogue inputs and use it for digital inputs.

A better technique for removing noise (switch bounce) on digital inputs is 'de-bouncing'.

There is a tutorial on it, here,

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.

Well I still have the problem with the unstable supply of DC.

If I read with analogRead I get this:

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).

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

In your other post we asked what you powersupply was.
Can you tell us, or give us a schmatic, or a picture.

It almost sounds like you have a transformer to rectifier to output, so all it i is PULSED DC, not SMOOTH DC.

Thanks Tom... :slight_smile:

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

Looking at your schematic I realised it looks awfully familiar!

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).

wvmarle:
Looking at your schematic I realised it looks awfully familiar!

Yes and in post #14 I asked about the power supply.
The OPs circuit.


The reference to weird signal shape means the power supply is a standard model railway rectified AC unit, no smoothing/filter capacitor.

Careful if a cap is added the voltage will go up.

If 17Vdc is the average voltage (measured by a meter in DC mode)

Using;
http://www.electronics-tutorials.ws/diode/diode_6.html

Peak Voltage will be 17 / 0.637 = 26.6 Vp
So adding a capacitor will bring the DC voltage up to nearly 26.6Vdc

Tom... :slight_smile:

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... :slight_smile:
PS. I'm off to bed... :sleeping: :sleeping: :sleeping:

Thanks everyone!

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;
  }
}