Go Down

Topic: We've figured it out. Problem solved!! (Read 5794 times) previous topic - next topic

CrossRoads

You are pushing& holding the button? The LED will only turn on while the button is pushed as coded now.
If you want them to stay on for half a second or something, you can you do something like add a small delay, or write different code to have the LED turn on with one press, and off on the next.

The code is straightforward enough as is that they should turn on now while you hold the button down.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AKSoapy29

Here's the code with the add ins:

Code: [Select]
/*
 Button

Turns on and off a light emitting diode(LED) connected to digital  
pin 13, when pressing a pushbutton attached to pin 2.


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave <http://www.0j0.org>
modified 28 Oct 2010
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2;     // the number of the pushbutton pin
const int ledPin1 =  8;      // the number of the LED pin
const int buttonPin2 = 3;     // the number of the pushbutton pin
const int ledPin2 =  9;      // the number of the LED pin
const int buttonPin3 = 4;     // the number of the pushbutton pin
const int ledPin3 =  10;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;         // variable for reading the pushbutton status
int buttonState3 = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin1, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin1, INPUT);    
  // initialize the LED pin as an output:
 pinMode(ledPin2, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin2, INPUT);
  // initialize the LED pin as an output:
 pinMode(ledPin3, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin3, INPUT);  
 digitalWrite(buttonPin1, HIGH);  //enable internal pullup resister
 digitalWrite(buttonPin2, HIGH);  //enable internal pullup resister
 digitalWrite(buttonPin3, HIGH);  //enable internal pullup resister
}

void loop(){
 // read the state of the pushbutton value:
 buttonState1 = digitalRead(buttonPin1);
   // read the state of the pushbutton value:
 buttonState2 = digitalRead(buttonPin2);
   // read the state of the pushbutton value:
 buttonState3 = digitalRead(buttonPin3);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState1 == LOW) {    
   // turn LED on:    
   digitalWrite(ledPin1, HIGH);  
 }
 else {
   // turn LED off:
   digitalWrite(ledPin1, LOW);
 }
   if (buttonState2 == LOW) {    
   // turn LED on:    
   digitalWrite(ledPin2, HIGH);  
 }
 else {
   // turn LED off:
   digitalWrite(ledPin2, LOW);
 }
   if (buttonState3 == LOW) {    
   // turn LED on:    
   digitalWrite(ledPin3, HIGH);  
 }
 else {
   // turn LED off:
   digitalWrite(ledPin3, LOW);
 }
}
Andrew K.

AKSoapy29

I cleaned it up a bit. Here:

Code: [Select]
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2;     // the number of the pushbutton pin
const int ledPin1 =  8;      // the number of the LED pin
const int buttonPin2 = 3;     // the number of the pushbutton pin
const int ledPin2 =  9;      // the number of the LED pin
const int buttonPin3 = 4;     // the number of the pushbutton pin
const int ledPin3 =  10;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;         // variable for reading the pushbutton status
int buttonState3 = 0;         // variable for reading the pushbutton status

void setup() {
 pinMode(ledPin1, OUTPUT);
 pinMode(buttonPin1, INPUT);
 pinMode(ledPin2, OUTPUT);
 pinMode(buttonPin2, INPUT);
 pinMode(ledPin3, OUTPUT);
 pinMode(buttonPin3, INPUT);
 digitalWrite(buttonPin1, HIGH);  //enable internal pullup resister
 digitalWrite(buttonPin2, HIGH);  //enable internal pullup resister
 digitalWrite(buttonPin3, HIGH);  //enable internal pullup resister
}

void loop(){
 buttonState1 = digitalRead(buttonPin1);
 buttonState2 = digitalRead(buttonPin2);
 buttonState3 = digitalRead(buttonPin3);
 if (buttonState1 == LOW) {
   digitalWrite(ledPin1, HIGH);
 }
 else {
   digitalWrite(ledPin1, LOW);
 }
 if (buttonState2 == LOW) {
   digitalWrite(ledPin2, HIGH);
 }
 else {
   digitalWrite(ledPin2, LOW);
 }
 if (buttonState3 == LOW) {
   digitalWrite(ledPin3, HIGH);
 }
 else {
   digitalWrite(ledPin3, LOW);
 }
}

Andrew K.

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AKSoapy29

#34
Dec 26, 2010, 11:42 pm Last Edit: Dec 26, 2010, 11:43 pm by Soapy29 Reason: 1
No. That's the sad part. We still need to reverse the voltage going to the led.  :-/

Moving...
Andrew K.

CrossRoads

What are the resister values connected to pins D8, D9, D10?

Do you have a multimeter that you can take voltage measurements with?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AKSoapy29

#36
Dec 27, 2010, 12:07 am Last Edit: Dec 27, 2010, 12:10 am by Soapy29 Reason: 1
Their all a orange-red, orange-red, brown, then gold. I have a multimeter, but I really only used it for voltage and to see if switches connect. I don't even know how  to use it for resisters. But these resisters work.
Andrew K.

CrossRoads

Ok, that sounds like 330 ohm resisters, thats good.
What I would like to suggest is to make some voltage measurements at D8, D9, D10 while you press & hold the corresonding button down.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AKSoapy29

Ok. If I'm right, one of the settings on my meter said 325.
Andrew K.

CrossRoads

Yep, 330 +/- 5% or 10% percent, I'm not sure which Gold represents.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AKSoapy29

5V come out of each. I know I have to switch the voltage around. I just pulled 2 of the 4 pins, and I switched them around, and the led lighted up when I pressed the button!
Andrew K.

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AKSoapy29

No! It still doesn't work for all of them!Is there some way I can switch the value of the pins so there 5V?
Andrew K.

CrossRoads

The basics are all done - you need to check the wiring to the switchess, use your meter on resistance setting and see which pins are actually connected together when pressed and get the wires to the right places.
If you want, leave one end of a wire on D2 (or D3 or D4) and touch the other end to ground, convince  yourself the LED & code works, then work on getting the switch wiring correct.
Sounds like you are close.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AKSoapy29

It has nothing todo with the switches. They work the right way. It's the output of the pins 8 9 and 10. They, when the button is pressed, should be Grounded. Right now they are 5V, and that won't work for my led.
Andrew K.

Go Up