คิอผมไใ่เข้าใจ ตรงที่ รับค่า a แล้วจะให้ a ทำงานไปเรื่อยๆ จนกว่าจะรับค่าอื่นมา แล้วถึงเปลี่ยนไปทำงานค่าอื่น จะต้องเขียนแบบไหนครับ
ผู้รู้ช่วยที
#include <SoftwareSerial.h>
SoftwareSerial mySerial (11,10); //TX, RX
#define RED 9
#define GREEN 13
#define BLUE 12
//int fadetime = 200 ;
void setup()
{
mySerial.begin(9600);
pinMode (RED,OUTPUT);
pinMode (GREEN,OUTPUT);
pinMode (BLUE,OUTPUT);
digitalWrite(RED,LOW);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,LOW);
}
void loop()
{
if (mySerial.available()>0)
{
char data = mySerial.read();
if (data=='r')
{
digitalWrite(RED,HIGH);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,LOW);
}
else if(data == 'g')
{
digitalWrite (RED,LOW);
digitalWrite(GREEN,HIGH);
digitalWrite(BLUE,LOW);
}
else if(data == 'b')
{
digitalWrite (RED,LOW);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,HIGH);
}
else if(data == 'y')
{
digitalWrite (RED,HIGH);
digitalWrite(GREEN,HIGH);
digitalWrite(BLUE,LOW);
}
else if(data == 's')
{
digitalWrite (RED,LOW);
digitalWrite(GREEN,HIGH);
digitalWrite(BLUE,HIGH);
}
else if(data == 'm')
{
digitalWrite (RED,HIGH);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,HIGH);
}
else if(data == 'w')
{
digitalWrite (RED,HIGH);
digitalWrite(GREEN,HIGH);
digitalWrite(BLUE,HIGH);
}
else if(data == 'a')
{
{
//r
digitalWrite(RED,HIGH);
digitalWrite(BLUE,LOW);
digitalWrite(GREEN,LOW);
delay(500);
//b
digitalWrite(RED,LOW);
digitalWrite(BLUE,HIGH);
digitalWrite(GREEN,LOW);
delay(500);
//g
digitalWrite(RED,LOW);
digitalWrite(BLUE,LOW);
digitalWrite(GREEN,HIGH);
delay(500);
//y
digitalWrite(RED,HIGH);
digitalWrite(BLUE,HIGH);
digitalWrite(GREEN,LOW);
delay(500);
//c
digitalWrite(RED,LOW);
digitalWrite(BLUE,HIGH);
digitalWrite(GREEN,HIGH);
delay(500);
//m
digitalWrite(RED,HIGH);
digitalWrite(BLUE,LOW);
digitalWrite(GREEN,HIGH);
delay(500);
//y
digitalWrite(RED,HIGH);
digitalWrite(BLUE,HIGH);
digitalWrite(GREEN,HIGH);
delay(500);
}
delay(500);
}
else
{data == 'x';
digitalWrite (RED,LOW);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,LOW);
}
}
}
จะต้องใช้คำสั่งวนซ้ำแบบไหน ใน Arduino
ผู้รู้ช่วยที
#include <SoftwareSerial.h>
SoftwareSerial mySerial (11,10); //TX, RX
#define RED 9
#define GREEN 13
#define BLUE 12
//int fadetime = 200 ;
void setup()
{
mySerial.begin(9600);
pinMode (RED,OUTPUT);
pinMode (GREEN,OUTPUT);
pinMode (BLUE,OUTPUT);
digitalWrite(RED,LOW);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,LOW);
}
void loop()
{
if (mySerial.available()>0)
{
char data = mySerial.read();
if (data=='r')
{
digitalWrite(RED,HIGH);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,LOW);
}
else if(data == 'g')
{
digitalWrite (RED,LOW);
digitalWrite(GREEN,HIGH);
digitalWrite(BLUE,LOW);
}
else if(data == 'b')
{
digitalWrite (RED,LOW);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,HIGH);
}
else if(data == 'y')
{
digitalWrite (RED,HIGH);
digitalWrite(GREEN,HIGH);
digitalWrite(BLUE,LOW);
}
else if(data == 's')
{
digitalWrite (RED,LOW);
digitalWrite(GREEN,HIGH);
digitalWrite(BLUE,HIGH);
}
else if(data == 'm')
{
digitalWrite (RED,HIGH);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,HIGH);
}
else if(data == 'w')
{
digitalWrite (RED,HIGH);
digitalWrite(GREEN,HIGH);
digitalWrite(BLUE,HIGH);
}
else if(data == 'a')
{
{
//r
digitalWrite(RED,HIGH);
digitalWrite(BLUE,LOW);
digitalWrite(GREEN,LOW);
delay(500);
//b
digitalWrite(RED,LOW);
digitalWrite(BLUE,HIGH);
digitalWrite(GREEN,LOW);
delay(500);
//g
digitalWrite(RED,LOW);
digitalWrite(BLUE,LOW);
digitalWrite(GREEN,HIGH);
delay(500);
//y
digitalWrite(RED,HIGH);
digitalWrite(BLUE,HIGH);
digitalWrite(GREEN,LOW);
delay(500);
//c
digitalWrite(RED,LOW);
digitalWrite(BLUE,HIGH);
digitalWrite(GREEN,HIGH);
delay(500);
//m
digitalWrite(RED,HIGH);
digitalWrite(BLUE,LOW);
digitalWrite(GREEN,HIGH);
delay(500);
//y
digitalWrite(RED,HIGH);
digitalWrite(BLUE,HIGH);
digitalWrite(GREEN,HIGH);
delay(500);
}
delay(500);
}
else
{data == 'x';
digitalWrite (RED,LOW);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,LOW);
}
}
}