Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //typedef unsigned char uchar
- struct {
- char get() {
- char ch = 0;
- if(Serial.available()) {
- ch = Serial.read();
- }
- return ch;
- }
- } bluetooth;
- struct Mover{
- static const int LEFT_DIR_PIN = 2;
- static const int LEFT_POWER_PIN = 5;
- static const int RIGHT_DIR_PIN = 4;
- static const int RIGHT_POWER_PIN = 6;
- void init() {
- //right
- pinMode(RIGHT_DIR_PIN, OUTPUT);
- pinMode(RIGHT_POWER_PIN, OUTPUT);
- analogWrite(RIGHT_POWER_PIN, 0);
- //left
- pinMode(LEFT_DIR_PIN, OUTPUT);
- pinMode(LEFT_POWER_PIN, OUTPUT);
- analogWrite(LEFT_POWER_PIN, 0);
- }
- void rotateRight(int speed) {
- digitalWrite(RIGHT_DIR_PIN, LOW);
- analogWrite(RIGHT_POWER_PIN, speed);
- }
- void rotateLeft(int speed) {
- digitalWrite(LEFT_DIR_PIN, HIGH);
- analogWrite(LEFT_POWER_PIN, speed);
- }
- void moveForward(int speed) {
- digitalWrite(RIGHT_DIR_PIN, HIGH);
- digitalWrite(LEFT_DIR_PIN, LOW);
- analogWrite(RIGHT_POWER_PIN, speed);
- analogWrite(LEFT_POWER_PIN, speed);
- }
- void moveBack(int speed) {
- digitalWrite(RIGHT_DIR_PIN, LOW);
- digitalWrite(LEFT_DIR_PIN, HIGH);
- analogWrite(RIGHT_POWER_PIN, speed);
- analogWrite(LEFT_POWER_PIN, speed);
- }
- void stop() {
- analogWrite(RIGHT_POWER_PIN, 0);
- analogWrite(LEFT_POWER_PIN, 0);
- }
- };
- struct Driver {
- char lastCalled;
- Mover mover;
- static const int MAX_SPEED = 255;
- static const int MIN_SPEED = 195;
- static const int SPEED_DELTA = 15;
- int speed = MIN_SPEED;
- bool activated;
- Driver() {
- lastCalled = 0;
- mover.init();
- activated = true;
- }
- void drive(char ch) {
- switch(ch) {
- case 'i':
- speed = speed == MAX_SPEED ? speed : speed + SPEED_DELTA;
- drive(lastCalled);
- break;
- case 'd':
- speed = speed == MIN_SPEED ? speed : speed - SPEED_DELTA;
- drive(lastCalled);
- break;
- case 'l':
- lastCalled = ch;
- mover.rotateLeft(speed);
- break;
- case 'r':
- lastCalled = ch;
- mover.rotateRight(speed);
- break;
- case 'f':
- lastCalled = ch;
- mover.moveForward(speed);
- break;
- case 'b':
- lastCalled = ch;
- mover.moveBack(speed);
- break;
- case 's':
- lastCalled = ch;
- mover.stop();
- break;
- default:
- break;
- }
- }
- } driver;
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- char ch;
- if (ch = bluetooth.get()) {
- Serial.println(ch);
- }
- driver.drive(ch);//*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement