Can we use two buttons; one for Volume+, one for Volume- ?
I couldn't find any example code for this function.
Yeah, sure, be my guest!
But uhm, no idea what volume and where you want to connect the buttons... Have a look here.
#define Buton1 12
#define Buton2 13
int defaultvolume=1000; //for example max volume value=2000, min vol value=100
void setup() {
pinMode(12, INPUT); // Button1 for volume +
pinMode(13, INPUT); // Button2 for volume -
if (digitalRead(Buton1)==1) {
defaultvolume=defaultvolume+100;
if(defaultvolume>1900) {
defaultvolume=1900;}
}
if (digitalRead(Buton2)==1) {
defaultvolume=defaultvolume-100;
if(defaultvolume<100) {
defaultvolume=100;}
}
}
I've tried this sample code but it didn't work.
it didn't work.
What were you expecting it to do ?
What did it actually do ?
How do you know what it did ?
setup() is invoked just once. loop() is executed repeatedly. you should put the code that monitors for button presses in loop().
it's common practice to connect switches between a digital input and ground with the input configured with an internal pull-up resistor the pulls the input HIGH (1). pressing the button grounds the input, making it LOW (0)
UKHeliBob:
What were you expecting it to do ?
What did it actually do ?
How do you know what it did ?
"volume" is a metophor. i just want to make the program change a "value" with +&- buttons.
I've tried previous code, the program has run but buttons didn't change antything.
OK, but how do you know what it did ?
How can you possibly know? And how are they connected?
Please spend a little bit more time reading how to get the best out of the forum and a little bit less slamming the keyboard like a monkey ![]()
meuzun:
I've tried this sample code but it didn't work.
Since it doesn't contain a loop() it doesn't even compile so you've certainly never tested it to see if it works or not.
Steve
This should do what you appear to want to do. Note that by giving useful names to constants the code is easier to read.
const byte UpButtonPin = 12;
const byte DownButtonPin = 13;
int Volume = 1000;
const int MaxVolume = 2000;
const int MinVolume = 100;
const int VolumeIncrement = 100;
void setup()
{
Serial.begin(115200);
pinMode(UpButtonPin, INPUT);
pinMode(DownButtonPin, INPUT);
}
void loop()
{
if (digitalRead(UpButtonPin) == HIGH)
{
Volume += VolumeIncrement;
if (Volume > MaxVolume)
{
Volume = MaxVolume;
}
Serial.println(Volume);
}
if (digitalRead(DownButtonPin) == HIGH)
{
Volume -= VolumeIncrement;
if (Volume < MinVolume)
{
Volume = MinVolume;
}
Serial.println(Volume);
}
}
Good one!
Do I get a prize if I predict the OP’s next question?
a7
johnwasser:
This should do what you appear to want to do. Note that by giving useful names to constants the code is easier to read.const byte UpButtonPin = 12;
const byte DownButtonPin = 13;
int Volume = 1000;
const int MaxVolume = 2000;
const int MinVolume = 100;
const int VolumeIncrement = 100;
void setup()
{
Serial.begin(115200);
pinMode(UpButtonPin, INPUT);
pinMode(DownButtonPin, INPUT);
}
void loop()
{
if (digitalRead(UpButtonPin) == HIGH)
{
Volume += VolumeIncrement;
if (Volume > MaxVolume)
{
Volume = MaxVolume;
}
Serial.println(Volume);
}
if (digitalRead(DownButtonPin) == HIGH)
{
Volume -= VolumeIncrement;
if (Volume < MinVolume)
{
Volume = MinVolume;
}
Serial.println(Volume);
}
}
Thank you John. This would be helpfull.
I won't ask moderator and the other guys any question. I would rather learn how to write code from scratch than ask you another question about it!!