Pin-mapping problem?

So I made the following sketch, it works fine on my Duemilanove hooked up to my MacBook's USB port, but when I remove the chip, put it on the breadboard, and wire everything up, I get nothing. Obviously, I'm not wiring it properly.

Here's how I have it wired:

  • Positive line from battery pack to pin 7 (VCC)
  • Negative line from battery pack to pin 8 (GND)
  • Positive lead from blue LED to pin 15 (digital pin 9)
  • Positive lead from red LED to pin 16 (digital pin 10)
  • Common negative from both LEDs to negative lead of battery pack
  • Switch wire to pin 4 (digital pin 2) with switch leads connected to +/- battery pack leads (Is this necessary? Does the switch need to be powered?)
    The battery pack in the photo is four AAAs, but in the final project it will be three AAs.

The sketch starts off with the blue 3-Watt LED on and the red 3-Watt LED off. When the button is pressed, blue goes off and red goes on. When pressed again, it goes into flashing mode, alternating between blue and red. When pressed again, it goes back to blue-on/red-off.

// 3-mode blue and red

const int ledRed = 10; // assign red LED to pin 10
const int ledBlue = 9; // assign blue LED to pin 9
const int buttonPin = 2; // assign tactile switch to pin 2

int button = 0; // current state of button
int previousButton = 0; // previous debounced state of button
unsigned long deBounceTimer = 0;
const unsigned long deBounceDelay = 6; // increase if you get multiple presses in one buttonpress.
int x = 0; // a number that tells us where we are in the cycle

void setup() // setting things up
{
pinMode(ledBlue, OUTPUT); // set blue LED pin to output
pinMode(ledRed, OUTPUT); // set red LED pin to output
pinMode(buttonPin,INPUT); // set button pin as input
}
void loop() // starting loop
{
button = digitalRead(buttonPin); // read current state of the button
// If button changed state to pressed and stayed there for > deBounceDelay milliseconds:
if (button == previousButton) deBounceTimer = millis();
if (!button && previousButton && (millis()-deBounceTimer) > deBounceDelay)
{
x++;
if (x>2)x=0; // variable x is reset to 0 and the cycle begins again
previousButton = button; // set previous state to current state which is debounced and assumed sure.
deBounceTimer = millis(); // reset debouncetimer
}
// If button changed state to not pressed and stayed there for > deBounceDelay milliseconds:
if (button && !previousButton && (millis()-deBounceTimer) > deBounceDelay)
{
previousButton = button; // set previous state to current state which is debounced and assumed sure.
deBounceTimer = millis(); // reset debouncetimer
}

if(x==0) { // first mode
digitalWrite(ledRed, LOW); // makes sure red is turned off
digitalWrite(ledBlue, HIGH); // turns on blue
}
if(x==1) { // second mode
digitalWrite(ledBlue, LOW); // turns off blue
digitalWrite(ledRed, HIGH);
} // turns on red
if(x==2) { // third mode, flashing
if (!button && previousButton && (millis()-deBounceTimer) > deBounceDelay)
{
x++;
if (x>2)x=0; // variable x is reset to 0 and the cycle begins again
previousButton = button; // set previous state to current state which is debounced and assumed sure.
deBounceTimer = millis(); // reset debouncetimer
}
// If button changed state to not pressed and stayed there for > deBounceDelay milliseconds:
if (button && !previousButton && (millis()-deBounceTimer) > deBounceDelay)
{
previousButton = button; // set previous state to current state which is debounced and assumed sure.
deBounceTimer = millis(); // reset debouncetimer
}
digitalWrite(ledRed, LOW); // turns off red
digitalWrite(ledBlue, HIGH); // turns on blue
delay(300); // waits 300 milliseconds
digitalWrite(ledBlue, LOW); // turns off blue
digitalWrite(ledRed, HIGH); // turns on red
delay(300); // waits 300 milliseconds
}
}



Thanks in advance for any help.

Um....

Have I been totally naive in thinking I could just pop the ATMEG chip out of the Arduino board and plug it into my project without adding all sorts of transistors and other mysterious entities? Because if I have, I'm in trouble. My understanding of electricity extends to Volts and Amperes and resistors and relays, but stops short of transistors and semiconductors and such. And putting the whole Duemilanove into the project is not an option. (I'm making a lightsaber for a customer, and there's not room in the hilt for much more than an ATMEG chip and perhaps a few much smaller items. :-/

Looks like you forgot the crystal and caps - so it will have to be Volts, Amps, resistors, relays, and crystals. The RESET line needs to be pulled up too. This should help . . .
http://art364.pbworks.com/Standalone+Arduino

Crystals! I knew there was some kind of new age witchcraft involved here. :wink: Many thanks, BroHogan. This looks like something even a n00b like me can work out.

But I don't understand what you mean by pulling up my RESET line. Is it drooping? :-?

I'm making a lightsaber for a customer, and there's not room in the hilt for much more than an ATMEG chip and perhaps a few much smaller items.

You could always use one of the small format Arduino clones like the om328p (Arduino Playground - HomePage). It is less than half the size of a 9v. battery, and I have found it completely compatible with the Arduino software.

Nemo,

Your reset line may or may not be drooping. But you want to be sure it doesn't. :slight_smile:
If you look at the diagram I linked, notice the wire from +5V to the RESET pin (#1). This is one way to stop a drooping reset. (If you add a reset button, you should pull up the reset with a 10K resistor instead of a wire.)

Glad your excited about the crystal. Unfortunately it's inside a little metal can. But it's there - keeping the beat.

You have other options on the crystal:

  • You can replace it with a resonator and skip the 2 caps.
  • You could do without it, but you'd have to re-burn the bootloader to a Lilypad.

Enjoy

Thank you, BroHogan. I went the non-pagan route and ordered a resonator.

I hadn't noticed the RESET line. You've saved me from an embarrassing faux pas. Do I need to make any changes to the sketch?

Another question. That diagram shows leads coming from the "rx" and "tx" pins. I'm guessing those don't stand for "pharmacy" and "Texas," but beyond that I have no idea what they are or if I need to worry about them in my project.

TBAr, thanks for the suggestion and link. But since this is going to be permanently installed and end up in someone else's hands, it seems a waste (both of functionality and money) to embed a whole Arduino board in this project.

"Rx" and "Tx" are serial "receive" and "transmit".

Ran

You should also notice that there are two VCC and two GND pins on the Arduino. While the other two have names like "AGND" and "AVCC" implying that they might only be needed if you're using the AD converter, they need to be connected anyway.

Thank you, Ran and westfw. So for my project, I don't need to worry about RX and TX? I'm planning to use an unregulated three-AA battery pack.

Also, the LED I'm using is a LedEngin LZ4-00MA10 (RGBA). It's red LED has a minimum forward voltage of 2.24V (maximum 3.2V), and its blue LED has a minimum forward voltage of 3.2V (maximum 4.16V). I want the current to the LEDs to be somewhere between 700 mA and 1000 mA. Will the ATMEG chip be able to supply that voltage and current to the LEDs?

I want the current to the LEDs to be somewhere between 700 mA and 1000 mA

That is going to mean a big heat sink on the LED.

Will the ATMEG chip be able to supply that voltage and current to the LEDs?

No way, its 40mA maximum out of an Arduino pin. You will have to use a constant current supply to power these LEDs.
For a discussion on high power LEDs see:- http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234273497

And if you use 3 AA batteries at 1A how long do you think they are going to last? My guess is in the region of seconds.

Thanks, Grumpy Mike. I've used these LEDs with similar battery setups (without Arduino), and they last quite a while. Yesterday I tried out a 5-Watt LedEngin LED with a 4aaa pack (1000mAh Ni-MHs), and it was still giving out plenty of light after an hour and a half. (And, yes, I always use heatsinks with high-power LEDs.)

I will read the thread you linked to. Am I right in thinking that having the Arduino chip control a relay, rather than power the LED directly, is a way to get around the mA limit?

Thanks again.

4aaa pack (1000mAh Ni-MHs)

OK that's altogether different. Maker sure you don't exceed 5.5V on the chip input.

Am I right in thinking that having the Arduino chip control a relay,

Well you could, but I would use an FET to switch it directly. I would not use a transistor at these currents as the saturation voltage of one would mean a big heat sink.

Ooo! Magic crystals! Or rather, magic resonators. It works!

Kind of.

The LEDs are much dimmer than they should be. I'm thinking I might be able to use a six-wire 1000mA or 700mA Buckpuck (with "CTL" and "REF" leads) to brighten the LEDs, but while I have the Buckpucks on hand, I've never actually tried to use the REF and CTL wires. Does anyone see anyway I can put a Buckpuck to good use here?

Once again, thanks in advance for any assistance (and thanks for the assistance you've given so far).

Congrats on getting it working!

We would need a pointer to the manual or specifications for the particular "buckpack" that you are using to answer your question about REF and CTL lines. It's not like a buckpack" is a standardized component...

A lot of LED driver chips have a low-current "enable" input of some kind, so that they can be easily controlled by (for example) microcontrollers. Whether that's sufficient is an open question. The "buckpack" that showed up first in Google has some weird characteristics (wanting "common" to be the minus side of LED output instead of the minus side of the battery, for instance.)

Relays or transistors are other possibilities for getting the arduino to drive high-current LEDs. A switching regulator (as I imagine would be present in the a "buckpack") with control inputs is likely to be a much more efficient (and more expensive) solution.

That's "Buckpuck," not "Buckpack." :wink:
The ones I'm talking about are LuxDrive, made by LEDdynamics. Specifically, I'm talking about the 0323-D-E-1000 (1000mA) and 0323-D-E-1000 (700mA). But after looking at the sample circuit diagram for a microcontroller, my head started spinning, and I'm now thinking of just trying out good old electromechanical relays. (I'm just not sure if the voltage coming from the ATMEG chip is enough to trigger a standard 5V relay.)

(I'm just not sure if the voltage coming from the ATMEG chip is enough to trigger a standard 5V relay.)

It's not you will need a transistor to drive the relay.

Anyway it's not the voltage it's the current capability.

See:-http://www.thebox.myzen.co.uk/Workshop/Motors_1.html for a relay circuit.

Thanks, Mike. Since this is an LED and not a motor, could I just wire it the way you show in "Basic Driving"? If so, it doesn't look as painful as I had imagined. What kind of transistor should I use? I've never used one before. :-/

Thanks, Mike. Since this is an LED and not a motor, could I just wire it the way you show in "Basic Driving"? If so, it doesn't look as painful as I had imagined. What kind of transistor should I use? I've never used one before. :-/

could I just wire it the way you show in "Basic Driving"?

Yes just replace the motor with your LED. Any sort of small signal NPN transistor would work. I use BC183 but there are many others, just see what your supplyer has in stock.