0

我创建了一个程序,每次单击后的按钮都会移动某些动作。我正在使用切换方法。除 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;
    }*/

  }
} 
4

1 回答 1

0

这应该解决它

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;
  }
}
于 2014-07-14T17:14:59.927 回答