My_arduino(2)小项目代码1
程序员文章站
2024-03-23 17:42:10
...
My_arduino(2)小项目代码1
这里主要依托Michael McRoberts 著的《Arduino从基础到实践》进行总结,在本博文中,进行对书中给出的项目从3-26的代码进行给出,希望可以帮助大家在学习的过程中减少对一些琐碎的代码的书写。后续代码将在以后更新。。
项目3 -----交通信号灯
//项目3 -----交通信号灯
//5秒红灯,然后闪3秒灭;2秒黄灯,灭;3秒绿灯,闪三秒灭
void setup() {
// put your setup code here, to run once:
pinMode(8,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void flash(int pin){
digitalWrite(pin,LOW);
delay(500);
digitalWrite(pin,HIGH);
delay(500);
digitalWrite(pin,LOW);
delay(500);
digitalWrite(pin,HIGH);
delay(500);
digitalWrite(pin,HIGH);
delay(500);
digitalWrite(pin,LOW);
delay(500);
digitalWrite(pin,HIGH);
delay(500);
return ;
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13,HIGH);
delay(5000);//亮5秒
flash(13);
digitalWrite(13,LOW);
digitalWrite(12,HIGH);
delay(3000);
digitalWrite(12,LOW);
digitalWrite(8,HIGH);
delay(3000);
flash(8);
digitalWrite(8,LOW);
delay(10000);
}
project4(待补充)
project5 跑马灯
//project5 跑马灯
byte pin[10] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
byte count = 0;
int ledDelay = 100;//这个变量控制住了跑马灯的速度
int direction = 1;
unsigned long changeTime;
void setup() {
// put your setup code here, to run once:
for (int i = 0; i < 10; i++)
pinMode(pin[i], OUTPUT);
changeTime = millis();
}
void loop() {
// put your main code here, to run repeatedly:
if (millis() - changeTime > ledDelay) {
changeLed();
changeTime = millis();
}
}
void changeLed() {
for (int i = 0; i < 10; i++) {
digitalWrite(pin[i], LOW);
}
digitalWrite(pin[count], HIGH);
count += direction;
if (count == 9) {
direction = -1;
}
if (count == 0) {
direction = 1;
}
}
project6 电位器控制跑马灯
//project6 电位器控制跑马灯
byte pin[10] = {4,5,6,7,8,9,10,11,12,13};
byte count = 0;
int ledDelay;//这个变量控制住了跑马灯的速度
int direction = 1;
unsigned long changeTime;
int potPin = 2;//电位计的输入引脚
void setup() {
// put your setup code here, to run once:
for (int i=0;i<10;i++)
pinMode(pin[i],OUTPUT);
changeTime = millis();
}
void loop() {
ledDelay = analogRead(potPin);//从电位计中读值。
// put your main code here, to run repeatedly:
if (millis() - changeTime > ledDelay){
changeLed();
changeTime = millis();
}
}
void changeLed(){
for(int i = 0;i<10;i++){
digitalWrite(pin[i],LOW);}
digitalWrite(pin[count],HIGH);
count += direction;
if(count == 9){direction = -1;}
if(count == 0){direction = 1;}
}
project7 控制小灯的亮度
//project7 控制小灯的亮度。
int pin = 11;
float sinVal;
int ledVal;
void setup() {
// put your setup code here, to run once:
pinMode(pin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
for (int x = 0; x < 180; x++) {
sinVal = (sin(x * 3.1412 / 180)); //从0到1嘛
ledVal = int(sinVal * 255); //从0 - 255 嘛
analogWrite(pin, ledVal);
delay(25);
}
}
project8 彩灯
//project8 彩灯
float RGB1[3];
float RGB2[3];
float INC[3];
int red,green,blue;
int redpin =11;
int greenpin = 10;
int bluepin = 9;
void setup() {
// put your setup code here, to run once:
randomSeed(analogRead(0));
RGB1[0] = 0;
RGB1[1] = 0;
RGB1[2] = 0;
RGB2[0] = random(256);
RGB2[1] = random(256);
RGB2[2] = random(256);
}
void loop() {
// put your main code here, to run repeatedly:
randomSeed(analogRead(0));
for (int i =0;i<3;i++){
INC[i] = (RGB1[i] - RGB2[i])/256;}
for (int i=0;i<256;i++){
red = int(RGB1[0]);
green = int(RGB1[1]);
blue = int(RGB1[2]);
analogWrite(redpin,red);
analogWrite(greenpin,green);
analogWrite(bluepin,blue);
delay(100);
RGB1[0] -= INC[0];
RGB1[1] -= INC[1];
RGB1[2] -= INC[2];
}
for (int i=0;i<3;i++){
RGB2[i] = random(556)-300;
RGB2[i] = constrain(RGB2[i],0,255);
delay(1000);
}
}
project9 LED火焰效果
//project9 LED火焰效果
int pin1 = 9;
int pin2 = 10;
int pin3 = 11;
void setup() {
// put your setup code here, to run once:
pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);
pinMode(pin3,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(pin1,random(120)+135);
analogWrite(pin2,random(120)+135);
analogWrite(pin3,random(120)+135);
delay(random(100));
}
project10 串口控制灯
//project10 串口控制灯
char b[18];
int r,g,b;
int rpin = 11;
int gpin = 10;
int bpin = 9;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.fulsh();
pinMode(rpin,OUTPUT);
pinMode(gpin,OUTPUT);
pinMode(bpin,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available() > 0){
int index = 0;
delay(100);//等待缓冲区填满
int num = Serial.available();
if (num >15){
num = 15;
}
while (num--){
b[index++] = Serial.read();
}
splitString(butter);
}
}
void splitString(char* data){
Serial.print("Data enterd:");
Serial.println(data);
char* p;
p = strtok (NULL,",");
while ( p != NULL){
setLED(p);
p = strtok (NULL,",");
}
//清除窗口缓存区中的脚本
for (int i = 0;i<16;i++){
b[i] = '\0';
}
Serial.flush();
}
void setLED(char* data){
if ((data[0] == 'r') || (data[0] =='R')){
int Ans = strtol(data+1,NULL,10);
Ans = (constrain(Ans,0,255);
analogWrite(rpin,Ans);
Serial.print("Red is set to :");
Serial.println(Ans):
}
if((data[0] == 'g') || (data[0] =='G')){
int Ans = strtol(data+1,NULL,10);
Ans = constrain(Ans,0,255):
analogWrite(gpin,Ans);
Serial.print("Green is set to :");
Serial.println(Ans):
}
if((data[0] == 'b') || (data[0] =='B')){
int Ans = strtol(data+1,NULL,10);
Ans = constrain(Ans,0,255):
analogWrite(bpin,Ans);
Serial.print("Blue is set to :");
Serial.println(Ans):
}
}
project11 压电声音报警器
//project11 压电声音报警器
float sinVal;
int toneVal;
void setup() {
// put your setup code here, to run once:
pinMode(8, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i < 180; i++) {
//当使用sin函数时转化角度到弧度
sinVal = (sin(i * (3.1412 / 180))); //0-1的数字
toneVal = 2000 + (int(sinVal * 1000));
tone(8, toneVal);
delay(2);//因为for循环时间很短,所以延时让再次改变声音之前持续至少2ms
}
}
project12 压电扬声器的音乐演奏
//project12 压电扬声器的音乐演奏
//这个时定义的音符,相当于都如艾米发馊拉稀
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
//这个是定义的音调
#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.125
#define SIXTEENTH 0.0625
//这个就是谱了,,,牛逼。。。
int tune[] = {NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_B3, NOTE_G3,
NOTE_A3, NOTE_C4, NOTE_C4, NOTE_G3, NOTE_G3, NOTE_F3, NOTE_F3, NOTE_G3, NOTE_F3,
NOTE_E3, NOTE_G3, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_A3, NOTE_B3, NOTE_C4, NOTE_D4
};
//几拍
float duration[] = {EIGHTH, QUARTER + EIGHTH, SIXTEENTH, QUARTER, QUARTER, HALF,
HALF, HALF, QUARTER, QUARTER, HALF + QUARTER, QUARTER, QUARTER, QUARTER, QUARTER + EIGHTH,
EIGHTH, QUARTER, QUARTER, QUARTER, EIGHTH, EIGHTH, QUARTER, QUARTER, QUARTER, QUARTER, HALF + QUARTER
};
int length;
void setup() {
// put your setup code here, to run once:
pinMode(8, OUTPUT);
length = sizeof(tune) / sizeof(tune[0]);
}
void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i < length; i++) {
tone(8, tune[i]);
delay(1500 * duration[i]);
noTone(8);
}
delay(5000);
}
project13 振动传感器
//project13 振动传感器 未成功进行。因为没有压电器
int ledpin = 9;
int piezopin = 5;
int threshold = 120;//传感器产生动作的阈值
int sensorvalue = 0;//存储从传感器读出的值的变量
float ledvalue = 0; //控制led亮度
void setup() {
// put your setup code here, to run once:
pinMode(ledpin, OUTPUT);
Serial.begin(9600);
//闪烁led俩次显示程序已经开始
digitalWrite(ledpin, HIGH); delay(150); digitalWrite(ledpin, LOW);
delay(150);
digitalWrite(ledpin, HIGH); delay(150); digitalWrite(ledpin, LOW);
delay(150);
}
void loop() {
// put your main code here, to run repeatedly:
sensorvalue = analogRead(piezopin);//从传感器读值
Serial.println(sensorvalue);
if (sensorvalue >= threshold) {//如果检测到敲击,设置亮度为最大值
ledvalue = 255;
}
analogWrite(ledpin, int(ledvalue) ); //写亮度值到led
ledvalue = ledvalue - 0.05; //慢慢使led变暗
if (ledvalue <= 0 ) {
ledvalue = 0; //确保没有低于0
}
}
project14 光敏元件
//project14 光敏元件
int piezopin = 8;
int ldrpin = 0;
int ldrvalue = 0;//从LDR中读到的值
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
ldrvalue = analogRead(ldrpin);
Serial.println(ldrvalue);
tone(piezopin, 1000); //用压电盘发出1000hz的声音
delay(25);//短促的提示音
noTone(piezopin);//停止声音
delay(ldrvalue);//等上这么多秒
}
project15 简单的电机控制
//project15 简单的电机控制
int potpin = 0; //模拟引脚0连接到变阻器
int transistorpin = 9; // PWM引脚9连接到三极管
int potvalue = 0; //从变阻器中读出的模拟值
void setup() {
// put your setup code here, to run once:
//设置连接到三极管上的引脚模式为输出
pinMode(transistorpin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//读变阻器值并转化为0-255
potvalue = analogRead(potpin) / 4;
//使用这个值控制三极管
analogWrite(transistorpin, potvalue);
Serial.println(potvalue);
}
//接上电源后,就可以通过转动变阻器来控制电机的速度了。
project16 使用L293D电机驱动芯片
//project16 使用L293D电机驱动芯片
/*注:一旦上电L293D芯片将变得非常热。
* 改变变压器可改变电机速度
* 诺要改变电机的转向,首先要把速度设置为最小,按扳扭开关,电机将反向旋转。
*
*/
#define switchPin 2//按钮开关输入
#define motorPin1 3//L293D 输入1
#define motorPin2 4//L293D 输入2
#define speedPin 9 // L293D使能引脚1
#define potPin 0 // 连接变阻器的模拟引脚
int Mspeed = 0; //变量存储当前的速度值
void setup() {
// put your setup code here, to run once:
//设置按钮开关引脚为INPUT
pinMode(switchPin, INPUT);
// 设置其他引脚为OUTPUT
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(speedPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Mspeed = analogRead(potPin) / 4; //从变阻器中读速度值
analogWrite(speedPin, Mspeed); // 写速度到使能引脚
if ( digitalRead(switchPin)) { //如果按钮开关是HIGH,电机顺时针旋转
digitalWrite(motorPin1, LOW); //设置L293D 输入1为HIGH
digitalWrite(motorPin2, HIGH);//设置L294D输入2为LOW
}
}
project17 移位寄存器8位二进制计数器
//project17 移位寄存器8位二进制计数器 16 ---- 17 都未进行
int latchPin = 8; // Arduino 连接到74HC595 的引脚12(Latch)
int clockPin = 12;//Arudnio连接到74HC595的引脚11(Clock)
int dataPin = 11;//Arduino连接到74HC595的引脚14(Data)
void setup() {
// put your setup code here, to run once:
pinMode(latchPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(dataPin,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// 从0-255计数
for (int i= 0;i < 256;i++){
//设置latchPin引脚为LOW,允许数据输入芯片
digitalWrite(latchPin,LOW);
shiftOut(i);
//设置latchPin 引脚为HIGH,锁存数据并送出数据
digitalWrite(latchPin,HIGH);
delay(1000);
}
}
void shiftOut(byte dataOut){
//在时钟上升沿送8数据
boolean pinState;
digitalWrite(dataPin,LOW);//清除移位寄存器,为送数据做准备
digitalWrite(clockPin,LOW);
for (int i = 0;i<=7;i++){//送出数据的每一位
digitalWrite(clockPin,LOW);//再输出数据前设置clockPin,引脚为LOW
//如果dataOut 与位 掩码进行逻辑或运算的结果是true,设置pinState为HIGH
if (dataOut & (1<<i)){
pinState = HIGH;
}
else{
pinState = LOW;
}
//根据pinState设置dataPin为HIGH或LOW
digitalWrite(dataPin,pinState);//在时钟上升沿送出数据
digitalWrite(clockPin,HIGH);
}
digitalWrite(clockPin,LOW);//停止移位输出数据
}
project18 16位二进制计数器
//project18 16位二进制计数器
int latchPin = 8; // arduino 连接到74HC595 (Latch) 引脚12上的引脚
int clockPin = 12;//Arudnio连接到74HC595的引脚11(Clock)
int dataPin = 11;//Arduino连接到74HC595的引脚14(Data)
void setup() {
// put your setup code here, to run once:
pinMode(latchPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(dataPin,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// 从0-255计数
for (int i= 0;i < 256;i++){
//设置latchPin引脚为LOW,允许数据输入芯片
digitalWrite(latchPin,LOW);
shiftOut(i);
shiftOut(255 - i);
//设置latchPin引脚为HIGH,允许输入数据
digitalWrite(latchPin,HIGH);
delay(250);
}
}
void shiftOut(byte dataOut){
//在时钟上升沿送8数据
boolean pinState;
digitalWrite(dataPin,LOW);//清除移位寄存器,为送数据做准备
digitalWrite(clockPin,LOW);
for (int i = 0;i<=7;i++){//送出数据的每一位
digitalWrite(clockPin,LOW);//再输出数据前设置clockPin,引脚为LOW
//如果dataOut 与位 掩码进行逻辑或运算的结果是true,设置pinState为HIGH
if (dataOut & (1<<i)){
pinState = HIGH;
}
else{
pinState = LOW;
}
//根据pinState设置dataPin为HIGH或LOW
digitalWrite(dataPin,pinState);//在时钟上升沿送出数据
digitalWrite(clockPin,HIGH);
}
digitalWrite(clockPin,LOW);//停止移位输出数据
}
project 23 LCD显示器
//project 23 LCD显示器
#include<LiquidCrystal.h>//还真TM是液 晶 库。。。
//用接口引脚初始化液晶显示库
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //用相应引脚建立一个lcd对象
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2); //设置显示器为16列2行
}
void loop() {
// put your main code here, to run repeatedly:
//运行7个演示程序
basicPrintDemo();
displayOnOffDemo();
setCursorDemo();
scrollLeftDemo();
scrollRightDemo();
cursorDemo();
createGlyphDemo();
}
void basicPrintDemo() {
lcd.clear();//清空显示器
lcd.print("Holl World");//hello world
delay(2000);
}
void displayOnOffDemo() {
lcd.clear();//清空显示器
lcd.print("Display On/Off");//打印字符
for (int i = 0; i < 3; i++) {
lcd.noDisplay();//关闭显示器
delay(1000);
lcd.display();//再开显示器
delay(1000);
}
}
void setCursorDemo() {
lcd.clear();
lcd.print("SetCursor Demo");
delay(1000);
lcd.clear();
lcd.setCursor(5, 0); //把光标设置在第五列第零行
lcd.print("5.0");
delay(2000);
lcd.setCursor(10, 1); //把光标设置在第10列第一行
lcd.print("10,1");
delay(2000);
lcd.setCursor(3, 1); //把光标设置在第3列第一行
lcd.print("3,1");
delay(2000);
}
void scrollLeftDemo() {
lcd.clear();
lcd.print("Scroll Left Demo");
delay(1000);
lcd.clear();
lcd.setCursor(7, 0);
lcd.print("Beginning");
lcd.setCursor(9, 1);
lcd.print("Arduino");
delay(1000);
for (int i = 0; i < 16; i++) {
lcd.scrollDisplayLeft();//向左卷动显示16此
delay(250);
}
}
void scrollRightDemo() {
lcd.clear();
lcd.print("Scroll Right");
lcd.setCursor(0, 1);
lcd.print("Demo");
delay(1000);
lcd.clear();
lcd.print("Beginning");
lcd.setCursor(0, 1);
lcd.print("Arduino");
delay(1000);
for (int i = 0; i < 16; i++) {
lcd.scrollDisplayRight();//向右卷动16次
delay(250);
}
}
void cursorDemo() {
lcd.clear();
lcd.cursor();//使光标可见
lcd.print("Cursor on");
delay(3000);
lcd.clear();
lcd.noCursor();//光标不可见
lcd.print("Cursor Off");
delay(3000);
lcd.clear();
lcd.cursor();
lcd.blink();//光标闪烁
lcd.print("Cursor Blink On");
delay(3000);
lcd.noCursor();//不可见
lcd.noBlink();//关闭闪烁模式
}
void createGlyphDemo() {
lcd.clear();
byte happy[8] = {//生成一个笑脸的字节型数组
B00000,
B00000,
B10001,
B00000,
B10001,
B01110,
B00000,
B00000
};
byte sad[8] = {//生成一个哭脸的字节型数组
B00000,
B00000,
B10001,
B00000,
B01110,
B10001,
B00000,
B00000
};
lcd.createChar(0, happy); // 生成用户字符 0
lcd.createChar(1, sad); //生成用户字符 1
for (int i=0;i<5;i++){
lcd.setCursor(8,0);
lcd.write(1);
delay(1000);
lcd.setCursor(8,0);
lcd.write(1);
delay(1000);
}
}
project24 LCD温度显示器
//project24 LCD温度显示器
#include<LiquidCrystal.h>
//初始化一个LiquidCrystal对象,设置相应的引脚
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //建立一个lcd对象,并设置相应的引脚
int maxC = 0, minC = 100, maxF = 0, minF = 212;
int scale = 1;
int buttonPin = 8;
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2); //设置显示器为16列2行
analogReference(INTERNAL);
pinMode(buttonPin, INPUT);
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0, 0); // 设置光标到它的初始位置
int sensor = analogRead(0); // 从传感器中读值
int buttonState = digitalRead(buttonPin); //检测按钮是否被按下
switch (buttonState) { //如果按钮被按下改变单位状态
case HIGH:
scale = -scale; //改变单位
lcd.clear();
}
delay(250);
switch (scale) { //确定是摄氏度还是华氏度
case 1:
celsius(sensor);
break;
case -1:
fahrenheit(sensor);
}
}
void celsius(int sensor) {
lcd.setCursor(0, 0);
int temp = sensor * 0.09765625; // 转化到摄氏度
lcd.print(temp);
lcd.write(B11011111);//温度符号
lcd.print("C ");
if (temp > maxC) {
maxC = temp;
}
if (temp < minC) {
minC = temp;
}
lcd.setCursor(0, 1);
lcd.print("H=");
lcd.print(maxC);
lcd.write(B11011111);
lcd.print(" C L=");
lcd.print(minC);
lcd.write(B11011111);
lcd.print("C ");
}
void fahrenheit(int sensor) {
lcd.setCursor(0, 0);
float temp = ((sensor * 0.09765625) * 1.8) + 32;//转化为华氏度
lcd.print(int(temp));
lcd.write(B11011111);
lcd.print("F ");
if (temp > maxF) {
maxF = temp;
}
if (temp < minF) {
minF = temp;
}
lcd.setCursor(0, 1);
lcd.print("H=");
lcd.print(maxF);
lcd.write(B11011111);
lcd.print("F L=");
lcd.print(minF);
lcd.write(B11011111);
lcd.print("F ");
}
project25 舵机控制
//project25 舵机控制
#include<Servo.h>
Servo servol;
void setup() {
// put your setup code here, to run once:
servol.attach(5); // 将引脚5上的舵机与舵机对象连接起来
}
void loop() {
// put your main code here, to run repeatedly:
int angle = analogRead(0); //读模拟量值
angle = map(angle,0,1023,0,180);//映射模拟量值到0-180之间
servol.write(angle);//写角度到舵机
delay(15); //延时15毫秒让舵机转到指定位置.
}
项目 26 俩个舵机控制系统
//项目 26 俩个舵机控制系统
#include<Servo.h>
char buffer[10];
Servo servo1; // 声明第一个Servo对象
Servo servo2; // 声明第二个Servo对象
void setup() {
// put your setup code here, to run once:
servo1.attach(5);//引脚5上的舵机连接到servo1对象
servo2.attach(6);//引脚6上的舵机连接到servo2对象
Serial.begin(9600);
Serial.flush();
servo1.write(90); // 设置舵机1 到初始位置
servo2.write(90); // 设置舵机2 到初始位置
Serial.println("STARTING...");
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {//检查是否有数据送到串口
int index = 0;
delay(100); // 延时使缓存填满
int numChar = Serial.available(); // 确定字符串的长度
if (numChar > 10) {
numChar = 10;
}
while (numChar--) {
//用字符串填满缓存
buffer[index++] = Serial.read();
}
splitString(buffer); // 运行splitString函数
}
}
void splitString(char* data) {
Serial.print("Data enterd: ");
Serial.println(data);
char* parameter;
parameter = strtok (data, ","); // 到逗号的字符串
while ( parameter != NULL) { //如果还没有到达字符串结尾
setServo(parameter); // 运行setServo函数
parameter = strtok (NULL, ",");
}
//清除串口缓冲器的脚本
for (int i = 0; i < 9; i++) {
buffer[i] = '\0';
}
Serial.flush();
}
void setServo(char* data) {
if ((data[0] == 'L') || (data[0] == '1')) {
int firstVal = strtol(data + 1, NULL, 10); // 字符串转化成长整型
firstVal = constrain(firstVal, 0, 180); // 数字约束
servo1.write(firstVal);
Serial.print("Servol is set to: ");
Serial.print(firstVal);
}
if ((data[0] == 'R') || (data[0] == 'r')) {
int secondVal = strtol(data + 1, NULL, 10); //字符串转换成长整型
secondVal = constrain(secondVal, 0, 255); // 数字约束
servo2.write(secondVal);
Serial.print("Servo2 is set to : ");
Serial.println(secondVal);
}
}
注:
1.某些代码并没有进行硬件检测,请读者自行把握。
2.给出代码的主要目的是可以帮助读者减少琐碎的时间浪费,例如音乐龙项目。
3.如有错误,欢迎大家在评论区提出!
上一篇: PythonDay3知识点
推荐阅读
-
My_arduino(2)小项目代码1
-
SSH框架网上商城项目第1战之整合Struts2、Hibernate4.3和Spring4.2
-
SSH框架网上商城项目第1战之整合Struts2、Hibernate4.3和Spring4.2
-
C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等)第1/2页
-
asp.net javascript 文件无刷新上传实例代码第1/2页
-
如何在tensorflow2环境运行tensorflow1代码
-
C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等)第1/2页
-
Java 1,2,3,4能组成多少个互不相同且无重复数字的实现代码
-
Java 1,2,3,4能组成多少个互不相同且无重复数字的实现代码
-
Java求1+2!+3!+...+20!的和的代码