Unknown problem with sketch

lennevia

  • Administrator
  • Hero Member
  • *****
    • Posts: 437
    • View Profile
Can you verify you are using the exact Arduino example for pin 9 that I included above?

The program you included before differed from the Arduino example so I am unable to help you further without knowing what complete code you are using.

To clarify, the loop() in the Arduino program loops over and over on the TinyDuino, so the first line of the code snippet you included " int brightness = 0; " initializes and sets the brightness variable to zero on every loop iteration. This is likely the problem. The brightness variable, as shown in the Arduino example I included, needs to be a global variable outside of the loop() function in order to be usable between loop iterations.


TheQuestor

  • Full Member
  • ***
    • Posts: 15
    • View Profile
**CRASH!!*  (This is the sound of me banging my head on the desk).

DERP.

OMG.

I can't believe I did that.  Forty years of programming, and I made a rookie mistake like that.  Boy, do I feel dumb.  Thanks for enlightening me.  Now it works perfectly.


TheQuestor

  • Full Member
  • ***
    • Posts: 15
    • View Profile
Here's my entire code:

Code: [Select]
//Pin Number Assignments:
const int LED01 = 1;
const int LED02 = 2;
const int LED03 = 3;
const int LED04 = 4;
const int LED05 = 5;
const int LED06 = 6;
const int LED07 = 7;
const int LED08 = 8;
const int LED09 = 9;
int brightness1 = 0;    // how bright the LED is
int fadeAmount1 = 5;    // how many points to fade the LED by
int brightness2 = 0;    // how bright the LED is
int fadeAmount2 = 5;    // how many points to fade the LED by

void setup()
  {
    pinMode(LED01, OUTPUT);
    pinMode(LED02, OUTPUT);
    pinMode(LED03, OUTPUT);
    pinMode(LED04, OUTPUT);
    pinMode(LED05, OUTPUT);
    pinMode(LED06, OUTPUT);
    pinMode(LED07, OUTPUT);
    pinMode(LED08, OUTPUT);
    pinMode(LED09, OUTPUT);
    randomSeed(analogRead(0));
  }

void loop()
  {
    FlashPair();
    Sequence();
    Ramp1();
    Ramp2();
  }
void FlashPair()
  {
    digitalWrite(LED01, HIGH); digitalWrite(LED03, HIGH);
    digitalWrite(LED02, LOW);  digitalWrite(LED04, LOW);
    delay(500);
    digitalWrite(LED01, LOW);  digitalWrite(LED03, LOW);
    digitalWrite(LED02, HIGH); digitalWrite(LED04, HIGH);
  }
void Sequence()
  {
    long randNumber;
    //5 ~~ 5 ~~ 8 ~~ 8 ~~ 7 ~~ 7
    //8 ~~ 7 ~~ 5 ~~ 7 ~~ 5 ~~ 8
    //7 ~~ 8 ~~ 7 ~~ 5 ~~ 8 ~~ 5
    randNumber = random(1, 6);
    if (randNumber == 1)
      {
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW); delay(500);
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW); delay(500);
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW);
      }
    if (randNumber == 2)
      {
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW); delay(500);
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW); delay(500);
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW);
      }
    if (randNumber == 3)
      {
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW); delay(500);
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW); delay(500);
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW);
      }
    if (randNumber == 4)
      {
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW); delay(500);
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW); delay(500);
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW);
      }
    if (randNumber == 5)
      {
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW); delay(500);
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW); delay(500);
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW);
      }
    if (randNumber == 6)
      {
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW); delay(500);
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW); delay(500);
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW);
      }
  }
void Ramp1()
  {
    // set the brightness of pin 6:
    analogWrite(LED06, brightness1);
    // change the brightness for next time through the loop:
    brightness1 = brightness1 + fadeAmount1;
    // reverse the direction of the fading at the ends of the fade:
    if (brightness1 <= 0 || brightness1 >= 255)
      {
        fadeAmount1 = -fadeAmount1;
      }
    // wait for 20 milliseconds to see the dimming effect
    delay(20);
  }
void Ramp2()
  {
    delay(150);
    // set the brightness of pin 9:
    analogWrite(LED09, brightness2);
    // change the brightness for next time through the loop:
    brightness2 = brightness2 + fadeAmount2;
    // reverse the direction of the fading at the ends of the fade:
    if (brightness2 <= 0 || brightness2 >= 255)
      {
        fadeAmount2 = -fadeAmount2;
      }
    // wait for 50 milliseconds to see the dimming effect
    delay(50);
  }

Everything seems to be working... except that pin 6 & pin 9 seem to be on, both at the same brightness, and aren't fading or brightening.  Am I missing something here?


TheQuestor

  • Full Member
  • ***
    • Posts: 15
    • View Profile
I actually "fixed" that problem -- however, the ramp up/down process was WAY too slow; I solved that by not only increasing the amount for the brightness, but calling the ramp functions before & after every delay within my 2nd function.  Not as good as I wanted, but good enough.


lennevia

  • Administrator
  • Hero Member
  • *****
    • Posts: 437
    • View Profile
If you wanted the fade effect to move faster, you can decrease the delay. It looks like you have it at 50ms in your program.


TheQuestor

  • Full Member
  • ***
    • Posts: 15
    • View Profile
I actually removed the delay, but it's still slow.  Ideally, I'd like all of my effects to be happening simultaneously, but that's just not possible.  I guess close is good enough.


 

SMF spam blocked by CleanTalk