Strange behaviour Arduino UNO compile-verify

I'm having some strange behaviour when I verify-upload below sketch. In setup(), I call displayInstructions(); // Display user instructions and it writes some garbage on the screen which I do not understand. So, to debug it, I verify-upload an empty sketch that does nothing. Then, I upload same sketch with //displayInstructions(); // Display user instructions line commented out. However, the text in displayInstructions(); // Display user instructions keeps showing up when this function has been commented out. Any hints why it's uploading a previous version of the sketch and not the last I uploaded?? Thank you. AA

//###################################################################################
//     ESC CALIBRATION PROGRAM FOR ESC TYPE HW30A
//###################################################################################
//     WARNING: PROGRAM PROVIDED AS IS, NO WARRANTY
//###################################################################################
// The Arduino must be connected to ESC beforehand:
// - pin 4 on ESC on white control wire. NOTE FIG. PAGE 81 USES pin 3!!!!!
// - GND on black ESC control wire 
// - WARNING: red ESC wire NOT used, never connected
// 
// ESC is connected to brushless motor, BUT NOT YET TO BATTERY. Steps to follow are:
// 1. Upload program to Arduino. Program starts send PWM pulses of 2ms (max)
// 2. Open Arduino serial monitor, set baud rate to 9600. Text appears with
//    instructions on how to continue.
// 3. Connect ESC to battery: 3 rising "beeps" followed by 2 identical "beeps".
//    The 2 "beeps" confirm that the max speed control point were memorized.
// 4. Within 2secs after the 2 beeps, type in the serial monitor letter 
//    "v"+"enter". This sends a min speed command (PWM pulses 1ms) to ESC. 
//    ESC will emit 3 short "beeps" followed by a long "beep", confirming that
//    min speed command is acknowledged. Calibration is now complete.
// 5. Press "t" to perform with increasing/decreasing speed control. Motor will
//    run up to max speed and then down to min speed.
//
//    NOTE: IF STEP "4" IS NOT DONE QUICKLY ENOUGH, ESC ENTERS CONFIGURATION MODE.
//    In this case, it is necessary to:
//               - disconnect ESC from battery 
//               - close serial monitor
//               - reset Arduino or plug/unplug USB from PC
//               - reopen serial monitor
//               - reconnect ESC to battery
//               - resume calibration from step "4"
//    After calibration, disconnect: ESC from battery, Arduino from PC, Arduino/ESC 
//    link. Close serial monitor and Arduino IDE.
//###################################################################################
// Include "Servo.h" library to generate PWM signals, this is the same type of signal 
// used to control servomotors.
#include <Servo.h>
Servo motor; // Instance of type "Servo"
char data; // Variable to read a character typed in the keyboard at serial monitor
//############################################################
//#####                 SETUP() FUNCTION                 #####
//############################################################
void setup(){
	Serial.begin(9600); // Initialiaze serial communication with PC
	motor.attach(3, 1000, 2000); // ESC controlled at pin 3, PWM pulses of 1ms to 2ms
	motor.write(180); // Throttle max corresponds to 2ms. Writes a value to the servo, 
	   // controlling the shaft accordingly. On a STANDARD servo, this will set angle 
		 // of the shaft (in deg), moving the shaft to that orientation. In a CONTINUOUS 
		 // rotation servo (like a motor), this will set the speed of the servo (with 0 
		 // being full-speed in one direction, 180 being full speed in the other, and a 
		 // value near 90 being no movement). REGULAR/STANDARD servo motors only turn
		 // over a narrow range, with precise control over position. CONTINUOUS rotation 
		 // servos spin continuously, with control over its speed and direction.
	//displayInstructions(); // Display user instructions 
}// End setup()
//############################################################
//#####                 LOOP() FUNCTION                  #####
//############################################################
void loop(){
	if( Serial.available() ){
		data = Serial.read(); // Read character from serial monitor
		// Based on user answer, execute the following switch:
		switch(data){
			case 118: // 118 is ASCII for letter "v"
				Serial.println("Send min gas command");
				motor.write(0); //Min speed command. Should this be 90?? see setup()
				break;
			case 109: // 109 is ASCII for letter "m"
			  Serial.println("Send max gas command");
				motor.write(180);// Max speed command 
				break;
			case 116: // 116 is ASCII for letter "t"
			    Serial.print("Motor test start in 3secs...\n");
			    delay(3000);
				ESC_test();
				break;
		}
	}
}
//###################################################################################
// ESC test function has no input nor output parameters. 
// ESC setpoint (the desired value in a closed-loop feedback system, as in regulation 
// of temperature or pressure) varies from min to max and from max to min.
//###################################################################################
void ESC_test(){
	for (int i = 0 ; i <=  180 ; i++){//Should it be 90-180??????
		Serial.print("Speed = ");
		Serial.println(i);
		motor.write(i);
		delay(10);
	}
	for (int i = 180 ; i >= 0 ; i--){//Should it be 180-90??????
		Serial.print("Speed = ");
		Serial.println(i);
		motor.write(i);
		delay(10);
	}
	Serial.println("STOP");
	motor.write(0); //Should it be 90??????
}
//###################################################################################
// Function to display instructions to user. Has no input/output parameters.
// ##################################################################################
void displayInstructions(){
	Serial.println("READY FOR CALIBRATION\n");
	Serial.println("Read the following instructions completely before you begin:\n");
	Serial.println("Connect the ESC to the battery");
	Serial.println("ESC emits a melody of 3 rising beeps, then 2 beeps of same tone");
	Serial.println("Within 2 secs, type \"v\" followed by \"enter\" in serial monitor");
	Serial.println("ESC then emits 3 identical short beeps followed by long beep");
	Serial.println("This signals that calibration is complete");
	Serial.println("To start a test in variable speed, enter letter \"t\" + \"enter\": ");
}

Is the Serial Monitor also set to 9600 baud? Probably better to make them both 115200 since 9600 is the 1980s standard and we've gotten a bit better since then :slight_smile:

You can put a small delay after your Serial.begin() to make sure the connection is up before you starting printing things.

Serial.begin(115200); // Initialiaze serial communication with PC
delay(10000;

FYI... rather than a comment about ASCII codes

  	case 118: // 118 is ASCII for letter "v"

just use the character directly

  	case 'v':

Strange behaviour is now gone, thank you!!. Unfortunately motor still not starting. It does work when I plug the ESC into a servomotor control instead of the Arduino UNO (which is new). Baffled...

Just got the motor to work as expected. If anybody is interested the line motor.write(180); needed to be changed to motor.write(0); . Thanks again @blh64 for previous help.

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