this is my first post here. For some time I have been struggling with a problem and would like to know if anyone has a solution to share.
I am building a 4 tone sequencer, where I use 4 potentiometers to change the pitch of each tone beat. I want to add more beats to the sequencer so I would like to have an elegant solution instead of keep adding potentiometers. My idea was to use 1 potentiometer to control the pitch and use multiple push buttons with the single potentiometer for this process. All in all it goes like this: - When switch1=HIGH, pitch1=analogRead(A0); - When switch2=HIGH, pitch2=analogRead(A0); The problem is that when I change pitch1 and pitch2 the end up getting the same values as soon as I press their switch. My code looks like this:
if(switch1state==HIGH)
{
Tone1Pot=analogRead(A0); // 0 – 1023
pitch1=Tone1Pot/13;
}
tone(speakerPin, notes[pitch1], 200);
Serial.print(pitch1);
Serial.print(" ");
delay(tempo);
if(switch2state==HIGH)
{
Tone2Pot=analogRead(A0); // 0 – 1023
pitch2=Tone2Pot/13;
}
tone(speakerPin, notes[pitch2], 200);
Serial.print(pitch2);
Serial.print(" ");
delay(tempo);
I would really like to store that analog reading value when I press switch1 and keep it until switch1 goes HIGH again.
EDIT:
Let me try to explain the issue better: I have 2 push buttons and 1 potentiometer, all of them are inputs. The code I am aiming for is to do the following: 1 - While pressing push button1 I want to read the value of potentiometer and store it to a variable1. When the button1 is not pressed I want to keep that variable stored. 2 - While pressing push button2 I want to read the value of potentiometer and store it to a variable2. When the button2 is not pressed I want to keep that variable stored. I have successfully achieved this, however the problem comes when I press button1 again to adjust variable1, variable2 is also getting adjusted in the process, even when I am not pressing button2 Chris - The push buttons are correct since I can change the states of the digital pins when I press them ( and I measure the 5V when pressed). I do not need the buttons to be pressed at the same time. What I need is the buttons to trigger the event of storing potentiometer data only when either one of them is pressed accordingly. jsotola - Sorry for the bad explanation. I hope this one is better. Code:
#include "pitches.h"
int notes[]={
NOTE_B1,
NOTE_C2,
NOTE_CS2,
NOTE_D2,
NOTE_DS2,
NOTE_E2,
NOTE_F2,
NOTE_FS2,
NOTE_G2,
NOTE_GS2,
NOTE_A2,
NOTE_AS2,
NOTE_B2,
NOTE_C3,
NOTE_CS3,
NOTE_D3,
NOTE_DS3,
NOTE_E3,
NOTE_F3,
NOTE_FS3,
NOTE_G3,
NOTE_GS3,
NOTE_A3,
NOTE_AS3,
NOTE_B3,
NOTE_C4,
NOTE_CS4,
NOTE_D4,
NOTE_DS4,
NOTE_E4,
NOTE_F4,
NOTE_FS4,
NOTE_G4,
NOTE_GS4,
NOTE_A4,
NOTE_AS4,
NOTE_B4,
NOTE_C5,
NOTE_CS5,
NOTE_D5,
NOTE_DS5,
NOTE_E5,
NOTE_F5,
NOTE_FS5,
NOTE_G5,
NOTE_GS5,
NOTE_A5,
NOTE_AS5,
NOTE_B5,
NOTE_C6,
NOTE_CS6,
NOTE_D6,
NOTE_DS6,
NOTE_E6,
NOTE_F6,
NOTE_FS6,
NOTE_G6,
NOTE_GS6,
NOTE_A6,
NOTE_AS6,
NOTE_B6,
NOTE_C7,
NOTE_CS7,
NOTE_D7,
NOTE_DS7,
NOTE_E7,
NOTE_F7,
NOTE_FS7,
NOTE_G7,
NOTE_GS7,
NOTE_A7,
NOTE_AS7,
NOTE_B7,
NOTE_C8,
NOTE_CS8,
NOTE_D8,
NOTE_DS8
};
int Tone = 0;
int speakerPin = 3;
int switch1=6;
int switch2=7;
int switch3=8;
int switch4=9;
int switch5=10;
int switch6=11;
int switch7=12;
int switch8=13;
void setup() {
Serial.begin(9600);
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(switch3, INPUT);
pinMode(switch4, INPUT);
pinMode(switch5, INPUT);
pinMode(switch6, INPUT);
pinMode(switch7, INPUT);
pinMode(switch8, INPUT);
}
void loop() {
// get potentiometers inputs
//int TonePot = analogRead(A0); // 0 – 1023
//int DurationPot = analogRead(A1); // 0 – 1023
int TempoPot = analogRead(A2); // 0 – 1023
int Tone1Pot;
int Tone2Pot;
int Tone3Pot;
int Tone4Pot;
int Tone5Pot;
int Tone6Pot;
int Tone7Pot;
int Tone8Pot;
int Duration1Pot;
int Duration2Pot;
int Duration3Pot;
int Duration4Pot;
int Duration5Pot;
int Duration6Pot;
int Duration7Pot;
int Duration8Pot;
// calculate corresponding, divide by 13 as we have 77 pitches. 1023/77 ~ 13
int pitch1;
int pitch2;
int pitch3;
int pitch4;
int pitch5;
int pitch6;
int pitch7;
int pitch8;
int pitch1stored;
int pitch2stored;
int noteduration1 = Duration1Pot/8;
int noteduration2 = Duration2Pot/8;
int noteduration3 = Duration3Pot/8;
int noteduration4 = Duration4Pot/8;
int noteduration5 = Duration5Pot/8;
int noteduration6 = Duration6Pot/8;
int noteduration7 = Duration7Pot/8;
int noteduration8 = Duration8Pot/8;
int tempo = TempoPot;
//Get state of switches if HIGH or LOW
int switch1state=digitalRead(switch1);
int switch2state=digitalRead(switch2);
int switch3state=digitalRead(switch3);
int switch4state=digitalRead(switch4);
int switch5state=digitalRead(switch5);
int switch6state=digitalRead(switch6);
int switch7state=digitalRead(switch7);
int switch8state=digitalRead(switch8);
////////////////////////////////////////Sound generation code /////////////////////////////////
if(switch1state==HIGH)
{
Tone1Pot=analogRead(A0); // 0 – 1023
pitch1=Tone1Pot/13;
}
tone(speakerPin, notes[pitch1], 200);
//tone(speakerPin, 0, 200);}// make the tone mute
Serial.print("pitch1= ");
Serial.print(pitch1);
Serial.print(" ");
delay(tempo);
if(switch2state==HIGH)
{
Tone2Pot=analogRead(A0); // 0 – 1023
pitch2=Tone2Pot/13;
}
tone(speakerPin, notes[pitch2], 200);
Serial.print("pitch2= ");
Serial.print(pitch2);
Serial.print(" ");
delay(tempo);
if(switch3state==HIGH)
{
Tone3Pot=analogRead(A0); // 0 – 1023
pitch3=Tone3Pot/13;
}
tone(speakerPin, notes[pitch3], 200);
Serial.print("pitch3= ");
Serial.print(pitch3);
Serial.print(" ");
delay(tempo);
}
switch1=HIGH
store that analog reading value when I press switch1 and keep it until switch1 goes HIGH again
... what does that mean? ... what doesswitch1 goes HIGH again
actually mean? ... i think that your main problem is that you do not actually know how you want the device to work