//ARDUINO Hiro NANO
//RXD to TXO (white)
//TXD to RXO (black) BT adapter
// motor one
int enA = 11; //gris
int in1 = 10; // bleu
int in2 = 9; //vert
// motor two
int enB = 3; // violet
int in3 = 6; // jaune
int in4 = 5; // orange
char command;
String btdata;
int power;
int dir;
// Variables for the duration and the distance
long duration;
int distance;
void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(57600);
Serial.print("#ihihuuho");
//MotorsForward();
}
void loop()
{
if (Serial.available() > 0)
{
}
while (Serial.available() > 0)
{ command = (Serial.read());
//Serial.println(" cmdd ");
//Serial.println(command);
//Serial.println("str: " + btdata);
if (command == '?')
{
//Serial.println();
//Serial.println("Command: " + btdata);
delay(1);
if (btdata.indexOf("forw") >= 0){
MotorsForward(255);
}
if (btdata.indexOf("back") >= 0){
MotorsBackward(255);
}
else{
Serial.println("Command not recognized: " + btdata);
}
btdata = "";
}
else if (command == '/')
{
//Serial.println();
//Serial.println("Direction: " + btdata);
dir = btdata.toInt();
btdata = "";
}
else if (command == '%')
{
//Serial.println();
Serial.println("Power: " + btdata);
power = btdata.toInt();
btdata = "";
if ( dir == 1 && power>0) {
MotorsForward(power);
}
else if ( dir == 5 && power>0) {
MotorsBackward(power);
}
else if ( dir == 7 && power>0) {
MotorsSpinR(power);
}
else if ( dir == 3 && power>0) {
MotorsSpinL(power);
}
else if ( dir == 8 && power>0) {
MotorsFR(power);
}
else if ( dir == 2 && power>0) {
MotorsFL(power);
}
else if ( dir == 4 && power>0) {
MotorsBL(power);
}
else if ( dir == 6 && power>0) {
MotorsBR(power);
}
else{
MotorsStop();
}
}
else{
btdata = btdata + command;
}
}
delay(500);
}
void LedBlue()
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
//delay(1000); // wait for a second
}
void LedRed()
{
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
}
void MotorsBackward(int powa)
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, powa);
analogWrite(enB, powa);
}
void MotorsForward(int powa)
{
// Serial.println("Forward!");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, powa);
analogWrite(enB, powa);
}
void MotorsSpinR(int powa)
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, powa);
analogWrite(enB, powa);
}
void MotorsSpinL(int powa)
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, powa);
analogWrite(enB, powa);
}
void MotorsFL(int powa)
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enA, powa);
}
void MotorsFR(int powa)
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB, powa);
}
void MotorsBR(int powa)
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enA, powa);
}
void MotorsBL(int powa)
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, powa);
}
void MotorsStop()
{
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น