我的arduino代码遇到了一些问题。我的代码有3个电位器可以改变LED中的每个颜色,还有一个按钮可以通过红色,绿色,蓝色淡入淡出(按下时,它会循环一次,当它连续循环时)。它曾经工作,但当我尝试添加一个显示每个RGB值的LCD屏幕时,它停止工作。我知道这不是最好的,它只是一个实验性的代码,因为我刚刚得到一个arduino,我只是想熟悉自己。当我插上电源时,液晶显示屏会打印出顶行的所有白色方块,而指示灯呈红色闪烁蓝色,按下按钮或转动电位器时没有任何反应。这是代码,任何帮助表示赞赏:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potPin1 = 14 ; // select the input pin for the potentiometer, ANALOG
int potPin2 = 15;
int potPin3 = 16;
int potVal1 = 0; // variable to store the value coming from the sensor
int potVal2 = 0;
int potVal3 = 0;
int ledPin1 = 6; // select the pin for the LED, PWM for analogWrite capability?
int ledPin2 = 9;
int ledPin3 = 10;
int buttonPin = 7;
int buttonState= LOW;
String printCycleVal1 = (""); //print values when cycling colors
String printCycleVal2 = ("");
String printCycleVal3 = ("");
String printVal1 = ("");//print values when not cycling colors
String printVal2 = ("");
String printVal3 = ("");
int val1 = 0; //values to store rgb colors in
int val2 = 0;
int val3 = 0;
void setup()
{
pinMode(potPin1, INPUT);
pinMode(potPin2, INPUT);
pinMode(potPin3, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
lcd.begin(2,16);
}
void loop()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RGB Values: ");
lcd.setCursor(0,1);
potVal1 = analogRead(potPin1);
val1 = map(potVal1, 0, 1023, 0, 255);
analogWrite(ledPin1, val1);
printVal1 += ("V1: ");
printVal1 += (val1);
printVal1 += (" | ");
potVal2 = analogRead(potPin2);
val2 = map(potVal2, 0, 1023, 0, 255);
analogWrite(ledPin2, val2);
printVal2 += ("V2: ");
printVal2 += (val2);
printVal2 += (" | ");
potVal3 = analogRead(potPin3);
val3 = map(potVal3, 0, 1023, 0, 255);
analogWrite(ledPin3, val3);
printVal3 = ("V3: ");
printVal3 += (val3);
buttonState = digitalRead(buttonPin);
while (buttonState == HIGH)
cycle();
}
void cycle() {
setColourRgb(0, 0, 0);
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour += 1) {
int incColour = decColour == 2 ? 0 : decColour + 1;
// cross-fade the two colours.
for(int i = 0; i < 255; i += 1) {
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RGB Values: ");
lcd.setCursor(0,1);
printCycleVal1 += ("V1: ");
printCycleVal1 += (rgbColour[0]);
printCycleVal1 += (" | ");
printCycleVal2 += ("V2: ");
printCycleVal2 += (rgbColour[1]);
printCycleVal2 += (" | ");
printCycleVal3 += ("V3: ");
printCycleVal3 += (rgbColour[2]);
delay(5);
}
buttonState = LOW; n
}
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
analogWrite(ledPin1, red);
analogWrite(ledPin2, green);
analogWrite(ledPin3, blue);
}