Go Down

Topic: Multiple Pushbuttons with Toggle (Read 460 times) previous topic - next topic

MistrPibb

I coded a simple synth where

Pitch = sensor value * 2

But then I gave it a shot to make it more interesting.

I want to have 4 toggle-able pushbuttons which replace the "multiplier" with a variable multiplier.

This way, depending on what button was pressed last, the pitch is unique from the others.

I sort of left the coding at a cliffhanger.
Any help or advice at all really is appreciated
.


Code: [Select]
//declare variables:
const int b1Pin = 3;
const int b2Pin = 4;
const int b3Pin = 5;
const int b4Pin = 6;
int varPin = analogRead(A0);
int button1 = digitalRead(b1Pin);
int button2 = digitalRead(b2Pin);
int button3 = digitalRead(b3Pin);
int button4 = digitalRead(b4Pin);
int lastButtonPressed = 1;
int currentButton = 1;
int buttonState = 0;
int lastButtonState = 0;

void setup(){}  

void loop()
{

 //Button input changes variable currentButton
 if (button1 == HIGH)
 {currentButton == 1;}
 if (button2 == HIGH)
 {currentButton == 2;}
 if (button3 == HIGH)
 {currentButton == 3;}
 if (button4 == HIGH)
 {currentButton == 4;}
 if (currentButton != lastButtonPressed) //If new button isn't the same as last
 {lastButtonPressed = currentButton;}    //Change it to new value
 
 int varPitch = map(varPin, 100, 1000, 100, 5000) * lastButtonPressed;
 //Pitch will equal sensor value times the "button multiplier".
 tone(7, varPitch); //Play it.
}




dcb

I havent scanned the whole thing but this did jump out at me:

if (button1 == HIGH)
 {currentButton == 1;}

should be: (one equals sign for assignment)

if (button1 == HIGH)
 {currentButton = 1;}


You have a few of them like that.

MistrPibb

Yeah, I must've overlooked it.

Thanks for catching that.

Go Up