Hi,
I am a newbie who up to a few weeks ago was having a great time teaching myself to code in Arduino. I have been working through the various code examples in the various books and was having great success modifying the programs to teach myself what can be done with the various code.
I have been stuck on one particular exercise which must be really basic but after about 30 hours of trying absolutely everything I can think of I am just not getting it to work. Whilst I can move on to other exercises I just keep coming back to this exercise as it has me totally stumped and would really appreciate it if somebody could show me the code to make it work. I would learn a lot and it would help me reignite my enthusiasm for this wonderful product.
The code I am working is exercise 2, chapter e in Michael McRoberts excellent book - Beginning Arduino. I have to use the sample code to make a bouncing ball effect so that a led starts at the bottom and bounces up to the top 10th led then back to the bottom then only up to the 9th led then back down, then up to the 8th led, then back down and so on to simulate a bouncing ball losing momentum after each bounce.
Here is the original code which I must work with - I know I could achieve the same affect by using a sequence of digitalWrite commands for each individual led but the purpose is to achieve this by using the aray and modifying the code below
Many Many thanks in anticipation of your help
Buzbot
// Project 5 - LED Chase Effect
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Create array for LED pins
int ledDelay(65); // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
void setup() {
for (int x=0; x<10; x++) { // set all pins to output
pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
}
void loop() {
if ((millis() - changeTime) > ledDelay) { // if it has been ledDelay ms since last change
changeLED();
changeTime = millis();
}
}
void changeLED() {
for (int x=0; x<10; x++) { // turn off all LED's
digitalWrite(ledPin[x], LOW);
}
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
currentLED += direction; // increment by the direction value
// change direction if we reach the end
if (currentLED == 9) {direction = -1;}
if (currentLED == 0) {direction = 1;}
}
[code]
Hey now, don't lose the love. I always say that a certain amount of banging your head against the wall is a good thing, but there is a limit. Lots of folks here to help. Maybe next time, instead of 30 hours, 25 is enough XD
Never code more than enough. Brain will turn to self-defense mode before you know it... and you'll become blind to the obvious. Stop and move on. The next day it'll all be shiny clear
Believe me I have tried what you have suggested but it doesn't work. The leds go all over the place. I am so new at this I am obviously constructing the code wrong. When I think I have got it right I get compilling errors which can only be fixed by removing a curly brace, then the code doesn't execute what I have put in.
I am beginning to realise there must be some convention to encourage people to work things out for themselves but I have tried every possible combination and keep stuffing it up.
Can some kind hearted soul please show me how to do this.
Thanks for pointing this out. I posted the code from the book thinking it would be correct. It does work on the Arduino compiler and will work when downloaded to a UNO.
About 2 weeks ago I started using Simulator for Arduino to work out what I am doing wrong and they pointed out the error as well.
Even with the proper format with this code I still cant work it out. Have tried all sorts of combinations with the suggestion re currentLed == 8 mentioned earlier today but the code just jumps straight to that and misses the currentLed == 9. I am sure I have some structural problem trying to use curly braces in the loop and am missing something really basic.
Hence my asking for help to show how it can be done.
the suggestions so far are very helpful. They suggest making a few changes like this ( note I have not tested this!):
int ledDelay = 65; // NOT ledDelay(65) !
int upperLED = 9; // need this decrement this as you go, to create bounce
.....
....
if (currentLED == 9) {direction = -1; upperLED += -1;}
Whenever things don't work best thing is to break the code into logical pieces/concepts and get each concept working one by one.
For example, if it still doesn't try to make code without the bounce, i.e. simply a LED rolling up and down the full range. Once this works add bouncing
Don't lose the love! I've done some of it because LEDs are so cute, particularly on the Blinkenlight shield. This sort-of works:
// Project 5 - LED Chase Effect
const byte ledPin[] = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Create array for LED pins
const unsigned long ledDelay = 65; // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
const byte numberOfLeds = 10;
int topLED = numberOfLeds;
void setup() {
for (int x = 0; x < numberOfLeds; x++)
{ // set all pins to output
pinMode(ledPin[x], OUTPUT);
}
changeTime = millis();
}
void loop() {
if ((millis() - changeTime) > ledDelay)
{ // if it has been ledDelay ms since last change
changeLED();
changeTime = millis();
}
}
void changeLED() {
// turn off current one
digitalWrite (ledPin[currentLED], LOW);
// change direction if we reach the end
if (currentLED >= (topLED - 1))
direction = -1; // go down
if (currentLED == 0)
{
topLED--;
direction = 1; // go up
}
currentLED += direction; // increment by the direction value
// turn on new one
digitalWrite(ledPin[currentLED], HIGH);
}
The only problem is that once the light hits the bottom it just jiggles there. I'll leave it to you to work out a way of resetting back to (whatever) behaviour you want at that point.
int old = 9;
int ledPin[] = { 4,5,6,7,8,9,10,11,12,13 };
/*----------------------------------------------------------------------------*/
/* Turn off the old LED and turn on the next in the sequence - and delay */
/*----------------------------------------------------------------------------*/
void change(int cur)
{ if (cur != old)
{ digitalWrite(ledPin[old],LOW);
digitalWrite(ledPin[cur],HIGH);
old = cur;
delay(65);
}
}
/*----------------------------------------------------------------------------*/
/* Do a "bounce" to the current height */
/*----------------------------------------------------------------------------*/
int runLed(int top)
{ int i;
for (i = 0; i <= top; i++) change(i);
for (i = 0; i <= top; i++) change(top - i);
}
/*----------------------------------------------------------------------------*/
/* setup() - Make all LED pins outputs and turn off the LEDs */
/*----------------------------------------------------------------------------*/
void setup(void)
{ int i;
for (i = 0; i < 10; i++)
{ pinMode(ledPin[i],OUTPUT);
digitalWrite(ledPin[i],LOW);
}
}
/*----------------------------------------------------------------------------*/
/* loop() - Ramp bounce height up (optional) and down again */
/*----------------------------------------------------------------------------*/
void loop(void)
{ static int i;
for (i = 0; i < 10; i++) runLed(i); /* Optional */
for (i = 0; i < 10; i++) runLed(9-i);
}
Thanks very much for helping out with this. The code works perfectly on my UNO. I look forward to running it on the simulator and learning how it works.
Thanks Also to Nick Gammon, That code also works perfectly.
Unfortunately I couldn't quite get the code snippet to work sent by Ninja2 but I do appreciate all the support.
I am now going to experiment with both sets of code to see what I can learn.
Buzbot:
When I think I have got it right I get compilling errors which can only be fixed by removing a curly brace, then the code doesn't execute what I have put in.
You are using a coding style which some people like because it is compact, but I dislike it intensely because it encourages exactly the type of bug you're fighting with.
I suggest you adopt the following conventions:
Always follow 'if', 'for', 'do', 'while' etc clause with a curly bracket '{'. In other words, each separate path through your code should be a compound statement not a simple statement.
Each '{' should be on a new line with nothing else on that line.
Each '{' should be matched by a '}' on a line on its own indented to the same position as the '{' that it matches.
The lines between '{' and '}' should be indented by a consistent amount. I suggest four spaces, but you will find people using anything from one to eight. Whatever you use, be consistent.
When you code an 'if' statement, consider whether you should include an 'else' clause. While you're learning, I suggest you put in an 'else' clause every time; if nothing needs to be done then stick a comment in between the '{' '}' noting why nothing needs to be done. Once you're more used to designing code you can eliminate these empty blocks, but in the early days it will force you to think about the logical design of your code and especially the control structure.
Sometimes, even using correct indentation it is not always easy to know what block of code each '}' is ending. In that case you may find it helpful to put a comment after the '}' reminding you what it is the end of.