USB device drawing too much power (beginner ques.)

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

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

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

SNAP! We even used the same words and came to the same conclusion, only 3 seconds apart :wink:

Arduino Hive Mind?

--
The Quick Shield: breakout all 28 pins to quick-connect terminals

I put a photo of what I have here.. Thesis

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.

Thanks! Good news is I no longer get that USB power message. This is new photo Thesis 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);
}

}

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

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

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

Or even simpler (but inverted)

void loop() {
 digitalWrite(LED, digitalRead(BUTTON) 
}

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

And you even got the syntax right with two closing parens.

And you even got the syntax right with two closing parens.

but missed the semicolon off the end.

OK, I'll probably get this wrong as well but I'll give it another go :wink:

void loop() {
 digitalWrite(LED, !digitalRead(BUTTON));
}

Better?

Hmmm, here we are talking among ourselves and don't even know if the original poster has had any success.

@PeteAJ: are we helping, or just making you more confused?

Oops, looks like Jon and I both go to the back of the C 101 class :-[

Jon, Mike, Graynomad.. thanks for the input. I've tried your code versions and the code itself and the suggestions/mods make sense..

I'm new to this. Working on the second example in the Massimo's 'Getting Started with Arduino' book. Controlling an LED with a pushbutton.

I went back to my original setup with the second yellow and red wires flipped in relation to the resistor 'cause that got rid of my USB power warning error as per Jeremy's suggestion.

This is what I have: Thesis

Also went back to the original code in the book:

#define LED 13
#define BUTTON 7

int val = 0;

void setup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}

void loop() {
val = digitalRead(BUTTON);

if (val == HIGH) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}

Replugged the USB
changed by LED ..just in case
Compile, save, upload in Arduino, and my LED still doesn't turn on.

The 2nd orange light on the Arduino board (marked L) responds to the button press (which must be a good sign).

This is only my second thing but not giving up until I figure this out.

Does it look like I have the right 10K ohm? There are 1K and 10K ones in the MakerShed starter kit and I wasn't completely convinced I'm telling them apart.

If you are trying to do a:
press
led on
press
led off
(repeat)
Then I have some code that is tested and working. Just let me know and I'll post it...

LED connected backwards? Or blown?

If the on-board LED (L) is responding correctly to your button then the code is almost certainly fine.

Edit: and yeah, that looks like a 10k resistor to me. To be honest, I cheat most of the time and use a free Resisitor Color Code app on my phone. :wink: