Вот скетч:
#include <MeetAndroid.h>
const int EnablePin1 = 3;
const int EnablePin2 = 6;
const int Motor_1_A = 2;
const int Motor_1_B = 4;
const int Motor_2_A = 5;
const int Motor_2_B = 7;
float data[3] = {0};
int intdata[3] = {0};
int i = 1;
MeetAndroid meetAndroid(error);
void error(uint8_t flag, uint8_t values)
{
Serial.print("ERROR: ");
Serial.print(flag);
}
void setup() {
Serial.begin(9600);
Serial.println("\t\t\t.----------------------.");
Serial.println("\t\t\t| Starting Up.. |");
Serial.println("\t\t\t'----------------------'");
pinMode(EnablePin1, OUTPUT);
pinMode(EnablePin2, OUTPUT);
pinMode(Motor_1_A, OUTPUT);
pinMode(Motor_1_B, OUTPUT);
pinMode(Motor_2_A, OUTPUT);
pinMode(Motor_2_B, OUTPUT);
delay(1000);
meetAndroid.registerFunction(floatValues, 'A');
}
void loop()
{
meetAndroid.receive();
}
void floatValues(byte flag, byte numOfValues)
{
meetAndroid.getFloatValues(data);
for (int i=0; i<3;i++)
{
meetAndroid.send(data[i]);
}
if (-10 <= data[0] && data[0] <= 10) // This is for Forward/Reverse
{
intdata[0] = int(data[0]);
intdata[0] = intdata[0] * 24;
if (data[0] <= -2)
{
intdata[0] = abs(intdata[0]);
digitalWrite(Motor_1_A, HIGH);
digitalWrite(Motor_2_A, HIGH);
digitalWrite(Motor_1_B, LOW);
digitalWrite(Motor_2_B, LOW);
analogWrite(EnablePin1, intdata[0]);
analogWrite(EnablePin2, intdata[0]);
}
else if (data[0] >= 2)
{
digitalWrite(Motor_1_A, LOW);
digitalWrite(Motor_2_A, LOW);
digitalWrite(Motor_1_B, HIGH);
digitalWrite(Motor_2_B, HIGH);
analogWrite(EnablePin1, intdata[0]);
analogWrite(EnablePin2, intdata[0]);
}
else if (-1 <= data[0] && data[0] <= 1)
{
digitalWrite(Motor_1_A, LOW);
digitalWrite(Motor_2_A, LOW);
digitalWrite(Motor_1_B, LOW);
digitalWrite(Motor_2_B, LOW);
digitalWrite(EnablePin1, LOW);
digitalWrite(EnablePin2, LOW);
}
}
if (-10 <= data[1] && data[1] <= 10) // This is for Turn Left/Turn Right
{
if (data[1] <= -2)
{
digitalWrite(Motor_1_A, HIGH);
digitalWrite(Motor_2_B, HIGH);
digitalWrite(Motor_1_B, LOW);
digitalWrite(Motor_2_A, LOW);
analogWrite(EnablePin1, intdata[0]);
analogWrite(EnablePin2, intdata[0]);
}
else if (data[1] >= 2)
{
digitalWrite(Motor_1_A, LOW);
digitalWrite(Motor_2_B, LOW);
digitalWrite(Motor_1_B, HIGH);
digitalWrite(Motor_2_A, HIGH);
analogWrite(EnablePin1, intdata[0]);
analogWrite(EnablePin2, intdata[0]);
}
else if (-1 <= data[1] && data[1] <= 1)
{
digitalWrite(Motor_1_A, LOW);
digitalWrite(Motor_2_A, LOW);
digitalWrite(Motor_1_B, LOW);
digitalWrite(Motor_2_B, LOW);
digitalWrite(EnablePin1, LOW);
digitalWrite(EnablePin2, LOW);
}
}
}