Jump to content

ARDUINO: Use Two Pushbuttons To Toggle Two Programs

Go to solution Solved by clad,

Thanks a lot for your replies...actually one switch broke and that was the issue my code is working now and i used functions this tym its better with functions...

i am trying to make a led controller for my room rgb led strip

i am new to arduino and this is my first project 

i want two pushbuttons :

1. controls static colors of led ( one color at a time....cycles color on pressing that same switch again )

2. rgb crossfade

both programs work separately

Now i want these two programs in one sketch and toggled by two pushbuttons one for color cycle and one for crossfade

sketch works but only one pushbutton is working 

i have connected 2 10k ohm resistors with both buttons according to various guides and tutorials

i think wiring is correct

here is the sketch: 

 

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

  
}

Now if I reverse The if condition for second program...second program runs and first doesnt run 

what is wrong here?

 

Link to post
Share on other sites

Also since you already have the buttons on pins 2 and 3 you should use interrupts for the key presses as then you won't miss key presses and you won't need the if statement to select the program. Don't forget to add debouncing.

Link to post
Share on other sites

4 hours ago, r4tch3t said:

Also since you already have the buttons on pins 2 and 3 you should use interrupts for the key presses as then you won't miss key presses and you won't need the if statement to select the program. Don't forget to add debouncing.

some example code for interrupts??

Link to post
Share on other sites

3 hours ago, clad said:

some example code for interrupts??

Interrupts in Arduino C++

Interrupts are pretty important in general (they are basically how any modern computer is capable of doing anything, including decisions, context switches, DMA, subroutine calls, I/O functions, OS calls, and more...) so it's actually a really good idea to learn more about them in general.

Barring that, however, you could always use an SR latch on your buttons. This would allow you to have two inputs to the arduino board: Q and Qnot . What you could then do is write the two different LED modes as functions, and just have the main function be a loop that looks like this (this is just psuedo code): 

function which_pin();
	if q == HIGH; // Where q is the name of the pin that corresponds to switching colors
		return True;

	else;
		return False;

function switch_color();
		// Code for switching colors goes here

function cross_fade();
	// Function for crossfade goes here

function main();
	While True;
		if which_pin();
			switch_color()

			while which_pin() != False;
					// Do nothing here

		else;
			cross_fade();

			while which_pin() != True;
				// Do nothing here



 

ENCRYPTION IS NOT A CRIME

Link to post
Share on other sites

Functions are great.I had no end of problems with my earlier coding that were made so much simpler when functions are used.

A for interrupts the Arduino site has examples. I would suggest reading through all the basic tutorials on there, they help a lot.

One note on interrupts is that the hardware interrupts, pins 2,3 on the uno, work great but I have never been able to get the pin change interrupts working reliably on other pins.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×