Arduino Nano - Will not Upload

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);

  }
}

Welcome to the forum (I guess)! I guess you need CH340 driver for your arduino and your operating system first. Here is how I would check whether my computer has proper driver. First, disconnect the arduino, then I show the Tools->port, take note what ports are listed. Then I plug in the arduino, wait for a few moments, show the same Tools->port again, compare with the previous list. If a new port appears on the list, I am inclined to say my arduino is on that new port. If no new port shows up, I think I need driver.

Yes thanks this is my first time in the forum. And my first time working with Arduino.

With the Nano not plugged in only Com1 shows. With Nano plugged in Com1 and Com5 show. Com5 is the port I have selected but cannot get an upload.

Good job doing correctly so far! Now you can go to Tools->processors and try the other options in it such as old bootloader to see if that works because we aren't sure what bootloader it has. It's unlikely in 2021 you will get a nano with ATMEGA168, a lower tier processor than 328, but you can confirm by looking at the square processor for identification, 168 or 328.

1 Like

Thanks for your help liuzengqiang that worked perfectly.
Cheers Sandy

You're welcome. Could you point out for the sake of someone else reading this thread in the future which option worked for you?

The square processor (about 7x7mm) is identified as "mega 328P".
The Tools-Processor setting is "ATmega 328P (old Bootloader)".

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.