gpt4 book ai didi

c++ - 开关中无法识别功能

转载 作者:行者123 更新时间:2023-11-30 17:47:11 26 4
gpt4 key购买 nike

我创建了一个程序,每次单击后按钮都会移动某些操作。我正在使用切换方法。除了 Rotate() 之外,一切都很好。如果我直接在循环函数中调用该函数,它就可以正常工作。它只是在这种情况下的 switch 方法中不起作用。有任何想法吗?谢谢。这是代码:

#define PIN_COUNT 10
#define UPDATE_DURATION 30

int states[PIN_COUNT];
int current_pin = 0;
int dir = 1;
int update_count = 0;

int ledPins[PIN_COUNT] = {0,1,2,3,4,5,6,7,8,9};
int switchPin = 13; // choose the input pin (for a pushbutton)


int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
int Mode = 0; // What mode is the light in?
boolean modeChanged = false;
const int NUM_MODES = 14;

/* RGB */
const int RED_PIN = 10;
const int GREEN_PIN = 11;
const int BLUE_PIN = 12;
int DISPLAY_TIME = 100;
/* RGB */


void setup()
{
int index;

for(index = 0; index <= 9; index++)
{
pinMode(ledPins[index],OUTPUT);
// ledPins[index] is replaced by the value in the array.
// For example, ledPins[0] is 2
}
pinMode(switchPin, INPUT); // Set the switch pin as input
buttonState = digitalRead(switchPin); // read the initial state
/* RGB */
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
/* RGB */
for ( int i = 0; i < PIN_COUNT; i++ ) {
pinMode(ledPins[i], OUTPUT);
states[i] = 0;
}
}





/* RGB */
void mainColors()
{
// Off (all LEDs off):

digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);

delay(1000);

// Red (turn just the red LED on):

digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);

delay(1000);

// Green (turn just the green LED on):

digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);

delay(1000);

// Blue (turn just the blue LED on):

digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);

delay(1000);

// Yellow (turn red and green on):

digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);

delay(1000);

// Cyan (turn green and blue on):

digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);

delay(1000);

// Purple (turn red and blue on):

digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);

delay(1000);

// White (turn all the LEDs on):

digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);

delay(1000);
}
/* RGB */

void Decade()
{
int index;
int delayTime = 100; // milliseconds to pause between LEDs


val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
Mode++;
if (Mode >= NUM_MODES) {
Mode = 0;
}
modeChanged = true;
}
}
buttonState = val; // save the new state in our variable
}

if (modeChanged) {
modeChanged = false;

// Now do whatever the lightMode indicates
switch(Mode) {
case 0:
digitalWrite(ledPins[index], LOW);
digitalWrite(BLUE_PIN, HIGH);
break;

case 1:
digitalWrite(ledPins[0], HIGH);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
break;

case 2:
digitalWrite(ledPins[1], HIGH);
break;

case 3:
digitalWrite(ledPins[2], HIGH);
break;

case 4:
digitalWrite(ledPins[3], HIGH);
break;

case 5:
digitalWrite(ledPins[4], HIGH);
break;

case 6:
digitalWrite(ledPins[5], HIGH);
break;

case 7:
digitalWrite(ledPins[6], HIGH);
break;

case 8:
digitalWrite(ledPins[7], HIGH);
break;

case 9:
digitalWrite(ledPins[8], HIGH);
break;

case 10:
digitalWrite(ledPins[9], HIGH);
break;

case 11:
Rotate();
break;

case 12:
digitalWrite(RED_PIN, HIGH);
break;

case 13:
digitalWrite(GREEN_PIN, HIGH);
break;



}
}
}

void loop()
{

Decade(); // RUNTIME!!
}

void updatePins() {
for ( int i = 0; i < PIN_COUNT; i++ ) {
analogWrite(ledPins[i], states[i]);
}
delay(6);
}

void decay() {
for ( int i = 0; i < PIN_COUNT; i++ ) {
states[i] = (19*states[i]/20);
}
}

void Rotate(){
decay();
states[current_pin] = 255 * update_count / UPDATE_DURATION;
updatePins();

update_count++;
if ( update_count > UPDATE_DURATION ) {
update_count = 0;
current_pin += dir;
if ( current_pin == 0 ) {
dir = 1;
}
/* else if ( current_pin == (PIN_COUNT-1) ) {
dir = -1;
}*/

}
}

最佳答案

这应该可以解决问题

void setup()
{
int index;

for(index = 0; index <= 9; index++)
{
pinMode(ledPins[index],OUTPUT);
// ledPins[index] is replaced by the value in the array.
// For example, ledPins[0] is 2
}
pinMode(switchPin, INPUT); // Set the switch pin as input
buttonState = digitalRead(switchPin); // read the initial state
/* RGB */
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
/* RGB */
for ( int i = 0; i < PIN_COUNT; i++ ) {
pinMode(ledPins[i], OUTPUT);
states[i] = 0;
}
}

关于c++ - 开关中无法识别功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19018538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com