1

我的 arduino 代码有问题。我的代码有 3 个电位器,它们可以改变 LED 中的每种颜色,还有一个按钮,它会淡出红色、绿色、蓝色(按下时会循环一次,按住时会连续循环)。它曾经可以工作,但是当我尝试添加一个显示每个 RGB 值的 LCD 屏幕时,它停止了工作。我知道它写得不是最好的,它只是一个实验代码,因为我刚得到一个 arduino 并且只是想熟悉自己。当我插入它时,LCD 会在顶行打印所有白色方块,并且 LED 为红色并闪烁蓝色,当我按下按钮或转动锅时没有任何反应。这是代码,任何帮助表示赞赏:

#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);
}
4

0 回答 0