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

Arduino Project 010 - Serial Controlled Mood Lamp

RonWang5个月前 (07-23)电子编程82

For Project 10, you will revisit the circuit from Project 8 — RGB Mood Lamp, but you’ll now delve into the world of serial communications. You’ll control your lamp by sending commands from the PC to the Arduino using the Serial Monitor in the Arduino IDE. Serial communication is the process of sending data one bit at a time across a communication link. 

This project also introduces how to manipulate text strings. So, set up the hardware as you did in Project 8 and enter the new code. 

项目10 Serial Controlled Mood Lamp

08 RGB Mood Lamp Circuit

/* Coding Ron Wang
   July 23rd 2024
   Autaba support for coding hardware
 */
// Project 10 - Serial controlled mood lamp
char buffer[18];
int red, green, blue;
int RedPin = 11;
int GreenPin = 10;
int BluePin = 9;
void setup()
{
 Serial.begin(9600);
 Serial.flush();
 pinMode(RedPin, OUTPUT);
 pinMode(GreenPin, OUTPUT);
 pinMode(BluePin, OUTPUT);
}
void loop()
{
 if (Serial.available() > 0) {
 int index=0;
 delay(100); // let the buffer fill up
 int numChar = Serial.available();
 if (numChar>15) {
 numChar=15;
 }
 while (numChar--) {
 buffer[index++] = Serial.read();
 }
 splitString(buffer);
 }
}
void splitString(char* data) {
 Serial.print("Data entered: ");
 Serial.println(data);
 char* parameter;
 parameter = strtok (data, " ,");
 while (parameter != NULL) {
 setLED(parameter);
 parameter = strtok (NULL, " ,");
}
 // Clear the text and serial buffers
 for (int x=0; x<16; x++) {
 buffer[x]='\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(RedPin, 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(GreenPin, 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(BluePin, Ans);
 Serial.print("Blue is set to: ");
 Serial.println(Ans);
 }
}

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

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

标签: Arduino

相关文章

Arduino Project 040 - Ultrasonic Distance Alarm

Arduino Project 040 - Ultrasonic Distance Alarm

The sensor consists of two primary components: a transmitter and a receiver . The transmitter is res...

Arduino Project 019 - LED Dot Matrix Display - Basic Animation

Arduino Project 019 - LED Dot Matrix Display - Basic Animation

So far you have dealt with individual 5mm LEDs. LEDs can also be obtained in a package known as a do...

Arduino Project 011 - Piezo Sounder Alarm

Arduino Project 011 - Piezo Sounder Alarm

Arduino project - sounder and sensors : Include project 11 Piezo Sounder Alarm ,Project piezo sounde...

Arduino Project 001 - LED Blink

Arduino Project 001 - LED Blink

Arduino 电子编程--灯项目及控制,主要使用Arduino编程控制LED灯,实现基本控制Project 1 LED闪烁,基本的应用Project 3和4红绿灯项目项目1 Project...

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

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

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

 Arduino Project 043 - SD CardTemperature Datalogger

Arduino Project 043 - SD CardTemperature Datalogger

Todady I made a simple Arduino datalogger using SD card and DHT11  relative humidity and t...

发表评论

访客

看不清,换一张

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