Go Down

Topic: 5 buttons on one pin. (Read 760 times) previous topic - next topic


Hi I am fairly new to Arduino and have written a few simple sketches but I have hit a problem and cannot find the answer. My sketch a 5 Button sketch all connected to 1 pin. It is only in the beginning stages and I would like to control the output with them. I have wired the buttons together with a common earth and a 3.3 k resistor between each of them and set up a voltage divider resistor to 5v with the reading going to analog Pin 0. That part works fine. The problem is with another voltage divider  set on analog Pin 1 reads the charge level of my Super Capacitors, which I want to be able to charge to different voltages. When the voltage reaches the value set by button 1 the circuit goes off as it should. but when I press button 2 which has a higher threshold set to it the circuit switches off at the button 1 threshold. I will add the rest of the sketch when i have overcome my problem.
Here is my sketch
Thank you

Super Capacitor charger .
int resetPin =1; //Voltage over voltage devider
int sensePin =0; //check which button was pressed

int led = 13; // led for testing
void setup(){
analogReference (DEFAULT);

pinMode (resetPin, INPUT);
pinMode (led, OUTPUT);


void loop() {
  int val = analogRead(sensePin);// read value for which button pressed
int reading = analogRead(resetPin); //read voltage value
  if(val >1015)  digitalWrite (led, HIGH);// led pin on
  if(reading >200)  digitalWrite (led, LOW);// led pin off

  if(val >970)  digitalWrite (led, HIGH);
  if(reading >300)  digitalWrite (led, LOW);// HERE I GET THE PROBLEM !!!!



  Serial.println(analogRead(sensePin));//print value to serial monitor
   Serial.println(analogRead(resetPin));//print value to serial monitor
  delay(500);// time between printing to monitor
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0


A schematic drawing of what you built would go a long way in trying to help you. Even if it's just hand drawn and a picture taken and posted would be better then just words. A schematic is the universal language of circuitry.

In your code, while it may possibly compile without errors ( I didn't try it) your use of many of your { and } are not proper at all.



Formatting your code before posting would help you and it help us help you as do code tags. The # button when posting.



Nov 29, 2012, 12:02 am Last Edit: Nov 29, 2012, 12:33 am by DVDdoug Reason: 1
I think your four if-statements in a row are a problem....

All of these if-statements are controlling the same LED.    So no matter what happens with the 1st three statements, the LED might flash on or off so fast you can't see it, and then the last LED state (if > 300) will hold for your delay.

It doesn't look like your curly brackets are doing anything...  You might need nested if-statements and/or if-else statements.

It will probably help your troubleshooting if you add some more delays and more serial outs so you can see what it's doing along the way...

Some Boolean and/or logic might help in your application too:

(This isn't code, it's just to show the logic)
(if X > 100 && X < 200)   do A   // If X is between 101 and 199...
(if X > 199 && X < 300)   do B  // if X is between 200 and 299...
(if X > 299 && X < 400)   do A  // If X is between 300 and 399...


Hi Lefty. Here is a simple picture for you. I forgot to show the resistor between the Arduino and the base of the transistor. I hope this comes across ok I have not uploaded a picture on here before.
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

Go Up