domingo, 4 de novembro de 2018

código arduino ,,esquema

/*
  Front Motor (Steering) => Channel A
  Back Motor => Channel B

  Since the motor shield hijacks 6 pins for the motors'
  control, they are declared in the MotorShieldR3 library.
*/
#include <MotorShield.h>
#include <AFMotor.h>

AF_DCMotor motorsteer(1);
AF_DCMotor motorengine(2);

#define pinfrontLights   4      //Pin that activates the Front lights.
#define pinbackLights     8     //Pin that activates the Back lights. 
char command = 'S';
char prevCommand = 'A';
int velocity = 0;
unsigned long timer0 = 2000;  //Stores the time (in millis since execution started)
unsigned long timer1 = 0;  //Stores the time when the last command was received from the phone

void setup()
{
  Serial.begin(9600);  //Set the baud rate to that of your Bluetooth module.
  pinMode(pinfrontLights , OUTPUT);
  pinMode(pinbackLights , OUTPUT);
  motorengine.setSpeed(200);
  motorsteer.setSpeed(200);
}

void loop() {
  if (Serial.available() > 0) {
    timer1 = millis();
    prevCommand = command;
    command = Serial.read();
    //Change pin mode only if new command is different from previous.
    if (command != prevCommand) {
      //Serial.println(command);
      switch (command) {
        case 'F':
          motorengine.run(FORWARD);
          break;
        case 'B':
          motorengine.run(BACKWARD);
          break;
        case 'L':
          motorsteer.run(FORWARD);
          break;
        case 'R':
          motorsteer.run(BACKWARD);
          break;
        case 'S':
          motorsteer.run(RELEASE);
          motorengine.run(RELEASE);
          break;
        case 'I':  //FR
          motorengine.run(FORWARD);
          motorsteer.run(BACKWARD);

          break;
        case 'J':  //BR
          motorengine.run(BACKWARD);
          motorsteer.run(BACKWARD);

          break;
        case 'G':  //FL
          motorengine.run(FORWARD);
          motorsteer.run(FORWARD);

          break;
        case 'H':  //BL
          motorengine.run(BACKWARD);
          motorsteer.run(FORWARD);
          break;
        case 'W':  //Font ON
          digitalWrite(pinfrontLights, HIGH);
          break;
        case 'w':  //Font OFF
          digitalWrite(pinfrontLights, LOW);
          break;
        case 'X':
          digitalWrite(pinfrontLights, HIGH);
          digitalWrite(pinbackLights, HIGH);
          break;
        case 'x':
          digitalWrite(pinfrontLights, LOW);
          digitalWrite(pinbackLights, LOW);
          break;

        case 'U':  //Back ON
          digitalWrite(pinbackLights, HIGH);
          break;
        case 'u':  //Back OFF
          digitalWrite(pinbackLights, LOW);
          break;
        case 'D':  //Everything OFF
          digitalWrite(pinfrontLights, LOW);
          digitalWrite(pinbackLights, LOW);
          motorengine.run(RELEASE);
          motorsteer.run(RELEASE);
          break;
        default:  //Get velocity
          if (command == 'q') {
            velocity = 255;  //Full velocity
            motorengine.run(RELEASE);
            motorsteer.run(RELEASE);
            motorengine.run(RELEASE);
            digitalWrite(pinfrontLights, LOW);
            digitalWrite(pinbackLights, LOW);
          }
          else {
            //Chars '0' - '9' have an integer equivalence of 48 - 57, accordingly.
            if ((command >= 48) && (command <= 57)) {
              //Subtracting 48 changes the range from 48-57 to 0-9.
              //Multiplying by 25 changes the range from 0-9 to 0-225.
              velocity = (command - 48) * 25;
              motorsteer.run(RELEASE);
              motorengine.run(RELEASE);

            }
          }
      }
    }
  }
  else {
    timer0 = millis();  //Get the current time (millis since execution started).
    //Check if it has been 500ms since we received last command.
    if ((timer0 - timer1) > 500) {
      //More tan 500ms have passed since last command received, car is out of range.
      //Therefore stop the car and turn lights off.
      digitalWrite(pinfrontLights, LOW);
      digitalWrite(pinbackLights, LOW);

    }
  }
}

Nenhum comentário:

Postar um comentário