I have some code which will not upload onto an Arduino Nano. With it plugged into the USB port of the computer there are two red lights, one glowing constantly the other flashing.
Below is the Nano I have, error message (shortened, it tried 10 times to load) and the code I am trying to upload (this code is not my own but is freely available to use).
The Arduino program seems to recognize the Nano and the correct port.
Any suggestions would be appreciated. This is the first time I have tried working with Arduino so my knowledge is limited.
BOARD
Nano 3.0 controller nano CH340 USB driver ATMEGA328 ATMEGA328P nano Mini USB with the bootloader for Arduino
ERROR MESSAGE
Arduino: 1.8.13 (Windows 10), Board: "Arduino Nano, ATmega328P"
Sketch uses 4194 bytes (13%) of program storage space. Maximum is 30720 bytes.
Global variables use 97 bytes (4%) of dynamic memory, leaving 1951 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x26
avrdude: stk500_recv(): programmer is not responding
CODING
//Includes
#include <Servo.h>
//Defines
#define Ch1Input 2 //for the nano, only can use 2 and 3
#define Ch2Input 3 //unless using the pinchangeinterrupt library
#define LBrakeOut 6 //outputs for servos can be any digital pins
#define RBrakeOut 7
#define LBrakeReverse -1 //these need to be either 1 or -1
#define RBrakeReverse 1 //if connected properly, should not need to change
//Variables
int Chan1Interrupt = Ch1Input;
int Chan2Interrupt = Ch2Input;
int pitchOffset = 500;
int rollOffset = 500;
int LBrakeVal, RBrakeVal;
unsigned long Chan1_start, Chan2_start;
volatile double Chan1_val, Chan2_val;
volatile double Chan1_last, Chan2_last;
volatile double Mix_val;
long LocalMillis;
long LocalFrameCounter;
long StartMillis;
long FrameCounter;
Servo LBrake;
Servo RBrake;
void Chan1_begin() {
Chan1_start = micros();
detachInterrupt(digitalPinToInterrupt(Chan1Interrupt));
attachInterrupt(digitalPinToInterrupt(Chan1Interrupt), Chan1_end, FALLING);
}
void Chan1_end() {
Chan1_val = micros() - Chan1_start;
detachInterrupt(digitalPinToInterrupt(Chan1Interrupt));
attachInterrupt(digitalPinToInterrupt(Chan1Interrupt), Chan1_begin, RISING);
if (Chan1_val < 1000 || Chan1_val > 2000) {
Chan1_val = Chan1_last;
}
else {
Chan1_last = Chan1_val;
}
}
void Chan2_begin() {
Chan2_start = micros();
detachInterrupt(digitalPinToInterrupt(Chan2Interrupt));
attachInterrupt(digitalPinToInterrupt(Chan2Interrupt), Chan2_end, FALLING);
}
void Chan2_end() {
Chan2_val = micros() - Chan2_start;
detachInterrupt(digitalPinToInterrupt(Chan2Interrupt));
attachInterrupt(digitalPinToInterrupt(Chan2Interrupt), Chan2_begin, RISING);
if (Chan2_val < 1000 || Chan2_val > 2000) {
Chan2_val = Chan2_last;
}
else {
Chan2_last = Chan2_val;
}
}
void setup() {
pinMode(Ch1Input, INPUT);
pinMode(Ch2Input, INPUT);
LBrake.attach(LBrakeOut);
RBrake.attach(RBrakeOut);
LBrakeVal = 1500 + (500 * LBrakeReverse);
RBrakeVal = 1500 + (500 * RBrakeReverse);
LBrake.writeMicroseconds(LBrakeVal);
RBrake.writeMicroseconds(RBrakeVal);
attachInterrupt(digitalPinToInterrupt(Chan1Interrupt), Chan1_begin, RISING);
attachInterrupt(digitalPinToInterrupt(Chan2Interrupt), Chan2_begin, RISING);
}
void loop() {
LocalMillis = millis();
LocalFrameCounter = (LocalMillis - StartMillis) / 20;
if (LocalFrameCounter > FrameCounter) {
FrameCounter = LocalFrameCounter;
if (Chan2_val >= 1500) {
pitchOffset = 0;
}
else if (Chan2_val < 1500) {
pitchOffset = abs(1500 - Chan2_val) * 2;
}
if (Chan1_val == 1500) {
rollOffset = 0;
}
else {
rollOffset = abs(1500 - Chan1_val) * 2;
}
if (Chan1_val > 1500) {
LBrakeVal = (1500 + (500 * LBrakeReverse)) - ((pitchOffset + rollOffset) * LBrakeReverse);
if (LBrakeVal > 2000) {
LBrakeVal = 2000;
}
else if (LBrakeVal < 1000) {
LBrakeVal = 1000;
}
RBrakeVal = 1500 + (500 * RBrakeReverse) - (pitchOffset * RBrakeReverse);
if (RBrakeVal > 2000) {
RBrakeVal = 2000;
}
else if (RBrakeVal < 1000) {
RBrakeVal = 1000;
}
}
if (Chan1_val < 1500) {
RBrakeVal = (1500 + (500 * RBrakeReverse)) - ((pitchOffset + rollOffset) * RBrakeReverse);
if (RBrakeVal > 2000) {
RBrakeVal = 2000;
}
else if (RBrakeVal < 1000) {
RBrakeVal = 1000;
}
LBrakeVal = 1500 + (500 * LBrakeReverse) - (pitchOffset * LBrakeReverse);
if (LBrakeVal > 2000) {
LBrakeVal = 2000;
}
else if (LBrakeVal < 1000) {
LBrakeVal = 1000;
}
}
LBrake.writeMicroseconds(LBrakeVal);
RBrake.writeMicroseconds(RBrakeVal);
}
}