I'm making a controller for a drone with an arduino nano and it crashes when I press one specific button. The controller consists of 2 joysticks (each of which contains 2 10k potentiometers), a 16x2 LCD screen, an NRF24L01+PA+LNA and 2 push buttons. Everything works individually but when i put everything into the same script, one of the buttons causes the arduino to freeze. The buttons are used to control the brightness of the LCD screen. The faulty one is the button that increases the brightness and its connected to pin 10.
This is the code:
#include <RF24.h>
#include <LiquidCrystal.h>
#include <Potentiometer.h>
// Transmitter set up
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
static int channels[4];
// LCD set up
LiquidCrystal lcd(0, 1, 2, 3, 4, 5); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
int upButton = 9;
int downButton = 10;
int brightness = 80;
int brightnessPin = 6;
int updateRemainingTime;
// Joystick set up
const int leftXPin = A3;
const int leftYPin = A2;
const int rightXPin = A1;
const int rightYPin = A0;
Potentiometer lxPot = Potentiometer(leftXPin);
Potentiometer lyPot = Potentiometer(leftYPin);
Potentiometer rxPot = Potentiometer(rightXPin);
Potentiometer ryPot = Potentiometer(rightYPin);
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
lcd.begin(16,2);
pinMode(brightnessPin, OUTPUT);
analogWrite(brightnessPin, brightness);
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
lxPot.setParameters(502, 10);
rxPot.setParameters(515, 10);
ryPot.setParameters(527, 10);
}
void updateScreen(int battery, int delay){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Battery:");
lcd.setCursor(0,1);
lcd.print((String) battery + "%");
lcd.setCursor(10,0);
lcd.print("Delay:");
lcd.setCursor(10,1);
lcd.print((String) delay + " ms");
}
void loop() {
int loopStartTime = millis();
if(updateRemainingTime < 0){
//need to ask drone for battery percentage here
updateScreen(channels[1]/10, 0);
updateRemainingTime = 200;
loopStartTime = millis();
}
channels[0] = lxPot.getValue();
channels[1] = lyPot.getValue();
channels[2] = rxPot.getValue();
channels[3] = ryPot.getValue();
if(digitalRead(upButton) == LOW){ brightness--; }
if(digitalRead(downButton) == LOW){ brightness++; }
if(brightness < 0){brightness = 0; }
if(brightness > 255){brightness = 255; }
analogWrite(brightnessPin, brightness);
radio.write(&channels, sizeof(channels));
delay(20);
updateRemainingTime -= millis() - loopStartTime;
}
sorry for the distinct lack of comments.
I am exceptionally confused because the button works in all my other tests like this one that just controls the LCD screen:
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(0, 1, 2, 3, 4, 5); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
int upButton = 10;
int downButton = 9;
int brightness = 80;
int brightnessPin = 6;
int looptime = 0;
int updateRemainingTime = 1000;
void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display }
pinMode(brightnessPin, OUTPUT);
analogWrite(brightnessPin, 80);
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
}
void updateScreen(int battery, int delay){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Battery:");
lcd.setCursor(0,1);
lcd.print((String) battery + "%");
lcd.setCursor(10,0);
lcd.print("Delay:");
lcd.setCursor(10,1);
lcd.print((String) delay + " ms");
}
void loop() {
int loopStartTime = millis();
if(updateRemainingTime < 0){
updateScreen(97, 23);
updateRemainingTime = 1000;
}
if(digitalRead(upButton) == LOW){ brightness--; }
if(digitalRead(downButton) == LOW){ brightness++; }
if(brightness < 0){brightness = 0; }
if(brightness > 255){brightness = 255; }
analogWrite(brightnessPin, brightness);
delay(20);
updateRemainingTime -= millis() - loopStartTime;
}
Both the buttons are connected in the same way, with a 10k resistor between them and ground.
After a series of tests it seems that the arduino freezes when digitalRead() is called and the value is 0. Sometimes it returns 0 but sometimes it crashes before that.
Thanks for any help.
