//I hope this code works good and please give any suggestions:D (im always updating this code for improvements) //https://www.instructables.com/member/Printerforge/ //!MAKE SURE WHEN WIRING, THAT THE IBUS PIN FROM THE FLYSKY RECIEVER IS UNPLUGED FROM GPIO 0/RX0 AND PLUG IT IN ONCE THE CODE HAS LOADED! //!IF YOU DONT YOUR CODE WILL NOT UPLOAD! #include IBusBM IBus; // Create IBus object // Pin Definitions for L298N Motor Driver const int motor_IN1 = 2; // Connected to IN1 of L298N const int motor_IN2 = 3; // Connected to IN2 of L298N const int motor_ENA = 9; // Connected to ENA of L298N (PWM pin) // Pin Definitions for Light Switch const int lightsPin = 5; // Connected to the light switch pin // Define constants for CH2 and CH3 sections const int minCH2 = 1000; const int midCH2 = 1339; const int maxCH2 = 2000; void setup() { Serial.begin(115200); IBus.begin(Serial); // Initialize IBus with Serial0 pinMode(motor_IN1, OUTPUT); pinMode(motor_IN2, OUTPUT); pinMode(motor_ENA, OUTPUT); pinMode(lightsPin, OUTPUT); // Set the lights pin as output } // Function to drive the motor forward void driveForward(int speed) { digitalWrite(motor_IN1, HIGH); digitalWrite(motor_IN2, LOW); analogWrite(motor_ENA, speed); // Enable the motor driver with PWM speed } // Function to drive the motor backward void driveBackward(int speed) { digitalWrite(motor_IN1, LOW); digitalWrite(motor_IN2, HIGH); analogWrite(motor_ENA, speed); // Enable the motor driver with PWM speed } // Function to stop the motor void stopMotor() { digitalWrite(motor_IN1, LOW); digitalWrite(motor_IN2, LOW); analogWrite(motor_ENA, 0); // Disable the motor driver (set PWM speed to 0) } void loop() { // Read channel values int CH2 = IBus.readChannel(1); // Assuming CH2 is the "drive stick" for motor control int CH3 = IBus.readChannel(2); // Assuming CH3 is the "accelerator stick" for motor speed int CH6 = IBus.readChannel(4); // Assuming CH6 is the light switch channel // Calculate motor speed based on CH3 values int pwm; if (CH3 > 1980) { pwm = 255; // Set maximum PWM if CH3 value is greater than 1980 } else { pwm = map(CH3, 1004, 1980, 0, 255); // Map CH3 range to PWM range } // Determine motor direction based on CH2 position String motorDirection; if (CH2 >= minCH2 && CH2 < 1339) { // Move the motor backward driveBackward(pwm); motorDirection = "Backward"; } else if (CH2 >= 1339 && CH2 < 1663) { // Stop the motor stopMotor(); motorDirection = "Stopped"; } else if (CH2 >= 1663 && CH2 < maxCH2) { // Move the motor forward driveForward(pwm); motorDirection = "Forward"; } // Control lights based on CH6 value if (CH6 < 1500) { // If CH6 is low, turn off lights digitalWrite(lightsPin, LOW); } else { // If CH6 is high, turn on lights digitalWrite(lightsPin, HIGH); } // Print motor direction, PWM, and light switch state to serial monitor Serial.print("CH3: "); Serial.print(CH3); Serial.print(", PWM: "); Serial.print(pwm); Serial.print(", CH2: "); Serial.print(CH2); Serial.print(", Light Switch: "); Serial.print(CH6); Serial.print(", Motor Direction: "); Serial.println(motorDirection); delay(100); // Adjust delay as needed to prevent serial output overflow }