|
下面是arduino的程序,中心板四个电调接口口其中各有一个管脚输出控制亮灯的pwm,
根据不同脉冲宽度点亮不同颜色的灯。
int pin = 7;
int red = 2;
int green = 3;
int blue = 5;
unsigned long duration;
void setup()
{
Serial.begin(38400); // Open serial connection to report values to host
Serial.println("Starting up");
pinMode(pin, INPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
void loop()
{
//digitalWrite(blue,HIGH);
//digitalWrite(green,HIGH);
duration = pulseIn(pin, HIGH);
Serial.print(duration);
Serial.print("\n");
// if ( duration > 380 && duration <480 ) YELLOW
//if ( duration > 280 && duration <380 )//GREEN
if ( duration > 180 && duration <280 )//RED
{
digitalWrite(red, HIGH);
}
else
{
digitalWrite(red, LOW);
if ( duration > 280 && duration <380 )//GREEN
{
digitalWrite(green, HIGH);
}
else
{
digitalWrite(green, LOW);
if ( duration > 380 && duration <480 ) //YELLOW
{
digitalWrite(blue, HIGH);
}
else
{
digitalWrite(blue, LOW);
}
}
}
}
|
|