I have a simple sketch where I'm reading a voltage from a potentiometer and sending out values through the UART TX pin to an XBee. The LED is lit as a totally separate operation indicating when a pin from the XBEE goes high. When I turn the pot quickly, the LED will flicker. The code is simple and I tried commenting things out and changing just about every parameter I could think of but the only thing that clears this up is when I remove the wire connecting the Arduino TX (Uno or Adafruit Metro Mini or Adafruit Trinket M0) and the XBee. I have tried powering the XBEE regulator board separately from the Uno and powering it from the 5V pin and the 3V pin.
Does anyone know what is occurring here? A major clue I feel is that it happens when the pot is turned in the direction that the pot values INCREASE. 0 up to 5V at the output. When I reverse the outside wires to change the direction the pot operates, the flickering follows and happens when turning the pot quickly in the opposite direction.
I'm pulling what's left of my hair out. I am going to try to isolate the Arduino TX from the XBee regulator Digital IN with an optoisolator later tonight but I would really love to understand what is occurring electronically.
Please post your code and a schematic.
I've been slashing at this temporary file so please ignore the extras.
Edit: If I comment out the digital read and pin state code and digitalWrite pin 13, HIGH, the LED will not flicker.
//Constants:
const int potPin = A0;
const int pin_State_Input = 2; //pinstate 9 on XB and IN 9 on Metro
const int pinStateLed = 13; // out to LED pinstate switch indicator
int ledState = HIGH; // the current state of the output pin
int pinState = 0; // the current reading from the input pin
int lastPinState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
const int debounceDelay = 50; // the debounce time;
//Variables:
//Value from pot
int potVal = 0;
int potValPrev = 0;
int pinCounter = 0;
void setup() {
//Start the serial communication.Baud rate must be the same as is on xBee module
Serial.begin(38400);
//Serial1.begin(38400);
pinMode(pin_State_Input, INPUT);
pinMode(pinStateLed, OUTPUT);
}
void loop() {
pinState = digitalRead(pin_State_Input);
// compare the buttonState to its previous state
if (pinState != lastPinState) {
// if the state has changed, increment the counter
if (pinState == LOW) {
// if the current state is HIGH then the button went from off to on:
pinCounter++;
digitalWrite (pinStateLed, LOW);
} else {
// if the current state is LOW then the button went from on to off:
digitalWrite (pinStateLed, HIGH);
}
}
lastPinState = pinState;
{ //Read the analog value from pot and store it to "value" variable
potVal = analogRead(potPin);
//Map the analog value to pwm value
potVal = map (potVal, 0, 1023, 255, 0);
if (abs(potVal - potValPrev) > 2)
if (potVal != potValPrev) {
Serial.print('<');
//Serial1.print('<');
Serial.print(potVal);
//Serial1.print(potVal);
Serial.println('>');
//Serial1.println('>');
potValPrev = potVal;
Serial.flush();
//Serial1.flush();
delay (10);
}
}
}
Please confirm which way your switch is wired ?
S1, S2, or S3
const byte potPin = A0;
const byte pin_State_Input = 2; //pinstate 9 on XB and IN 9 on Metro
const byte pinStateLed = 13; //out to LED pinstate switch indicator
byte ledState = HIGH; //the current state of the output pin
byte pinState = 0; //the current reading from the input pin
byte lastPinState = LOW; //the previous reading from the input pin
int potVal = 0;
int potValPrev = 0;
int pinCounter = 0;
//timing stuff
unsigned long lastDebounceTime = 0; //the last time the output pin was toggled
const int debounceDelay = 50; //the debounce time;
//********************************************^************************************************
void setup()
{
//Start the serial communication.Baud rate must be the same as is on xBee module
Serial.begin(38400);
pinMode(pin_State_Input, INPUT_PULLUP);
pinMode(pinStateLed, OUTPUT);
} //END of setup()
//********************************************^************************************************
void loop()
{
//*************************************************
pinState = digitalRead(pin_State_Input);
//has the pin changed state ?
if (pinState != lastPinState)
{
//update to the new state
lastPinState = pinState;
//did the pin go LOW ?
if (pinState == LOW)
{
pinCounter++;
digitalWrite (pinStateLed, LOW);
}
//the pin went HIGH
else
{
digitalWrite (pinStateLed, HIGH);
}
}
//*************************************************
//Read the analog value from pot and store it to "value" variable
potVal = analogRead(potPin);
potVal = map (potVal, 0, 1023, 255, 0);
//***********************
if (abs(potVal - potValPrev) > 2)
{
if (potVal != potValPrev)
{
Serial.print('<');
Serial.print(potVal);
Serial.println('>');
delay (10);
}
}
} //END of loop()
Thank you for looking at this LarryD.
S3? Input 2 on the Uno is set to listen to a high state from a pin on the XBee board. No pullups or resisters.
The sketch changes offered you has INPUT_PULLUP.
Confirm the Xbee goes LOW, normally sits HIGH, or is it the opposite.
Oh right. I had just tried different techniques of whether the pin was pulled high or started high and pulled low etc. My original sketch doesn't even use a state change. The signal from the XB is low until it receives a change wirelessly from a button state.
When the XB pin is low (normal state), the LED pin on the board should be low.
Z
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.