// Wifi-Car Code for FlyMaple // test control motors // // This code is basically copied from examples at DFrobot and arduino. So it is well explained and documented online. // You will need: // flymaple + motors + motor driver // LED will light when serial command is entered. // www.Open-Drone.org // Motors int M1 = 5; int E1 = 4; int M2 = 6; int E2 = 7; void setup() { SerialUSB.begin(); SerialUSB.println("Wifi-Car booting up"); pinMode(BOARD_LED_PIN, OUTPUT); pinMode(E1, OUTPUT); pinMode(E2, OUTPUT); pinMode(M1, PWM); pinMode(M2, PWM); Motor1(0,false); Motor2(0,false); delay(50); } void loop() { char value; while (SerialUSB.available()){ digitalWrite(BOARD_LED_PIN,HIGH); value = SerialUSB.read(); if(value!=-1) { switch(value) // here are all the commands i will be sending from the usb connection { case 'w'://Move ahead Motor1(65000,true); Motor2(65000,true); delay(50); Motor1(0,false); Motor2(0,false); break; case 'x'://move back Motor1(65000,false); Motor2(65000,false); delay(50); Motor1(0,false); Motor2(0,false); break; case 'd'://turn left Motor1(65000,false); Motor2(65000,true); delay(50); Motor1(0,false); Motor2(0,false); break; case 'a'://turn right Motor1(65000,true); Motor2(65000,false); delay(50); Motor1(0,false); Motor2(0,false); break; case 's'://stop Motor1(0,false); Motor2(0,false); break; } } } digitalWrite(BOARD_LED_PIN,LOW); delay(20); } // Basic motor functions void Motor1(int pwm, boolean reverse) { analogWrite(M1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed if(reverse) digitalWrite(E1,HIGH); else digitalWrite(E1,LOW); } void Motor2(int pwm, boolean reverse) { analogWrite(M2,pwm); if(reverse) digitalWrite(E2,HIGH); else digitalWrite(E2,LOW); }