Go Down

Topic: USB device drawing too much power (beginner ques.) (Read 3 times) previous topic - next topic

Jonathan Oxer

OK, I'll probably get this wrong as well but I'll give it another go   ;-)

Code: [Select]
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?

Graynomad

Oops, looks like Jon and I both go to the back of the C 101 class   :-[
Rob Gray aka the GRAYnomad www.robgray.com

PeteAJ

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:  http://datavisual.tumblr.com/#1204687027

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.

 


Jeremy1998

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

Eight

#19
Sep 28, 2010, 09:03 pm Last Edit: Sep 28, 2010, 09:06 pm by Eight Reason: 1
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. ;)

PeteAJ

Hey Jeremy.. could I try your code? Not sure what else to try. Might rewire off the arduino.cc version of this task ..

PeteAJ

Hey.. just wanted to say thanks guys! Totally learnin'. And in business.  

Grumpy_Mike

If the on board LED is responding to the button press your code is OK.
The external LED must be connected through a resistor somewhere between 300R (that's ohms) and 1K. If you use 10K it will not light up bright enough for you to see.
You must use an external resistor, those books that say there is an on board resistor are out of date and wrong.

Jeremy1998

#23
Sep 30, 2010, 05:00 pm Last Edit: Sep 30, 2010, 05:01 pm by Jeremy1998 Reason: 1
This uses the internal pull up resistor. Just connect the button to ground and the other side to digital pin 12. You don't NEED an led for this, because the board has  one built on to it, but you can use one if you want to. It would go on digital pin 13.

This code SHOULD work. I tested it a long time ago, and I can't remember if I modified it or not...

Code: [Select]
int ledPin = 13;
int buttonPin = 12;

int currentState;
int previousState;

int counter;

void setup(){
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
}

void loop(){
currentState = digitalRead(buttonPin);
if(currentState == LOW && previousState == HIGH){
 counter ++;
 digitalWrite(ledPin, HIGH);
}  
else if(currentState == HIGH){
 digitalWrite(ledPin, LOW);
}  
if(counter >= 2){
 counter = 0;
}  
}

Go Up