Stap 5: Arduino software
De software van de arduino is niet zo moeilijk. Ik heb toegevoegd opmerkingen, maar als iets niet duidelijk, laat het me weten.
#define Motor1_Dir 13 // Motor 1 direction control pin #define Motor2_Dir 11 // Motor 2 direction control pin #define Motor1_PWM 12 // Motor 1 speed control pin #define Motor2_PWM 10 // Motor 2 speed control pin int channel[4] = {0, 0, 0, 0}; // A variable to save the remote control values int pwm[4] = {0, 0, 0, 0}; // A variable to save the remote values from -255 to 255 int prev_pwm[4] = {0, 0, 0, 0}; // A variable to check if the state changed int pwm_m1 = 0; // A variable to save the speed of the first motor int pwm_m2 = 0; // A variable to save the speed of the second motor int reverse = 0; // A variable to check if the car needs to drive backwards int prev_reverse = 0; // A variable to check if the state of reverse changed const int pins[4] = {2, 3, 4, 5}; // A const variable for the pins of the remote void setup() { pinMode(2, INPUT); //Ch1 pinMode(3, INPUT); //Ch2 pinMode(4, INPUT); //Ch3 pinMode(5, INPUT); //Ch4 pinMode(6, INPUT); //Ch5 pinMode(7, INPUT); //Ch6 pinMode(Motor1_Dir, OUTPUT); pinMode(Motor2_Dir, OUTPUT); pinMode(Motor1_PWM, OUTPUT); pinMode(Motor2_PWM, OUTPUT); digitalWrite(Motor1_Dir, LOW); // Motor 1 forward digitalWrite(Motor2_Dir, LOW); // Motor 2 forward } void loop() { for (int i = 1; i < 3; i++) // check channel 2 and 3 (from 0 to 3) { channel[i] = pulseIn(pins[i], HIGH, 25000); // Read the channel pulse channel[i] = (channel[i] == 0) ? 990 : channel[i]; // If there is an error the value is 0 /***************************************** * Same as this but shorter: * if(channel[i] == 0) * { * channel[i] = 990; * } * else * { * channel[i] = channel[i]; * } *****************************************/ pwm[i] = map(channel[i], 990, 2000, -255, 255); // Convert from (990 - 2000) to (-255 - 255) if (i == 2) // If channel 3 is checked (vertical movement) { reverse = (pwm[i] < 2) ? 1 : 0; // Forward or backward pwm[i] = (pwm[i] < 0) ? (255 + pwm[i]) : pwm[i]; // if backward, instead of (-255 to 0) => (0 to 255) } if ((prev_pwm[i] + 4 < pwm[i]) || (prev_pwm[i] - 4 > pwm[i])) // If there is a minimal movement of 4 in one direction { /** Define motor 1 speed **/ pwm_m1 = pwm[2] - pwm[1]; pwm_m1 = (pwm_m1 < 0)? 0: pwm_m1; // Minimum 0, not negative pwm_m1 = (pwm_m1 > 255)? 255: pwm_m1; // Maximum 255, no more /** Define motor 2 speed **/ pwm_m2 = pwm[2] + pwm[1]; pwm_m2 = (pwm_m2 < 0)? 0: pwm_m2; // Minimum 0, not negative pwm_m2 = (pwm_m2 > 255)? 255: pwm_m2; // Maximum 255, no more analogWrite(Motor1_PWM, pwm_m1); // Write speed to the motors analogWrite(Motor2_PWM, pwm_m2); prev_pwm[i] = pwm[i]; // Place here to check for minimal movement of 4 (see up) } if (reverse != prev_reverse) // If the direction is changed { digitalWrite(Motor1_Dir, reverse); // Write it to the H bridge digitalWrite(Motor2_Dir, reverse); // Write it to the H bridge prev_reverse = reverse; // Define for later checking } } delay(100); // sleep a small time }
Dus is dat alle code.