5 buttons on one pin.

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
Jezza

/*
Super Capacitor charger .
I WILL WANT TO ADD LEDS TO MONITOR CHARGE STATE LATER.
*/
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);

Serial.begin(9600);
}

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
}

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.

Lefty

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

Mark

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...

P.S.
Some [u]Boolean and/or logic[/u] 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.
Regards
Jezza

Not sure that 'charging' circuit for the caps is going to work. It's acting like a 'high side switch' so must be a PNP transistor and as such I don't think the arduino output pin will be able to correctly turn on and off the transistor. But maybe I'm all wet and just miss seeing a real schematic drawing? :smiley:

Also I don't see a common ground connection from the 12 supply and the arduino, which I'm sure is a requirement.

Lefty

Hi Lefty there is a common ground between the Arduino and the power supply, But I forgot to show that. I just showed a very basic circuit. I have had the circuit running on a running on a breadboard with a 5v regulator limiting the voltage and a simple on off switch. You are right it is a PNP transistor Sorry my drawing was a bit vague. The other transistor is an NPN. The NPN transistor will conduct and pull its collector towards ground. The base of the PNP transistor is pulled down, and the PNP transistor is switched on. I an sorry I was worried about the code not the drawing.
Jezza

You are missing those wires that are important to show. Those are wires with resistors. Where are they?

The resistors are connected to the buttons in a daisy chain see picture.
Can I ask if I can use 5 pins on the Arduino to go to the same transistor or would that cause problems. I have no trouble controlling 5 separate pins with the buttons like this but would like to know if they all connected to the same output and say 2 buttons were pressed at the same time.

if (val >850 && val < 865 ) digitalWrite (Pin8, HIGH); // turn on test led 1
if (val >890 && val < 910 ) digitalWrite (Pin9, HIGH); // turn on test led 2
if (val >930 && val < 940 ) digitalWrite (Pin10, HIGH); // turn on test led 3
if (val >970 && val < 980 ) digitalWrite (Pin11, HIGH); // turn on test led 4
if (val >1015 && val < 1030)digitalWrite (Pin12, HIGH); // turn on test led 5
Jeremy

I have just had an Idea. Could I make it using one button with a time button is pressed for to set the value the capacitors are charged to? Also have an led blink while the butoon is pressed ie 1 secong 1 flash of led and charge to (x). two secconds two flashes of led and charge to (y). ETC
If this is possible how would i write the delay in button pressed time?
Thank you all for your input so far.
Jezza

jezza103:
The resistors are connected to the buttons in a daisy chain see picture.
Can I ask if I can use 5 pins on the Arduino to go to the same transistor or would that cause problems. I have no trouble controlling 5 separate pins with the buttons like this but would like to know if they all connected to the same output and say 2 buttons were pressed at the same time.

if (val >850 && val < 865 ) digitalWrite (Pin8, HIGH); // turn on test led 1
if (val >890 && val < 910 ) digitalWrite (Pin9, HIGH); // turn on test led 2
if (val >930 && val < 940 ) digitalWrite (Pin10, HIGH); // turn on test led 3
if (val >970 && val < 980 ) digitalWrite (Pin11, HIGH); // turn on test led 4
if (val >1015 && val < 1030)digitalWrite (Pin12, HIGH); // turn on test led 5
Jeremy

Shouldn't that 68k resistor be 10k to protect the pin?

Hi I might be getting somewhere. I think I have to use time for the program to work. One button and length of time it was pressed for. I can get 5 leds to light one after the other depending on how long I press for. now I need to work out how to get the different charge limits into the sketch, and that is where I am falling down. But I am getting somewhere. Not bad for a 47 year old who had never tried out anything to do with electronics 18 months ago.
Now Programming is another matter. They say "you can't teach an old dog new tricks" Well that may be so but with a little advice I might just learn a thing or two.
Regards to you all
Jezza :slight_smile:

The "new trick" is to realize button push is not a state (such as value between 100 and 200). Rather button push is a change of state (such as from 0 to between 100 and 200) within some reasonable time frame (20 ms so to debounce random contact from bouncing around). If you want true button actions in your program, you need to set up a variable to track state of the input and detect change of state.