// Arduino code for controling twin motor RC toys with an hobbygrade transmitter and a l298N H-Bridge. int motor1Left = 5; // In1 on digital pin 5 of the Arduino. int motor1Right= 6; // In2 on digital pin 6 of the Arduino. int motor2Left = 7; // In3 on digital pin 7 of the Arduino. int motor2Right = 8; // In4 on digital pin 8 of the Arduino. int Ch1; // Variables for channel read. int Ch2; void setup () { pinMode (motor1Left, OUTPUT); //Motor pins as outputs pinMode (motor1Right, OUTPUT); pinMode (motor2Left, OUTPUT); pinMode (motor2Right, OUTPUT); pinMode (Ch1, INPUT); //Channel pins as inputs pinMode (Ch2, INPUT); Serial.begin(9600); //Serial monitor begin for channel monitoring } void loop () { Ch2 = (pulseIn (10, HIGH, 21000)); //Reads channel 2 pulses on pin 10. Ch1 = (pulseIn (9, HIGH, 21000)); //Reads channel 1 pulses on pin 9. Serial.print("Channel 1:"); //prints channel readings on the serial monitor. Serial.println(Ch1); Serial.print("Channel 2:"); Serial.println(Ch2); if (Ch2 > 1300 && Ch2 < 1700 ) //If channel 2 is on its neutral position... { Ch1 = (pulseIn (9, HIGH, 21000)); if (Ch1 < 1500 && Ch1 > 1300){ //and if channel 1 is in its neutral position, set both motors off. digitalWrite (motor2Left, LOW); digitalWrite (motor2Right, LOW); digitalWrite (motor1Left, LOW); digitalWrite (motor1Right, LOW); } if (Ch1 < 1300) //and if channel 1 is in right position, drive the motors in a 360 turn right. { digitalWrite (motor1Left, HIGH); digitalWrite (motor1Right, LOW); digitalWrite (motor2Left, LOW); digitalWrite (motor2Right, HIGH); } if (Ch1 > 1500) //and if channel 1 is in left position, drive the motors in a 360 turn left. { digitalWrite (motor1Left, LOW); digitalWrite (motor1Right, HIGH); digitalWrite (motor2Left, HIGH); digitalWrite (motor2Right, LOW); } } if (Ch2 < 1300) //If channel 2 is in foward position... { Ch1 = (pulseIn (9, HIGH, 21000)); if (Ch1 > 1500) //and { digitalWrite (motor1Left, LOW); digitalWrite (motor1Right, HIGH); digitalWrite (motor2Left, LOW); digitalWrite (motor2Right, LOW); } if (Ch1 < 1300) { digitalWrite (motor1Left, LOW); digitalWrite (motor1Right, LOW); digitalWrite (motor2Left, LOW); digitalWrite (motor2Right, HIGH); } else if (Ch1 < 1500 && Ch1 > 1300){ digitalWrite (motor1Left, LOW); digitalWrite (motor1Right, HIGH); digitalWrite (motor2Left, LOW); digitalWrite (motor2Right, HIGH); } } if (Ch2 > 1500) { Ch1 = (pulseIn (9, HIGH, 21000)); if (Ch1 > 1500) { digitalWrite (motor1Left, HIGH); digitalWrite (motor1Right, LOW); digitalWrite (motor2Left, LOW); digitalWrite (motor2Right, LOW); } if (Ch1 < 1300) { digitalWrite (motor1Left, LOW); digitalWrite (motor1Right, LOW); digitalWrite (motor2Left, HIGH); digitalWrite (motor2Right, LOW); } else if (Ch1 < 1500 && Ch1 > 1300){ digitalWrite (motor1Left, HIGH); digitalWrite (motor1Right, LOW); digitalWrite (motor2Left, HIGH); digitalWrite (motor2Right, LOW); } } delay(100); }