Jump to content

A little Arduino help from the experts (or at least far more than me!)


G+_Chris O'Riley
 Share

Recommended Posts

arduino.jpg

A little Arduino help from the experts (or at least far more than me!)

 

So my son asked for me to teach him electronics. I only know the very basics, but was always interested to learn more, so I bought an Arduino Nano, a breadboard and various odds and ends. I'm just experimenting with simple stuff for now, but am confused by this.

 

I started with a sketch to fade an LED on and off. I added a pot to control the speed of the fade. Then I added a second LED and made it so when one was fading on, the other was fading off.

 

I have it working perfectly... with a catch. The confusing part is that if I switch the LEDs to other pins and update the sketch accordingly, they blink on/off instead of fade. Some pins will fade, others blink. So if I change the LEDs to run off pins 4 and 2, they'll both blink. If I change them to 6 and 8, one will blink the other will fade. It's not an even/odd thing, and I can't find any explanation for this behavior.

 

Any ideas?

 

Thanks in advance.

 

Here's my sketch:

 

int sensorPin = A7; // select the input pin for the potentiometer

int sensorValue = 0; // variable to store the value coming from the sensor

int RedLedPin = 5; // LED connected to digital pin 5

int BlueLedPin = 3; // LED connected to digital pin 3

int BlueFadeValue = 255;

int RedFadeValue = 0;

 

void setup() {

// nothing happens in setup

}

 

void loop() {

sensorValue = analogRead(sensorPin);

sensorValue = map(sensorValue, 0, 1023, 5, 255);

 

 

for (int FadeValue = 0 ; FadeValue <= 255; FadeValue += sensorValue) {

BlueFadeValue -= sensorValue;

RedFadeValue += sensorValue;

if (BlueFadeValue < 0)

{

BlueFadeValue = 1;

}

if (RedFadeValue > 255)

{

RedFadeValue = 255;

}

analogWrite(RedLedPin, RedFadeValue);

analogWrite(BlueLedPin, BlueFadeValue);

delay(30);

}

 

 

for (int FadeValue = 255 ; FadeValue >= 0; FadeValue -= sensorValue) {

BlueFadeValue += sensorValue;

RedFadeValue -= sensorValue;

if (RedFadeValue < 0)

{

RedFadeValue = 1;

}

if (BlueFadeValue > 255)

{

BlueFadeValue = 255;

}

analogWrite(RedLedPin, RedFadeValue);

analogWrite(BlueLedPin, BlueFadeValue);

delay(30);

}

}

 

And here's my circuit:

Link to comment
Share on other sites

First, I was impressed with the clarity of your photo! Looks like you cheated and used a Canon EOS - well done!

 

Second, I think you're running into an issue of some pins being PWM and some not. This picture is from the UNO, but I believe the pins have essentially the same capability. Pins 2, 4, and 8 are not marked as PWM but pin 6 is. I haven't looked through your code yet, but I'm pretty sure this is the difference. I'd try other PWM pins (3, 5, 6, 9, 10, 11) to see if that matches up.

 

14453%20-%2068747470733a2f2f726177676974

Link to comment
Share on other sites

Thanks Ben, that was exactly it, just tested it and it works perfectly on any PWM pin, but blinks on non-PWM pins. I totally forgot that some pins are PWM and others aren't.

 

And thanks, yea, I took the photo with my 5D. I initially took it with my phone, but hate to post a bad photo when I can post a better one!

 

Thanks again!

Link to comment
Share on other sites

That's awesome, Chris O'Riley. Teach him whatever he is interested in. You'll make an engineer or tinkerer of him and he'll no doubt have a lifetime of fun with projects of his own. He may even make his living doing something which he loves (and YOU taught him).

 

When I was a kid, such a request would have my dad or my cousin tearing apart something electrical to use as a training aid. Next step was to build something else of it.

 

In the first grade we took a broken Realistic tape deck and turned it into a PA of sorts. I don't remember what we used for a microphone, but I didn't have to hold the mangled cassette deck to my face to use it.

 

I can still remember being challenged to draw a circuit when I was probably six or seven. I'd asked how the hallway light could be controlled by two switches. It was then that I learned of alternating current and of different types of switches.

 

I wouldn't trade anything for the lessons learned and memories made way back when. Dad thought it was neat that I was curious about all this stuff. Now he's curious how I can fix virtually anything he has.

Link to comment
Share on other sites

Thanks, yea, sounds a lot like my childhood. I was always taking things apart, and almost always putting them back together! There was the occasional bowel of left over part that were never really needed in the first place! Whenever any electronic device stops working, I still tear apart to see if A, the cause of the problem is obvious enough for me to fix, or B, if there's anything inside that I might be able to scavenge for one use or another! I have a collection of those MuMetal brackets from inside hard drives that I KNOW I'll find a use for some day!

 

I learned enough about electronics to follow basic circuits but not enough to make anything of much complexity myself. With Raspberry Pi's and Arduinos, it's so much easier being able to offload so much of the physical electronic complexity to programming, which I can pick up fairly well.

 

I bought one of the MakeBlock MBots for my son and he's really taken to programming it. Now it's time to move on to less scripted things, hence the little Arduino Nano. I always try to facilitate anything he shows an interest in, and I end up getting as much enjoyment out of it as he does. Even if he doesn't make a career out of any of it, it's always good to have had the experience.

 

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...