第8步Arduino编程
对Arduino编程之前,需要先安装Amarino library。
Arduino的原代码如下:
#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| StartingUp.. |");
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]<=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]<=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]<=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]<=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);
}
}
}
猜你会喜欢
iPhone控制的简易物联网
这次我尝试制作一个简单的物联网系统,可以...
TEM36温度传感器
非常容易解释什么是模拟温度传感器,它就是...
基于MK802的摄像头自动光源跟踪
基于MK802的摄像头自动光源跟踪,展示...
用Android和Arduino控制门锁
这次我制作一个简易的无线控制门锁,实现用...
Arduino加速度传感器与幻彩立方的互动
这篇文章介绍MM7361三轴加速度传感器...
评 论
选择昵称后请轻按空格键
请勿进行人身攻击,谩骂以及任何违法国家相关法律法规的言论。