当前位置:首页 > 科学研究 > 电子编程 > 正文内容

Arduino Project 026 - Dual Servo Control

RonWang4周前 (11-14)电子编程72

This project you’ll create another simple project, but this time you’ll control two servos using commands from the serial monitor. You learned about serial control in Project 10 when you were changing the colors on an RGB lamp with serial commands. So let’s cannibalize the code from Project 10 to make this one.

Project 26  Dual Servo Control

/* Coding Ron Wang
   Nov.14th 2024
   Autaba support for coding hardware
   Project 26  Dual Servo Control
 */

#include <Servo.h>
char buffer[10];
Servo servo1; // Create a servo object
Servo servo2; // Create a second servo object
void setup()
{
 servo1.attach(5); // Attaches the servo on pin 5 to the servo1 object
 servo2.attach(6); // Attaches the servo on pin 6 to the servo2 object
 Serial.begin(9600);
 Serial.flush();
 servo1.write(90); // Put servo1 at home position
 servo2.write(90); // Put servo2 at home postion
 Serial.println("STARTING...");
}
void loop()
{
 if (Serial.available() > 0) { // Check if data has been entered
 int index=0;
 delay(100); // Let the buffer fill up
 int numChar = Serial.available(); // Find the string length
if (numChar>10) {
numChar=10;
}
while (numChar--) {
// Fill the buffer with the string
buffer[index++] = Serial.read();
}
splitString(buffer); // Run splitString function
}
}
void splitString(char* data) {
Serial.print("Data entered: ");
Serial.println(data);
char* parameter;
parameter = strtok (data, " ,"); //String to token
while (parameter != NULL) { // If we haven't reached the end of the string...
setServo(parameter); // ...run the setServo function
parameter = strtok (NULL, " ,");
}
// Clear the text and serial buffers
for (int x=0; x<9; x++) {
buffer[x]='\0';
}
Serial.flush();
}
void setServo(char* data) {
if ((data[0] == 'L') || (data[0] == 'l')) {
int firstVal = strtol(data+1, NULL, 10); // String to long integer
firstVal = constrain(firstVal,0,180); // Constrain values
servo1.write(firstVal);
Serial.print("Servo1 is set to: ");
Serial.println(firstVal);
}
if ((data[0] == 'R') || (data[0] == 'r')) {
int secondVal = strtol(data+1, NULL, 10); // String to long integer
secondVal = constrain(secondVal,0,255); // Constrain the values
servo2.write(secondVal);
Serial.print("Servo2 is set to: ");
Serial.println(secondVal);
}
}

Arduino Dual Servo Control Circuit

Arduino Dual Servo Control Schematic

版权声明:本文为原创文章,版权归donstudio所有,欢迎分享本文,转载请保留出处!

本文链接:http://parentscn.com/?id=280

标签: Arduino

相关文章

Arduino Project 044 - Simple RFID Reader

Arduino Project 044 - Simple RFID Reader

Arduino Programming Basic -- Making an RFID ReaderProject 44 Simple RFID ReaderWiring Diagram Betwee...

 Arduino Project 038 - Simple Ultrasonic Range HC-SR04

Arduino Project 038 - Simple Ultrasonic Range HC-SR04

Ultrasonic range finders measure distance by emitting a pulse of ultrasonic sound that travels throu...

Arduino Project 023C - Liquid Crystal Displays - Serial to Display

Arduino Project 023C - Liquid Crystal Displays - Serial to Display

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14...

Arduino Programming Basic - Serial Monitor

Arduino Programming Basic - Serial Monitor

Arduino 程序基础,介绍Arduino程序的基本组成,第一部分编写数个例程,讲解关于变量及变量名称,串口监视器,if循环,for循环,while循环等。第二部分介绍了函数,全局变量,局部变量和静...

C语言调试运行环境Pelles C的安装

C语言调试运行环境Pelles C的安装

C语言调试运行环境之TurboC一文介绍了在32位Windows系统下安装C语言运行环境之TubroC,但是由于TurobC只能在32位系统下运行,导致现在很多Windows10和Windows 11...

Arduino Project 023D - Liquid Crystal Displays - Custom Character

Arduino Project 023D - Liquid Crystal Displays - Custom Character

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。