For a school project we use 8 microswitches ( Omron D3V-164-2A5 Microschakelaar 250 V/AC 16 A 1x aan/(uit) 1 stuk(s) Bag kopen ? Conrad Electronic ). We placed the switches parallel with each other like in the following scheme.
We are rolling a cilinder over a hill and have placed the a microswitches in between with a distance of 11cm in between. The time between the activation of the switches (so when switch 2 gets activated after switch 1 is activated) is at minimum 90ms.
The problem we are having is when reading the 8 digital pins out, there is a lot of wordless data coming out. We found that the jittering of the microswitches is at max 1ms. A lot of the times we get values like 5ms and then 184ms for example. We have tried adding leds (diodes) on both ends of the switches, so 16 leds, but then the code did become crazy and kepty printing 12us for the timing between switches. We also have used a capacitor 0.1uF for every switch, unfortunatly that didn't work either.
We have rewritten the code multiple times and that also didn't work. So we are at a point that we don't know how to solve the problem anymore.
We use the following code:
int pinSwitch[8] = {3,4,5,6,7,8,9};
unsigned long timestamp[7]={0,0,0,0,0,0,0};
int pinnumber=0;
unsigned long timeTaken[6]={0,0,0,0,0,0};
void setup() {
// put your setup code here, to run once:
for (int i=0;i<7;i++){
pinMode(pinSwitch[i], INPUT);
}
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(pinSwitch[pinnumber])==0){
timestamp[pinnumber]=micros();
pinnumber++;
}
if (micros()-timestamp[0]>=5000000 && timestamp[0]!=0 || pinnumber>6){
printTime();
resetsystem();
}
}
unsigned long printTime(){
for (int i=0;i< (pinnumber-1);i++){
Serial.print("Switch ");
Serial.print(i+1);
Serial.print(" : ");
Serial.println(timestamp[i]);
}
for (int i=0;i<pinnumber-2;i++){
timeTaken[i]=timestamp[i+1]-timestamp[i];
Serial.print("Time from switch ");
Serial.print(i+1);
Serial.print(" to switch ");
Serial.print(i+2);
Serial.print(" :");
Serial.println(timeTaken[i]);
}
}
void resetsystem(){
for (int i=0; i < 7; i++){
timestamp[i]=0;
}
for (int i=0;i < 6;i++){
timeTaken[i]=0;
}
pinnumber=0;
Serial.println("Ready for next measurement");
delay(1000);
}
We hope that any of you see you the problem or has some suggestions, because we don't know what to do anymore at this point in time.