I have a pin that I'm using as an input pull up connected to a pushbutton and I have it printing the pin state back to the computer for troubleshooting. When the button is not pushed down the pin reads as a 1, like it should. However when pushed down, instead of reading zero because it's connected to ground, it doesn't read anything. Nothing prints out until the button is up again.
At first I thought it was a problem with the button but when I bypass the button and just directly connect the pin to ground I have the same issue. Could it just be a faulty pin? None of my other input pull up pins have been having this issue except for 13.
er 13 has always been a trouble maker cause "they" insist on putting an LED there, on the uno I think its buffered by an op amp but I dont have one so I cant say what effects that has on it
It still doesn't print anything back when connected. My main concern isn't really the fact is not printing back a number but because it's not it's not reading the pin as LOW ever effecting a big portion of my code.
if (!EstopVal && prevEstopVal) {
...
}
if (EstopVal && !prevEstopVal) {
...
}
Also, what is the purpose of "prevEstopVal". It seems to me that it is redundant as it always is the logical inverse of "EstopVal", and never used on its own.
You read in the button.
If its low, then EstopVal = low, preEstopVal = high.
else EstopVal = high, preEstopVal = low.
Next time the loop occurs, the same happens again. (meaning the two are Always the not of each other)
By any chance was this what you were trying to do:
void loop(){
int EstopVal = digitalRead(EstopBut);
int DispatchVal = digitalRead(DispatchBut);
int NowGate = digitalRead(GateSwitch);
int NowRestraint = digitalRead(RestraintSwitch);
unsigned long currentMillis = millis();
//***************EMERGENCY STOP MODE****************//
if (EstopVal == LOW && prevEstopVal == HIGH) {
......
}
if (EstopVal == HIGH && prevEstopVal == LOW) {
......
}
preEstopVal = EstopVal; //Note this stores the old value for next time around
}
Yes that is exactly what I am trying to do. When I write the code that way though it gets all funky and doesn't seem to work correctly.
Here is what my code is when written that way:
const int DispatchBut = 8;
const int EstopBut = A0;
const int EstopLT = 12;
const int DispatchLT = 3;
const int GateSwitch = 4;
const int RestraintSwitch = 5;
int DispatchLit = LOW;
int EstopLit = LOW;
int LastGate;
int LastRestraint;
int prevEstopVal;
long previousMillis = 0;
long DisInterval = 1000;
long EstopInterval =500;
void setup(){
//start serial connection
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(DispatchBut, INPUT_PULLUP);
pinMode(EstopBut, INPUT_PULLUP);
pinMode(DispatchLT, OUTPUT);
pinMode(EstopLT, OUTPUT);
pinMode(GateSwitch, INPUT_PULLUP);
pinMode(RestraintSwitch, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("#S|LIMITS|[]#");
}
void loop(){
int EstopVal = digitalRead(EstopBut);
int DispatchVal = digitalRead(DispatchBut);
int NowGate = digitalRead(GateSwitch);
int NowRestraint = digitalRead(RestraintSwitch);
unsigned long currentMillis = millis();
//***************EMERGENCY STOP MODE****************//
if (EstopVal == LOW && prevEstopVal == HIGH) {
if(currentMillis - previousMillis > EstopInterval) {
previousMillis = currentMillis;
if (EstopLit == LOW)
EstopLit = HIGH;
else
EstopLit = LOW;
digitalWrite(EstopLT, EstopLit);
}
}
if (EstopVal == HIGH && prevEstopVal == LOW) {
//**************GATE CONTROL*****************//
if (LastGate != NowGate) {
delay (50);
if (LastGate != NowGate) {
Serial.println("#S|SENDK|[0&{F10}]#");
LastGate = NowGate;
}
}
//***********RESTRAINT CONTROL**************//
if (LastRestraint != NowRestraint) {
delay (50);
if (LastRestraint != NowRestraint) {
Serial.println("#S|SENDK|[0&{F9}]#");
LastRestraint = NowRestraint;
}
}
//blinking dispatch button code
if(currentMillis - previousMillis > DisInterval) {
previousMillis = currentMillis;
if (DispatchLit == LOW)
DispatchLit = HIGH;
else
DispatchLit = LOW;
digitalWrite(DispatchLT, DispatchLit);
}
if (DispatchVal == LOW) {
digitalWrite(DispatchLT, HIGH);
Serial.println("#S|SENDK|[0&~]#");//Sends Enter Key
delay(3000);
}
}
prevEstopVal = EstopVal;
}
For some reason the lights aren't blinking at all, they are just staying on or off depending on the state of the EstopBut pin. It seems to be switching back and forth between the two but it isn't carrying everything out like it should. Thanks for all of your help!
You read in the button.
If its low, then EstopVal = low, preEstopVal = high.
else EstopVal = high, preEstopVal = low.
Next time the loop occurs, the same happens again. (meaning the two are Always the not of each other)
By any chance was this what you were trying to do:
void loop(){
int EstopVal = digitalRead(EstopBut);
int DispatchVal = digitalRead(DispatchBut);
int NowGate = digitalRead(GateSwitch);
int NowRestraint = digitalRead(RestraintSwitch);
unsigned long currentMillis = millis();
if (EstopVal == HIGH && prevEstopVal == LOW) {
......
}
preEstopVal = EstopVal; //Note this stores the old value for next time around
}
This doesn't seem to be working because it ends up just making both EstopVal and prevEstopVal the same value as soon as it starts so they are never the opposite value except for a split second so neither if statement is ever true for an extended period of time like it needs to be. As for the momentary button press, I'll need that later for something else but as far as this button goes its either open or closed.
There is another issue, which is that you are using 'previousMillis' for two different things, both the blinking dispatch, and estop light. I would suggest you use two variables, one for each (e.g. eStopPreviousMillis, and dispatchPreviousMillis).
What you will find though is that if you are using a tactile type switch, there will be lots of noise, so it will essentially call both until the button has settled.
You should put a capacitor between the input pin and ground (approx 10nF to 100nF) to act as debouncing.
...................................
Just to clarify something you wrote while I was writing this, if the switch is either open or closed, then why not just look at if(EstopVal == LOW){