0
Offline
Newbie
Karma: 0
Posts: 10
Arduino rocks
|
 |
« on: September 27, 2010, 10:54:10 pm » |
Hi! New to Arduino and psyched. However..I've been trying to do simple 'use a pushbutton to a LED' but I get this message when I press the button
Because a USB device was drawing too much power from your computer, one or more of your USB devices have been disabled. To prevent damaging your computer, the USB device drawing too much power has been disabled. Other devices may have also been disabled...
I'm using an iMac Mac OSX 10.6.4
I'd appreciate any advice. New to this..
Thanks
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 12
Posts: 2857
ruggedcircuits.com
|
 |
« Reply #1 on: September 27, 2010, 11:17:28 pm » |
It sounds like your pushbutton is connected to the wrong pins, perhaps between 5V and GND? Where did you connect your pushbutton? -- The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, light sensor, potentiometers, pushbuttons
|
|
|
|
|
Logged
|
|
|
|
|
Melbourne, Australia
Offline
Full Member
Karma: 0
Posts: 226
Cyborg-in-progress
|
 |
« Reply #2 on: September 27, 2010, 11:17:31 pm » |
Sounds like you've connected it so that the button shorts out the power supply! Could you post details of how you've connected everything together? A sketch or a photo would make it much easier for people to help you. -- Jon Practical Arduino: www.practicalarduino.com
|
|
|
|
|
Logged
|
|
|
|
|
Melbourne, Australia
Offline
Full Member
Karma: 0
Posts: 226
Cyborg-in-progress
|
 |
« Reply #3 on: September 27, 2010, 11:19:43 pm » |
SNAP! We even used the same words and came to the same conclusion, only 3 seconds apart ;-)
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 12
Posts: 2857
ruggedcircuits.com
|
 |
« Reply #4 on: September 27, 2010, 11:20:52 pm » |
Arduino Hive Mind? -- The Quick Shield: breakout all 28 pins to quick-connect terminals
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 10
Arduino rocks
|
 |
« Reply #5 on: September 27, 2010, 11:26:29 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 12
Posts: 2857
ruggedcircuits.com
|
 |
« Reply #6 on: September 27, 2010, 11:30:47 pm » |
Yup, definitely wrong wiring. Connect one side of the switch to GND. Connect the other side of the switch to pin 7 (red wire). Get rid of the resistor and other yellow wire (to 5V). In your sketch, enable the pullup on pin 7: void setup() { pinMode(7, INPUT); digitalWrite(7, HIGH); } Then, when the button is pushed, pin 7 will read LOW, else it will read HIGH. -- Need a custom shield? Let us design and build one for you.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 10
Arduino rocks
|
 |
« Reply #7 on: September 27, 2010, 11:57:19 pm » |
Thanks! Good news is I no longer get that USB power message. This is new photo http://datavisual.tumblr.com/#1202980891 and my code.. light still doesn't go on tho.. #define LED 13 #define BUTTON 7 int val = 0; int state = 0; void setup() { pinMode(7, INPUT); digitalWrite(7, HIGH); } void loop() { val = digitalRead(BUTTON); if (val ==HIGH) { state = 1 - state; } if (state == 1) { digitalWrite(LED, HIGH); } else { digitalWrite(LED,LOW); } }
|
|
|
|
|
Logged
|
|
|
|
|
Somewhere in Arizona
Offline
God Member
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
|
 |
« Reply #8 on: September 27, 2010, 11:58:11 pm » |
^^^^^^^^^^ Or you can leave the code as is, and swap the wires for ground and signal. ust move the right yellow wire over, and the red wire to thw contact on the pushbutton.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 10
Arduino rocks
|
 |
« Reply #9 on: September 28, 2010, 12:13:21 am » |
Thanks Jeremy. Liked your suggestion but still can't get the light to go with the wires switched as you said (and the original code). Tried restarting program and computer just for good measure as well. Guess I'm a little stumped here..
|
|
|
|
|
Logged
|
|
|
|
|
Melbourne, Australia
Offline
Full Member
Karma: 0
Posts: 226
Cyborg-in-progress
|
 |
« Reply #10 on: September 28, 2010, 12:27:25 am » |
I'm a bit confused about what you're trying to achieve. It *looks* like what you're wanting to do is have the LED change state on a button press, but what you've written won't achieve that. If you just want a simple "on while pressed" behavior, you could simplify your sketch to this (untested): #define LED 13 #define BUTTON 7
void setup() { pinMode(BUTTON, INPUT); digitalWrite(BUTTON, HIGH); pinMode(LED, OUTPUT); }
void loop() { if (digitalRead(BUTTON) == LOW) { digitalWrite(LED, HIGH); } else { digitalWrite(LED,LOW); } } It may seem like the logic is inverted, but what's happening is that the internal pull-up on the BUTTON pin will hold that input high unless the button is pressed. So a LOW value on the button corresponds to a press, which is then used to set a HIGH value (illuminated) for the LED. -- Jon
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6815
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #11 on: September 28, 2010, 01:32:15 am » |
Or even simpler (but inverted) void loop() { digitalWrite(LED, digitalRead(BUTTON) }
|
|
|
|
|
Logged
|
|
|
|
|
Melbourne, Australia
Offline
Full Member
Karma: 0
Posts: 226
Cyborg-in-progress
|
 |
« Reply #12 on: September 28, 2010, 02:02:53 am » |
Hey @Graynomad, spot on. Or to be just a little bit trickier: void loop() { digitalWrite(LED, !digitalRead(BUTTON)) } @PeteAJ: What Graynomad's suggestion does is set the LED output pin level to match the BUTTON input pin level. Very simple and clear. The "!" you see in my trivially modified version is a logical "NOT" operator, which reverses the logic so that instead of setting the LED output to the same level as the BUTTON input, it sets it to the *opposite* value. Since the button is "low" while pressed that'll give the effect of turning on the LED when the button is pressed, and off when released. -- Jon Edit: Added in the second closing bracket that was missing from the copied / pasted code
|
|
|
|
« Last Edit: September 28, 2010, 02:24:45 am by jonoxer »
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6815
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #13 on: September 28, 2010, 05:22:04 am » |
And you even got the syntax right with two closing parens.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 272
Posts: 25464
Solder is electric glue
|
 |
« Reply #14 on: September 28, 2010, 06:19:14 am » |
And you even got the syntax right with two closing parens. but missed the semicolon off the end.
|
|
|
|
|
Logged
|
|
|
|
|
|