Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //project for designing electronic toys, written by Toma Ogawa
- //Assigning pin numbers. n for negative and p for positive
- int motor_right_n = 2;
- int motor_right_p = 3;
- int motor_left_p = 4;
- int motor_left_n = 5;
- //EnA and EnB take the analog PWM input for motors 1 and 2 respectively.
- int EnA = 9;
- int EnB = 10;
- //receives the character sent by the laptop over the Tera Term terminal input.
- int incomingByte = 0;
- void setup()
- {
- //assigning pins
- pinMode(motor_right_n, OUTPUT);
- pinMode(motor_right_p, OUTPUT);
- pinMode(motor_left_p, OUTPUT);
- pinMode(motor_left_n, OUTPUT);
- pinMode(EnA, OUTPUT);
- pinMode(EnB, OUTPUT);
- //making sure everything is off at the start.
- digitalWrite(motor_right_n, LOW);
- digitalWrite(motor_right_p, LOW);
- digitalWrite(motor_left_p, LOW);
- digitalWrite(motor_left_n, LOW);
- digitalWrite(EnA, LOW);
- digitalWrite(EnB, LOW);
- //setting baud rate for connection over HC-06 bluetooth module.
- Serial.begin(9600);
- Serial.println("start");
- }
- void loop()
- {
- if (Serial.available() > 0) {
- //reads character input from laptop
- incomingByte = Serial.read();
- }
- //select case depending on character
- switch(incomingByte)
- {
- //see slide for combinations of digitalWrite values
- case 's':
- digitalWrite(motor_right_n, LOW); // control for stop
- digitalWrite(motor_right_p, LOW);
- digitalWrite(motor_left_p, LOW);
- digitalWrite(motor_left_n, LOW);
- analogWrite(EnA, 0);
- analogWrite(EnB, 0);
- Serial.println("Stop\n");
- incomingByte='*';
- break;
- case 'd':
- digitalWrite(motor_right_n, HIGH); // control for right
- digitalWrite(motor_right_p, LOW);
- digitalWrite(motor_left_p, HIGH);
- digitalWrite(motor_left_n, LOW);
- analogWrite(EnA, 200);
- Serial.println("right\n");
- incomingByte='*';
- break;
- case 'a':
- digitalWrite(motor_right_n, LOW); // control for left
- digitalWrite(motor_right_p, HIGH);
- digitalWrite(motor_left_p, LOW);
- digitalWrite(motor_left_n, HIGH);
- analogWrite(EnB, 200);
- Serial.println("left\n");
- incomingByte='*';
- break;
- case 'w':
- digitalWrite(motor_right_n, HIGH); // control for forward
- digitalWrite(motor_right_p, LOW);
- digitalWrite(motor_left_p, LOW);
- digitalWrite(motor_left_n, HIGH);
- analogWrite(EnA, 200);
- analogWrite(EnB, 200);
- Serial.println("forward\n");
- incomingByte='*';
- break;
- case 'x':
- digitalWrite(motor_right_n, LOW); // control for backward
- digitalWrite(motor_right_p, HIGH);
- digitalWrite(motor_left_p, HIGH);
- digitalWrite(motor_left_n, LOW);
- analogWrite(EnA, 200);
- analogWrite(EnB, 200);
- Serial.println("backwards\n");
- incomingByte='*';
- break;
- delay(5000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement