使用两个按钮切换两个程序

时间:2016-09-26 08:05:48

标签: arduino arduino-uno

我正在尝试为我的房间rgb led strip制作一个led控制器。 我正在使用arduino uno。我想要两个按钮:

  1. 控制led的静态颜色(一次一种颜色。再次按下同一个开关时颜色为彩色)
  2. rgb crossfade
  3. 两个程序分开工作。现在我想在一个草图中使用这两个程序并通过两个按钮切换一个用于颜色循环,一个用于交叉淡化 草图有效,但只有一个按钮正在工作。 我根据各种指南和教程连接了两个10k欧姆电阻器和两个按钮。我认为接线是正确的。

    这是草图:

    const int buttonPin1 = 2;  // button 1
    const int buttonPin2 = 3;  // button 2
    const int redPin = 9;      // red led pin
    const int greenPin = 11;   // green led pin
    const int bluePin = 10;    // blue led pin
    int pushCounter = 0;       // push counter for changing colors at each press
    int buttonState = 0;       // button state i.e. HIGH or LOW
    int lastButtonState = 0;   
    
    void setup()
    {
        pinMode(buttonPin1, INPUT);
        pinMode(buttonPin2, INPUT);
        pinMode(redPin, OUTPUT);
        pinMode(greenPin, OUTPUT);
        pinMode(bluePin, OUTPUT);
        setColourRgb(0,0,0);
    } 
    
    void setColor(int red, int green, int blue)  // function for setting custom colors
    {
        //#ifdef COMMON_ANODE
        //red = 255 - red;
        //green = 255 - green;
        //blue = 255 - blue;
        //#endif
        analogWrite(redPin, red);
        analogWrite(greenPin, green);
        analogWrite(bluePin, blue);  
    }
    
    void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {  //function for setting crossfade colors
        analogWrite(redPin, red);
        analogWrite(greenPin, green);
        analogWrite(bluePin, blue);
    }
    
    void loop()
    {
        int buttonState1 = 0;
        int buttonState2 = 0;
        buttonState1 = digitalRead(buttonPin1);          //reads button states
        buttonState2 = digitalRead(buttonPin2);
    
        if ((buttonState1 == HIGH) && (buttonState2 == LOW))   //compares button states of both switches to enable the correct program
        {
            buttonState = digitalRead(buttonPin1);               
            if (buttonState != lastButtonState)
            {
                if (buttonState == HIGH)      //increases pushcounter of button1 on every press
                {
                    pushCounter++;
                }
            }
            //delay(1);
            lastButtonState = buttonState;
    
            if (pushCounter == 1)              //custom led colors
            { setColor(255, 0, 0);}; //red
            if (pushCounter == 2)
            { setColor(0, 255, 0);}; //green
            if (pushCounter == 3)
            { setColor(0, 0, 255);}; //blue
            if (pushCounter == 4)
            { setColor(255,200,255);}; //white
            if (pushCounter == 5)
            { setColor(255,255,0);}; //lime
            if (pushCounter == 6)
            { setColor(0,255,255);}; //aqua
            if (pushCounter == 7)
            { setColor(255,0,255);}; //violet
            if (pushCounter == 8)
            { setColor(128,0,128);}; //dim_violet
            if (pushCounter == 9)
            { pushCounter = 0;}
    
        }
    
        if ((buttonState1 == LOW) && (buttonState2 == HIGH))    //compares button states to load second program 
        {
            unsigned int rgbColour[3];
    
            // Start off with red.
            rgbColour[0] = 255;                                 //sets first color
            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]);
                    delay(30);
                }
            }
        };
    }
    

    现在如果我反转第二个程序的if条件......第二个程序运行但第一个程序没有运行。这有什么不对?

1 个答案:

答案 0 :(得分:0)

由于您正在阅读两个按钮,因此需要进行一些去抖动。 像你这样的东西,但更好一点:) 有一个库可以为您执行此操作,可以从Arduino web

下载

下载后将#include "WProgram.h"更改为#include "Arduino.h"中的Debounce.cpp,您应该好好去。

下面是使用您的代码实现lib。

从描述逻辑是正确的。

#include <Debounce.h>

#define buttonPin1  2       // button 1
#define buttonPin2  3       // button 2
#define redPin  9           // red led pin
#define greenPin 11         // green led pin
#define bluePin  10         // blue led pin
#define debounceTime 20     // debounce time

int pushCounter = 0;       // push counter for changing colors at each press
int buttonState = 0;       // button state i.e. HIGH or LOW
int lastButtonState = 0;   

// Instantiate a Debounce object 
Debounce button1 = Debounce(debounceTime, buttonPin1); //for button 1
Debounce button2 = Debounce(debounceTime, buttonPin2); //for button 2

void setup()
{
    pinMode(buttonPin1, INPUT);
    pinMode(buttonPin2, INPUT);
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);
    setColourRgb(0,0,0);
} 



void loop()
{
    //update debounce stat for both buttons
    button1.update(); 
    button2.update();

    int button1State = button1.read();          //reads button states
    int button2State = button2.read();

    if ((button1State == 1) && (button2State == 0))   //compares button states of both switches to enable the correct program
    {

        //Since we are checking for the first button being HIGH or 1 we can increament the counter
        pushCounter++;
        /*
        buttonState = button1State;               
        if (buttonState != lastButtonState)
        {
            if (buttonState == HIGH)      //increases pushcounter of button1 on every press
            {
                pushCounter++;
                lastButtonState = buttonState;
            }
        }
        */
        //delay(1);
        switch (pushCounter)
        {
            case 1:
                setColor(255, 0, 0); //red
                break;
            case 2:
                setColor(0, 255, 0); //green
                break;
            case 3:
                setColor(0, 0, 255); //blue
                break;
            case 4:
                setColor(255,200,255); //white
                break;
            case 5:
                setColor(255,255,0); //lime
                break;
            case 6:
                setColor(0,255,255); //aqua
                break;
            case 7:
                setColor(255, 0, 0); //violet
                break;
            case 8:
                setColor(128,0,128); //dim_violet
                break;
            case 9:
                pushCounter = 0; // reset the counter
                break;
        }

    }

    else if ((button1State == 0) && (button2State == 1))    //compares button states to load second program 
    {
        unsigned int rgbColour[3];

        // Start off with red.
        rgbColour[0] = 255;                                 //sets first color
        rgbColour[1] = 0;
        rgbColour[2] = 0;  

        // Choose the colours to increment and decrement.
        for (int decColour = 0; decColour < 3; decColour++)  
        {
            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]);
                delay(30);
            }
        }
    }
}


void setColor(int red, int green, int blue)  // function for setting custom colors
{
    //#ifdef COMMON_ANODE
    //red = 255 - red;
    //green = 255 - green;
    //blue = 255 - blue;
    //#endif
    analogWrite(redPin, red);
    analogWrite(greenPin, green);
    analogWrite(bluePin, blue);  
}

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {  //function for setting crossfade colors
    analogWrite(redPin, red);
    analogWrite(greenPin, green);
    analogWrite(bluePin, blue);
}