Pages: 1 2 [3] 4 5   Go Down
Author Topic: We've figured it out. Problem solved!!  (Read 5355 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26958
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Minnesota!
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
My nickname on the forum used to be Soapy29.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's the code with the add ins:

Code:
/*
  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);
  }
}
Logged

Andrew K.

Minnesota!
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
My nickname on the forum used to be Soapy29.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I cleaned it up a bit. Here:

Code:
// 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);
  }
}

Logged

Andrew K.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26958
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And it works now?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Minnesota!
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
My nickname on the forum used to be Soapy29.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No. That's the sad part. We still need to reverse the voltage going to the led.  :-/

Moving...
« Last Edit: December 26, 2010, 05:43:22 pm by Soapy29 » Logged

Andrew K.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26958
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Do you have a multimeter that you can take voltage measurements with?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Minnesota!
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
My nickname on the forum used to be Soapy29.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 26, 2010, 06:10:11 pm by Soapy29 » Logged

Andrew K.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26958
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Minnesota!
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
My nickname on the forum used to be Soapy29.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Andrew K.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26958
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, 330 +/- 5% or 10% percent, I'm not sure which Gold represents.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Minnesota!
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
My nickname on the forum used to be Soapy29.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Andrew K.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26958
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Woo hoo! Success!
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Minnesota!
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
My nickname on the forum used to be Soapy29.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Andrew K.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26958
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Minnesota!
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
My nickname on the forum used to be Soapy29.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Andrew K.

Pages: 1 2 [3] 4 5   Go Up
Jump to: