Sunday, January 30, 2011

Project 6 - Modified Project #2

I just could NOT get the second modification working using the Project 5 (and sometimes Project 6) code... no matter how I modified it, I could not seem to find a way to tweak the code just a little bit to "bounce the ball." My best guess was that it would involve some sort of counter that would limit the value that CurrentLED would count up to each time (reducing it from 9... to 8... to 7...) but again... no luck with that.

So... I decided to simply hard code it to do what I wanted. I kept the array (see screenshot of my code) but got rid of much of the variables, including the CurrentLED variable, and went straight to a set of embedded FOR loops that would first loop 9 times (for nine LEDs - the ball bouncing up)... then 1... then 2... I created a variable called bounce that would simply decrement in the code every time the second loop (the ball dropping) finished. Probably not the most elegant solution, but it worked. You can download my code modification here.

I welcome my readers to share their code if they managed to perform the 2nd modification on page 58 using a different method... I'd love to see a few other programming techniques if anyone wants to share.

9 comments:

  1. What if you had a second array that contained [10, 9, 8, 7, etc...]? And that's all I'm gonna say.

    ReplyDelete
  2. I actually tried something like that (going on the idea that my mod #1 program also consisted of two arrays)... but even with two arrays, I couldn't figure out how to reduce the maximum size of an array using a loop or other technique.

    ReplyDelete
  3. How about using a Sine Wave to get a more realistic bounce? ;)

    ReplyDelete
  4. That's a good idea... if anyone tries it and cares to shoot some video, let me know and I'll be happy to share the results here.

    ReplyDelete
  5. I used another variable to keep track of the height that it should bounce to, decrementing it each time it reached that height and eventually setting it back to the max:


    byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
    int ledDelay;
    int direction = 1;
    int currentLed = 0;
    int maxHeight = 9;
    unsigned long changeTime;
    int potPin = 2;

    void changeLED() {
    for (int i = 0; i < 10; i++) {
    digitalWrite(ledPin[i], LOW);
    }
    digitalWrite(ledPin[currentLed], HIGH);

    currentLed += direction;

    if (currentLed >= maxHeight && direction > 0) {
    maxHeight--;
    direction = -1;

    if (maxHeight == 0) {
    maxHeight = 9;
    }
    }

    if (currentLed == 0) {
    direction = 1;
    }
    }

    void setup() {
    for (int i = 0; i < 10; i++) {
    pinMode(ledPin[i], OUTPUT);
    }
    changeTime = millis();
    }

    void loop() {
    ledDelay = analogRead(potPin);
    if ((millis() - changeTime) > ledDelay) {
    changeLED();
    changeTime = millis();
    }
    }

    ReplyDelete
  6. Hi,
    I did it this way. It was hard to figure out, took the hole evening, but I think I got a quite good looking code:

    // 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;
    int bounce = 9; // first place to change direction
    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();
    } //end of if-case
    } // end of void loop

    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 == bounce)
    {direction = -1;}

    if (currentLED == 0)
    {direction = 1;
    bounce -=1;}

    if (bounce == 0)
    {bounce = 9;}
    } //end of void changeLED

    ReplyDelete
  7. we've got another option:


    // Beginning Arduino
    // Arduino Básico
    // Project 6 - exercise 2
    // Projeto 6 - exercício 2

    byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Create array for LED pins
    int ledDelay(300); // delay between changes
    int currentLED = 0;
    int bounce = 9; // first place to change direction
    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();
    } //end of if-case
    } // end of void loop

    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. In the first loop the currentLED = 0

    if (currentLED == 0) // if the current led is 0
    {currentLED = bounce; bounce -=1;} // change current led to bounce and than sets bouce as bounce - 1
    else
    {currentLED = 0;}// if the current led is any number except 0, change the current led to 0
    if (bounce == 0) // if bounce is 0
    {bounce = 9;} // change bounce to 9 to close the cycle...

    }

    ReplyDelete
  8. This is the way I solved it:



    byte ledPin[] = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
    int ledDelay(65);
    int direction = 1;
    int currentLED = 0;
    int bounce = 9;
    unsigned long changeTime;

    void setup()
    {
    for (int x=0; x<10; x++) {
    pinMode(ledPin[x], OUTPUT); }
    changeTime = millis();
    }

    void loop()
    {
    if ((millis() - changeTime) > ledDelay) {
    changeLED();
    changeTime = millis();
    }
    }

    void changeLED() {
    for (int x=0; x<10; x++) {
    digitalWrite(ledPin[x], LOW);
    }
    if (currentLED <= bounce)
    { digitalWrite(ledPin[currentLED], HIGH);
    }

    // if at bottom, set direction up i.e. +1
    if ( currentLED == 0 ) {direction = 1;}

    // if at top, set direction down i.e. -1
    if (currentLED == bounce) {direction = -1;}

    // inc direction
    currentLED += direction;

    // if at bottom, reduce bounce height
    if ( currentLED == 0 ) {bounce-=1;}
    if (bounce==0) bounce=9;
    }

    ReplyDelete
  9. First, this blog is awesome.. I was stumped on both the project 6 exercises, having no previous programming at all..

    Second, I wanted to take this a step further by adding an increasing bounce once it hits the bottom. I have tried doing it with a 2nd array and function, but it seems like it just skips the function call and resets the bounce to 9..

    Any ideas? This is what I tried...

    // Project 6b - LED Chase Effect (Larson Scanner)
    byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //Create array for LED pins
    byte ledPin2[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //Create array for bounce increase
    int ledDelay; // delay between changes
    int direction = 1;
    int currentLED = 0;
    int bounce = 9; //max height
    int potPin = 2; //select the input pin for the potentiometer
    int bounce2 = 0; // max increase
    unsigned long changeTime;


    void setup() {
    for (int x=0; x<10; x++) { //set all pints to output
    pinMode(ledPin[x], OUTPUT); }
    changeTime = millis();
    }

    void loop() {
    ledDelay = analogRead(potPin); // read the value from the pot
    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 when we reach the ends
    if (currentLED == bounce) {direction = -1;}
    if (currentLED == 0) {direction = 1;}
    // bounce parameters
    if (currentLED == 0) {bounce -= 1;} // reduce bounce when at bottom
    if (bounce == 0) {
    bounce = 9; // bounce effect reset
    bounceUp; // function call to increase bounce
    }
    }

    void bounceUp() {
    for (int x=0; x<10; x++) { // turn off all LED's
    digitalWrite(ledPin2[x], LOW);
    }
    digitalWrite(ledPin2[currentLED], HIGH); // turn on the current LED
    currentLED += direction; // increment by the direction value

    //change direction when we reach the ends
    if (currentLED == bounce2) {direction = -1;}
    if (currentLED == 0) {direction = 1;}
    // bounce parameters
    if (currentLED == 0) {bounce2 += 1;} // reduce bounce when at bottom
    if (bounce2 == 9) {
    bounce2 = 0; // bounce up 0
    changeLED();
    }
    }

    ReplyDelete