Hello,
I am new to forums but I would appreciate any advice you have for my issue. I am using an Arduino Nano to read the analog signals from 5 flex sensors. I have a push button involved as well. (Each flex sensor and the push button is wired with a 10k ohm resistor).
In my program, I have a sequence that detects if the initial state of the button changes so the rest of the void loop is skipped until the button is toggled for the first time.
Once the button is initially toggled, the program will run through "mode 2" until it is toggled again where it will run through "mode 1" - Which you will continue to be able to toggle between until power is removed.
However, as soon as the button is toggled a second time (going from "mode 2" to "mode 1"), the program will radio.write() and print each value once but then crash.
I tried removing the "radio.write()" line and it will let you toggle between modes and continuously print the flex sensor values for as long as you want - so it must have something to do with the radio.
Another interesting thing I noticed is that when the button is toggled a second time (going from mode 2 to mode 1), and held down, the values will continue to print until it is released when the program crashes.
I also have a version of the code that doesn't involve the button logic and it will send values and be able to be received from another arduino just fine without crashing.
Here is the transmitter code with the button:
#include <RF24.h>
#include <RF24_config.h>
#include <nRF24L01.h>
#include <printf.h>
// Initialize Radio Data
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
// const int ARRAY_SIZE = 5;
int transmit[5];
// Initialize Flex Sensor Data
const int flexPin1 = A1;
const int flexPin2 = A2;
const int flexPin3 = A3;
const int flexPin4 = A4;
const int flexPin5 = A5;
int value1, value2, value3, value4, value5;
int flex1, flex2, flex3, flex4, flex5;
// Initialize Button Data
const int buttonPin = 10; // Pin connected to the push button
const int ledPin = 13; // Pin connected to the LED
int buttonState;
int lastButtonState = LOW;
int modeSelect = 1; // Current mode: 0 for mode 1, 1 for mode 2
bool debounceActive = false; // Flag to track if button debouncing is active
bool initialButtonChange = false; // Flag to track the initial button state change
unsigned long lastModeChange = 0; // Timestamp of the last mode change
unsigned long debounceDelay = 100; // Debounce delay in milliseconds
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.println("Start Test");
}
void loop() {
int buttonState = digitalRead(buttonPin); // Read the current state of the button
if (!initialButtonChange) {
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
if (buttonState == HIGH) {
initialButtonChange = true; // Button state changed for the first time, set initialButtonChange to true
}
}
return; // Skip the rest of the loop until initial button change occurs
}
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
if (buttonState == HIGH && debounceActive == false) {
changeMode(); // Button pressed, change the mode
lastModeChange = millis(); // Record the timestamp of mode change
debounceActive = true; // Activate button debouncing
}
}
if ((millis() - lastModeChange) > debounceDelay) {
debounceActive = false; // Disable button debouncing after debounceDelay
}
if (modeSelect == 0) {
mode1(); // Execute mode 1 functionality
} else {
mode2(); // Execute mode 2 functionality
}
}
void changeMode() {
if (modeSelect == 0) {
modeSelect = 1; // Toggle mode from 0 to 1
} else {
modeSelect = 0; // Toggle mode from 1 to 0
}
}
void mode1() { // Glove Control Mode
digitalWrite(ledPin, HIGH); // Turn on the LED
//Serial.println("LED is on");
// Transmitter Code
value1 = analogRead(flexPin1);
value2 = analogRead(flexPin2);
value3 = analogRead(flexPin3);
value4 = analogRead(flexPin4);
value5 = analogRead(flexPin5);
flex1 = map(value1, 759, 810, 0, 51); // PINKY **
flex1 = constrain(flex1, 0, 180);
flex2 = map(value2, 759, 811, 0, 52); // RING **
flex2 = constrain(flex2, 0, 180);
flex3 = map(value3, 777, 829, 0, 52); // MIDDLE **
flex3 = constrain(flex3, 0, 180);
flex4 = map(value4, 770, 880, 0, 110); // POINTER **
flex4 = constrain(flex4, 0, 180);
flex5 = map(value5, 799, 890, 0, 91); // THUMB **
flex5 = constrain(flex5, 0, 180);
transmit[0] = flex1;
transmit[1] = flex2;
transmit[2] = flex3;
transmit[3] = flex4;
transmit[4] = flex5;
radio.write(&transmit, sizeof(transmit)); // This line of code is casuing the crash!?
Serial.println("PINKY: " + String(transmit[0]));
Serial.println("RING: " + String(transmit[1]));
Serial.println("MIDDLE: " + String(transmit[2]));
Serial.println("INDEX: " + String(transmit[3]));
Serial.println("THUMB: " + String(transmit[4]));
delay(100);
}
void mode2() { // Preset Gesture Mode
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("LED is off");
delay(100);
//*** Set transmit[] variable to preset motions and send them -- receiver code will run normally
}
All the wiring is the same as when I tested it without the button logic and it worked fine so I don't believe that is the issue.
If there is anything else you need to know about my issue please let me know and I will attach it to this.
And if anyone can help me I would greatly appreciate it!