Pages: [1]   Go Down
Author Topic: 5 buttons on one pin.  (Read 663 times)
0 Members and 1 Guest are viewing this topic.
UK Norfolk
Offline Offline
Newbie
*
Karma: 3
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
}
Logged

There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2299
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mark
Logged

Offline Offline
Edison Member
*
Karma: 34
Posts: 1318
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 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...
« Last Edit: November 28, 2012, 06:33:40 pm by DVDdoug » Logged

UK Norfolk
Offline Offline
Newbie
*
Karma: 3
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


* 5 Button simplified circuit diagram.jpg (125.65 KB, 1060x631 - viewed 27 times.)
Logged

There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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



Lefty
« Last Edit: November 28, 2012, 06:59:47 pm by retrolefty » Logged

UK Norfolk
Offline Offline
Newbie
*
Karma: 3
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


UK Norfolk
Offline Offline
Newbie
*
Karma: 3
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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



* Button pin diagram.jpg (81.88 KB, 1060x631 - viewed 18 times.)
Logged

There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

UK Norfolk
Offline Offline
Newbie
*
Karma: 3
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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?
Logged

UK Norfolk
Offline Offline
Newbie
*
Karma: 3
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: