hey,
topic : "analogread value jumping around"
function : pwm signal to motordriver on buttonpush etc.
connections: endswitches gnd to A2/A3 norm. closes, pwm & dir out on D8/D9
3 buttons : like in the picture, R220 GND to A1, 5V via different resistors for every button, connected to A1
(https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button )
i have made a lot of theese, they are almost all working, but with some the analogread value tends to jump around big time and i am out of ideas what the problem might be.
do you have any suggestions what it could be?
Thanks
Hi, @unartifficialunintelligence
Welcome to the forum.
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Thanks.. Tom....
Measurements always have random errors. Please post some examples, so forum members can get an idea of what you mean by "jump around".
Hi, @unartifficialunintelligence
Thanks for the schematic.
Can you please post your code and some images of your project?
So we can see your component layout.
Do you have a DMM? Digital MultiMeter?
Thanks.. Tom....
i have a +- tolerance of 80
value can also jump to 0 while button pushed
not much to see on photos,
#include "PWM.h"
#include "Arduino.h"
#include "EEPROMextent.h"
#define buttonwires 2
//buttonresistors : A450/B20/C45
int i;
bool EEPwritten =false;
char incomingByte = 0;
bool m_interrupt=false;
unsigned long actionbegin=0;
#define buttonsvaluelimit 15
#define oldbuttonsvaluelimit 80
int buttonstatcountback=3;
#define oldbuttonAvalue 320
#define oldbuttonBvalue 510
#define oldbuttonCvalue 845
//#define buttonAvalue 45
//#define buttonBvalue 75
//#define buttonCvalue 105
float spdmod=10;
long speed_up= 30000;
long speed_down=40000;
int buttonpressedat=0;
int buttonswitchdelay=500;
#define commandenddelay 800
enum direction {UP, DOWN};
enum spinUp {ACCELERATE, DECELERATE};
#define accelsteps 200
#define deccelsteps 800
#define steppin 9
#define dirpin 8
#define startfreq 0
int buttonsvalue;
#define buttons A1
#define endswitchtop A2
int endswitchbottom=17;
bool dirHelp = false;
int32_t maxfreq = 300000;
int32_t freq = 0;
int buttonAstate;
int buttonBstate;
int buttonCstate;
int endswitchtopState;
int endswitchbottomState;
int interrupt = 0;
long actualpos = 0;
long movebeginfrompos =0;
long favpostop = 15000;
long favposlow = 0;
int startup = 1;
int pos = 1;
float stepstaken=0;
void countposition_acc_dcc() {
if (dirHelp == true) {
stepstaken=(((micros()-actionbegin)*0.00000001)*speed_up );
actualpos=(movebeginfrompos+stepstaken)/2;}
if (dirHelp == false) {
stepstaken=(((micros()-actionbegin)*0.00000001)*speed_down);
actualpos=(movebeginfrompos-stepstaken)/2; }}
void countposition() {
if (dirHelp == true) {
stepstaken=(((micros()-actionbegin)*0.00000001)*speed_up );
actualpos=movebeginfrompos+stepstaken;}
if (dirHelp == false) {
stepstaken=(((micros()-actionbegin)*0.00000001)*speed_down);
actualpos=movebeginfrompos-stepstaken; }}
void Inputread() {
m_interrupt=false;
buttonAstate=HIGH;buttonBstate=HIGH;buttonCstate=HIGH;
if (buttonwires==4){
if (digitalRead(A5)==HIGH && digitalRead(A5)==HIGH) buttonAstate = HIGH; else buttonAstate=LOW;
if (digitalRead(A4)==HIGH && digitalRead(A4)==HIGH) buttonBstate = HIGH; else buttonBstate=LOW;
if (digitalRead(A3)==HIGH && digitalRead(A3)==HIGH) buttonCstate = HIGH; else buttonCstate=LOW;}
if (buttonwires==2){
buttonsvalue = analogRead(buttons);
if ((digitalRead(endswitchtop)==HIGH) && (digitalRead(endswitchtop)==HIGH)) endswitchtopState = HIGH;
else endswitchtopState = LOW;
if ((digitalRead(endswitchbottom)==HIGH) && (digitalRead(endswitchbottom)==HIGH)) endswitchbottomState = HIGH;
else endswitchbottomState = LOW;
if (buttonstatcountback==0){buttonAstate=HIGH;buttonBstate=HIGH;buttonCstate=HIGH;}
buttonstatcountback--;if (buttonstatcountback<0) buttonstatcountback=0;
//a
if ((buttonsvalue > (oldbuttonAvalue - oldbuttonsvaluelimit) && buttonsvalue < (oldbuttonAvalue + oldbuttonsvaluelimit))
|| ((buttonsvalue > (45 - buttonsvaluelimit) && buttonsvalue < (45 + buttonsvaluelimit))))
{buttonAstate = LOW;buttonstatcountback=3;}
//b
if ((buttonsvalue > (510 - oldbuttonsvaluelimit) && buttonsvalue < (510 + oldbuttonsvaluelimit))
|| ((buttonsvalue > (75 - buttonsvaluelimit) && buttonsvalue < (75 + buttonsvaluelimit))))
{buttonBstate = LOW;buttonstatcountback=3;}
//c
if ((buttonsvalue > (845 - oldbuttonsvaluelimit) && buttonsvalue < (845 + oldbuttonsvaluelimit))
|| ((buttonsvalue > (105 - buttonsvaluelimit) && buttonsvalue < (105 + buttonsvaluelimit))))
{buttonCstate = LOW;}}
incomingByte =0;
if (Serial.available() > 0) {
incomingByte = Serial.read();}
if (incomingByte=='a') buttonAstate=LOW;
if (incomingByte=='b') buttonBstate=LOW;
if (incomingByte=='c') buttonCstate=LOW;
if (incomingByte=='0') {buttonAstate=LOW;buttonBstate=LOW;buttonCstate=LOW;}
if (buttonAstate == LOW || buttonBstate == LOW || buttonCstate == LOW) m_interrupt=true;}
void HoldSpeed(){
SetPinFrequency(steppin, freq); pwmWriteHR(steppin, 32786);
}
void turnOn(spinUp speedup){
buttonpressedat=actualpos;
if (dirHelp == false) maxfreq = speed_down;
else maxfreq = speed_up;
switch (speedup) {
case ACCELERATE:
{ for (freq = startfreq; freq <= maxfreq; freq += accelsteps) {HoldSpeed(); }
break;
Serial.print("freq : ");Serial.println(freq);}
case DECELERATE:
{for (freq = maxfreq; freq >= startfreq; freq -= deccelsteps) {HoldSpeed();}
pinMode(steppin, LOW);}}}
void moveToDirection(direction dir){
digitalWrite(dirpin, dir);
if (dir == UP) dirHelp = true;
else dirHelp = false;}
void favpos_save() {
favpostop = actualpos;
EEPwritten=false;
Serial.print("favpos_save ");
printvariables();
noise();
pinMode(steppin, LOW);
}
void noise() {
SetPinFrequency(9, 615000); // make the noise
pwmWriteHR(steppin, 32786);
delay(50);
pinMode(steppin, LOW);
delay (1000);}
void ButtonA() {
Serial.println("ButtonA");printvariables();
actionbegin=micros();
movebeginfrompos=actualpos;
if (endswitchtopState == LOW) {pos = 1;
moveToDirection(UP);
turnOn(ACCELERATE);
countposition();
actionbegin=micros();
actualpos=(actualpos-movebeginfrompos)/2+actualpos;
movebeginfrompos=actualpos;
//autoupbuttondelay
while ((actualpos-buttonpressedat < buttonswitchdelay) && (endswitchtopState == LOW) &&
(buttonAstate == LOW))
{ Serial.print("autoupbuttondelay ");
printvariables();
HoldSpeed();
Inputread();
countposition();
}
//manualup
while (buttonAstate == LOW && buttonBstate == HIGH
&& buttonCstate == HIGH && endswitchtopState == LOW && Serial.read()!=0) {
if (pos == 1) pos = 0;
Serial.print("manualup ");
printvariables();
HoldSpeed();
Inputread();
countposition();
m_interrupt=true;}
//autoup
while (endswitchtopState == LOW && actualpos < favpostop && m_interrupt==false) {
Serial.print("autoup ");
if (pos == 1) {favposlow = buttonpressedat;pos = 0; }
printvariables();
HoldSpeed();
Inputread();
countposition();}
turnOn(DECELERATE);
delay(commandenddelay);}
EEPwritten=false;
Inputread();}
void ButtonB() {
Serial.println("ButtonB");
actionbegin=micros();
movebeginfrompos=actualpos;
if (endswitchbottomState == LOW) {
moveToDirection(DOWN);
turnOn(ACCELERATE);
countposition();
actionbegin=micros();
actualpos=(movebeginfrompos-actualpos)/2+actualpos;
movebeginfrompos=actualpos;
//autodownbuttondelay
while ((buttonpressedat-actualpos < buttonswitchdelay) && (endswitchtopState == LOW) &&
(buttonBstate == LOW))
{ Serial.print("autodownbuttondelay ");
printvariables();
HoldSpeed();
Inputread();
countposition();
}
//manualdown
while (buttonAstate == HIGH && buttonBstate == LOW
&& buttonCstate == HIGH && endswitchbottomState == LOW && Serial.read()!=0) {
Serial.print("manualdown ");
printvariables();
HoldSpeed();
Inputread();
countposition();
m_interrupt=true;
}
//autodown
while (endswitchbottomState == LOW && actualpos > favposlow
&& Serial.read()!=0 && m_interrupt==false) {
Serial.print("autodown ");
printvariables();
HoldSpeed();
Inputread();
countposition();
}
turnOn(DECELERATE);
delay(commandenddelay);}
EEPwritten=false;
Inputread();}
void selfcheck() {
Serial.println("selfcheck");
noise();noise();
delay(2000);
Inputread();
while (buttonCstate==HIGH){
Inputread();
if (endswitchtopState == LOW) {
moveToDirection(UP);
turnOn(ACCELERATE);
while (endswitchtopState==LOW && buttonCstate==HIGH) {
HoldSpeed();
Inputread();
if (buttonAstate == LOW ) {
spdmod=spdmod+0.5;
spdmod=constrain(spdmod,4,12);
Eepromwrite();
Serial.print("spdmod : ");Serial.println(spdmod);
delay(500);}
if (buttonBstate == LOW) {
spdmod=spdmod-0.5;
spdmod=constrain(spdmod,4,12);
Eepromwrite();
Serial.print("spdmod : ");Serial.println(spdmod);
delay(500);}
speed_up=30000*spdmod;speed_down=40000*spdmod;
if (dirHelp==true) freq=speed_up;
if (dirHelp==false) freq=speed_down;
}
turnOn(DECELERATE);
delay(1000);
if (buttonCstate == LOW) spdmod=10;
Eepromwrite();
}
if (endswitchtopState == HIGH) {
moveToDirection(DOWN);
turnOn(ACCELERATE);
while (endswitchbottomState==LOW && buttonCstate==HIGH) {
HoldSpeed();
Inputread();
if (buttonAstate == LOW) {
spdmod=spdmod+0.5;
spdmod=constrain(spdmod,4,12);
Eepromwrite();
Serial.print("spdmod : ");Serial.println(spdmod);
delay(500);}
if (buttonBstate == LOW ) {
spdmod=spdmod-0.5;
spdmod=constrain(spdmod,4,12);
Eepromwrite();
Serial.print("spdmod : ");Serial.println(spdmod);
delay(500);}
speed_up=30000*spdmod;speed_down=40000*spdmod;
if (dirHelp==true) freq=speed_up;
if (dirHelp==false) freq=speed_down;
}
turnOn(DECELERATE);
}
}
Eepromwrite();
startup = 0;}
void Buttonstatecheck(){
if (buttonAstate == LOW) ButtonA();
if (buttonBstate == LOW) ButtonB();
if (buttonCstate == LOW) favpos_save(); }
void printvariables(){
Serial.print("eswt:");Serial.print(endswitchtopState);Serial.print("|");
Serial.print("eswb:");Serial.print(endswitchbottomState);Serial.print("|");
Serial.print("A:");Serial.print(buttonAstate);Serial.print("|");
Serial.print("B:");Serial.print(buttonBstate);Serial.print("|");
Serial.print("C:");Serial.print(buttonCstate);Serial.print("|");
Serial.print("actpos:");Serial.print(actualpos);Serial.print("|");
Serial.print("fptp:");Serial.print(favpostop);Serial.print("|");
Serial.print("fplo:");Serial.print(favposlow);Serial.print("|");
Serial.print("irrup:");Serial.print(m_interrupt);Serial.print("|");
Serial.print("spdmod:");Serial.print(spdmod);Serial.print("|");
Serial.print("bpres@:");Serial.print(buttonpressedat);Serial.print("|");
Serial.print("butval:");Serial.print(buttonsvalue);Serial.print("|");
Serial.print("pos:");Serial.print(pos);Serial.print("|");
Serial.print("micros:");Serial.print(micros());Serial.print("|");
Serial.print("actbegin:");Serial.print(actionbegin);Serial.print("|");
Serial.print("intrpt:");Serial.print(m_interrupt);Serial.print("|");
Serial.print("EEPwrt:");Serial.print(EEPwritten);Serial.print("|");
Serial.println();
}
void startuptest(){
if (startup == 1) {
Inputread();
printvariables();
if (buttonCstate == LOW) {spdmod=10;Eepromwrite();selfcheck();}
startup = 0; }}
void Eepromwrite(){
if (m_interrupt==false && EEPwritten==false)
{EEPwritten=true;
EEPROMextent.clear(0, 100);
EEPROMextent.writeAnything(10, actualpos);
EEPROMextent.writeAnything(20, favpostop);
EEPROMextent.writeAnything(30, spdmod);
Serial.print("WritetoEEPR : ");
printvariables();
}}
void Eepromgetdata(){
EEPROMextent.readAnything(10, actualpos);
EEPROMextent.readAnything(20, favpostop);
EEPROMextent.readAnything(30, spdmod);
if (spdmod==0 || spdmod!=spdmod) spdmod=10;}
void setup() {
pinMode(A1, INPUT);
if (buttonwires==4) {endswitchbottom = 15;pinMode(endswitchbottom, INPUT_PULLUP);}
delay(1000);
Serial.begin(115200);
Serial.println("________________________");
InitTimersSafe(2200);
delay(1000);
pinMode(A3, INPUT_PULLUP);
pinMode(A4, INPUT_PULLUP);
pinMode(A5, INPUT_PULLUP);
pinMode(endswitchbottom, INPUT_PULLUP);
pinMode(endswitchtop, INPUT_PULLUP);
pinMode(dirpin, OUTPUT);
//bool succes = SetPinFrequency(steppin, freq);
Eepromgetdata();
movebeginfrompos=actualpos;
speed_up=speed_up*spdmod;
speed_down=speed_down*spdmod;
startuptest();
noise();
}
void loop() {
//printvariables();
Inputread();
Buttonstatecheck();
Eepromwrite();}
If that means "jumps of 80 units" then you probably have an intermittently or fully open circuit at the analog input.
If fully open, waving a hand near the input will usually produce a response. Double check all connections and wiring for continuity or appropriate resistance.
basic stuff like connections are all tested, and i have this build multiple times, and just some do it, mostly when they are still "cold" just switched on, later the problem might occur not so often.
Still in debugging, trying to find circumstances when it occurs or not, couldn´t determine yet.
All works fine on the desk, but at customer some circuits just do their own thing ....(headscratch:)
Sounds like a quality control problem.
Hi, @unartifficialunintelligence
Thanks for the info.
You might need to dress your wiring a bit better.
Keep mains AC away from other low voltage signal wires.
Check you gnd wiring.
Do you have a DMM?
Measure the AC voltage on the analog input to see if you have any noise?
Do you have access to a oscilloscope?
Tom....
I'm curious about this pattern you've used multiple times:
if (digitalRead(A4)==HIGH && digitalRead(A4)==HIGH) buttonBstate = HIGH; else buttonBstate=LOW;
What is the purpose of reading the pin twice within a handful of microseconds?
a7
mikedb
April 7, 2025, 5:09am
13
Do you have a 100nF cap on A1 to GND?
What is the resistor values?
mikedb:
Debouncing ?
I am unaware of switches that finish bouncing in 7.2 musi rose condos microseconds.
Read the diagram in #3 above. The resistors are unnecessarily low in value, but should work. The maximum current the voltage divider consumes is < 20 mA.
a7
hey, thanks, there is no cap, not sure if i tried that already, is long time since i made this project. Will try that also, buttonresistors are 440 / 220 / 47 , and gnd resistor is 220.
will try to debug today, with logic analyzer.
Was just curious if you guys see any, simple thing i might have overseen/ignored,
Thanks!
no.. it´s 47 ohm, in the end it must be some kind of EMI, since like 50 of them are working as they suppose to do , and only a few make problems.
Trying to recreate the problem.