How can tracking a sketch running sequence

Or can some one help to explain how the attached sketch run? Thanks.

#include <VirtualWire.h>

/* ARDUINO RC GAMEPAD by TeamJaeger in Technology from https://www.instructables.com/id/Arduino-RC-Gamepad/ 
 *  saved to: E:\design_info\DIY\ARDUINO\01_ARDUINO RC GAMEPAD 
 *  this sketch was designed to load to a PRO MICRO, will be modify to suit sth new 
 */
 
//motor A connected between A01 and A02
//motor B connected between B01 and B02

int STBY = 10; //standby  ////  An integer (more commonly called an int) is a number without a decimal point.


//Motor A
int PWMA = 3; //Speed control 
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction

//Motor B
int PWMB = 5; //Speed control
int BIN1 = 6; //Direction
int BIN2 = 16; //Direction  ////WAS16

const boolean FORWARD = HIGH; ////The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only". This means that the variable can be used just as any other variable of its type, but its value cannot be changed. You will get a compiler error if you try to assign a value to a const variable.
const boolean REVERSE = LOW;
float speed_Max = 255; //pwm usually goes from 0-255  ////A float is a floating-point number, which means it is a number that has a decimal place
float speed_Min = 0;
float analogInput_Max = 1023;  ////bool类型只有两个值:true =1 、false=0。
float analogInput_Min = 0;
float analogInput_Middle_X = 515;//ideally it would be analogInput_Max / 2
float analogInput_Middle_Y = 495;//ideally it would be analogInput_Max / 2
float deadBand = 0;
float middleMax = (analogInput_Max / 2) + deadBand;
float middleMin = (analogInput_Max / 2) - deadBand;
boolean pastDirection = FORWARD;

void setup(){
	
	Serial.begin( 9600 );
	
	pinMode(STBY, OUTPUT); //// pin: the number of the pin whose mode you wish to set; mode: INPUT, OUTPUT, or INPUT_PULLUP.
	
	pinMode(PWMA, OUTPUT);
	pinMode(AIN1, OUTPUT);
	pinMode(AIN2, OUTPUT);
	
	pinMode(PWMB, OUTPUT);
	pinMode(BIN1, OUTPUT);
	pinMode(BIN2, OUTPUT);
	
	//receiver setup
	vw_set_rx_pin(2);          //Sets pin 12 as the RX Pin  //// should be pin '2'
	vw_set_ptt_inverted(true); // Required for DR3100
	vw_setup(2000);            // Bits per sec
	vw_rx_start();             // Start the receiver PLL running
}

void loop(){

	uint8_t buf[VW_MAX_MESSAGE_LEN]; // This declares a variable array. instead of 7 variables buf1, buf2 etc...
	uint8_t buflen = VW_MAX_MESSAGE_LEN;
	if (vw_get_message(buf, &buflen)) // Non-blocking
	{
		int i;
	    int column = 0;
	    String message;
	    int commands[30];
	    
        // Message with a good checksum received, dump it. 
        for (i = 0; i < buflen; i++)
        {     
        	//DEBUG:
        	Serial.print(char(buf[i]));
        	
        	if(char(buf[i]) == '|'){
        		commands[column] = message.toInt();
        		message = "";
        		column++;
        	}else{
        		message += char(buf[i]);
        	}
        }
        //one more time to capture the last value since the message does not end with |
        commands[column] = message.toInt();
        
        // DEBUG 
//		Serial.print("X: "); 
//		Serial.print(commands[0]);
//		Serial.print(" Y: ");
//		Serial.println(commands[1]);
		
		motorControl(commands[0], commands[1]);
		
	}
	
}

void move(int motor, int speed, boolean direction){
	digitalWrite(STBY, HIGH); //disable standby //// Write a HIGH or a LOW value to a digital pin.
	
	if(motor == 1){
		digitalWrite(AIN1, direction);
		digitalWrite(AIN2, !direction);
		analogWrite(PWMA, speed);   //// Writes an analog value (PWM wave) to a pin.
	}else{
		digitalWrite(BIN1, !direction);
		digitalWrite(BIN2, direction);
		analogWrite(PWMB, speed);
	}
}

void motorControl(float x, float y) {
	boolean currentDirection = y >= analogInput_Middle_Y;

	//map(value, fromLow, fromHigh, toLow, toHigh);
	if(currentDirection == REVERSE){
		y = map(y, analogInput_Middle_Y, analogInput_Min, speed_Min, speed_Max) ;  
	}else{
		y = map(y, analogInput_Middle_Y, analogInput_Max, speed_Min, speed_Max);
	}
  
	int subtractFromLeft = map(x, analogInput_Middle_X, analogInput_Min, speed_Min, y);
	int subtractFromRight = map(x, analogInput_Middle_X, analogInput_Max, speed_Min, y);

	if(subtractFromRight < 0){
		subtractFromRight = 0;
	}
	
	if(subtractFromLeft < 0){
		subtractFromLeft = 0;
	}
	
	int Throttle_RIGHT = y - subtractFromRight;
	int Throttle_LEFT = y - subtractFromLeft;
	
	boolean currentDirection_LEFT = currentDirection;
	boolean currentDirection_RIGHT = currentDirection;
	
	
	if(Throttle_LEFT < 1 && Throttle_RIGHT > 1){
		currentDirection_LEFT = !currentDirection; ////如果值为true,则!运算后为false
		Throttle_LEFT = Throttle_RIGHT;
	}
	
	if(Throttle_RIGHT < 1 && Throttle_LEFT > 1){
		currentDirection_RIGHT = !currentDirection;
		Throttle_RIGHT = Throttle_LEFT;
	}
	
	move(1, Throttle_LEFT, currentDirection_LEFT);
	move(2, Throttle_RIGHT, currentDirection_RIGHT);

Probably the best thing you can do is to add Serial.println() to the appropriate places in the program, and then watch the Serial Monitor as the program runs.

In some situations, this is not ideal because Serial.println() is quite slow and the delay it causes might interfere with the operation of your program. An alternative is to connect an LED to a spare pin and blink it as an indicator of the program state.

For debugging very fast stuff, I have found it useful to connect a cheap logic analyzer to spare pins. That allows me to record the pin states and then examine them later. That would not have been possible with LEDs, since they would have been blinking too fast for me to see.

The professional approach is to use an specialized hardware debugger. Unfortunately, we do not have good solutions in that area in the Arduino world, though the microcontrollers we're using certainly do support it.

1 Like

pert:
Probably the best thing you can do is to add Serial.println() to the appropriate places in the program, and then watch the Serial Monitor as the program runs.

In some situations, this is not ideal because Serial.println() is quite slow and the delay it causes might interfere with the operation of your program. An alternative is to connect an LED to a spare pin and blink it as an indicator of the program state.

For debugging very fast stuff, I have found it useful to connect a cheap logic analyzer to spare pins. That allows me to record the pin states and then examine them later. That would not have been possible with LEDs, since they would have been blinking too fast for me to see.

The professional approach is to use an specialized hardware debugger. Unfortunately, we do not have good solutions in that area in the Arduino world, though the microcontrollers we're using certainly do support it.

Thank you pert, I'll try it.