Pages: [1]   Go Down
Author Topic: weird buggy code problem  (Read 1730 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Dont think like a computer, make it think like you
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This function works fine, as long as the if statement in the middle is commented out. If I uncomment it the code breaks. It's such a simple piece that I don't know if I'm not looking at it closely enough or if there's a bug involved. Does anyone else see what I'm missing?

The code in its entirety (sp?) is here:
http://grace.evergreen.edu/~hergab13/metro_counter.cpp

(If anyone has comments/critiques on my code in general I'm listening. This style of programming is completely new to me)

Gabe

// blink the metro light at the rate specified
void metroBlink() {
  if (millis() - previousMillisBlink > tempo) {
    previousMillisBlink = millis();   // remember the last time we blinked the LED
    
    // events to be triggered
    counter++;
    //Serial.println(counter);
/*    if (counter > counterMax) {
      counter = counterMin;
      eventDebounceCounter = true;
    }*/
    
    // blink led at specified interval w/o delay()
    digitalWrite(ledPinClock, HIGH);  //blink red led on
    
    // secondary delay
  } else if (millis() - previousMillisBlink > 100) { //100 ms flash on time
    //events
    digitalWrite(ledPinClock, LOW); //blink red led off
  }
}
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the function supposed to do?  When when you uncomment the if-statement, what does it do (i.e. what do you mean by "breaks")?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Dont think like a computer, make it think like you
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The function is a blinking metronome. When working properly it turns a led on every [tempo] ms, and off again after 100 ms. (see http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay) The commented-out code is trying to add a counter to the metronome (I'm working towards a simple midi sequencer).  

Every time the first if statement allows the processor to get to the code inside, where the blink is, the counter increments. The simple if statement after it is just to keep the counter within a specific range.

So this is the part where it breaks:
When I uncomment the if statement, the code never gets to the blink (digitalWrite(ledPinClock, HIGH)smiley-wink that follows the if! It just stops executing somehow! This makes no sense!

To me it sounds like a bug in the hardware or compiler. Or, like I said above, something so obvious that I can't see it.

Gabe
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Dont think like a computer, make it think like you
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Figgered it out!

I had a function that wasn't called anywhere. I commented it and its function declaration out and the whole thing came back to life.

Believe it or not I've tripped over that one already.

So:

Don't leave unused functions in your code!

Can someone offer a suggestion as to why this breaks code?

Gabe
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's possible that the code is too big for the Arduino board - what size gets printed when you upload your sketch?  Unfortunately, we don't get error messages when this happens.  Arduino 0004 has a check if the sketch is too big, but it assumes the most recent bootloader version (which is 1 KB).  If you have an older (2 KB) version of the bootloader, the environment might not realize that the code is too big, attempt to upload it, and not realize that it failed.

When did you get your board?  If it's an old version, and you have access to an AVR-ISP or parallel port programmer, you might try burning the latest version of the bootloader (using the Tools | Burn Bootloader option).  Otherwise, you can tell the Arduino environment the amount of space available for sketches by editing the upload.maximum_size variable in your preferences file (see: http://www.arduino.cc/en/Main/Preferences for instructions on finding the file). Change 7168 to 6144, and the environment should correctly warn you when your sketch is too big.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Dont think like a computer, make it think like you
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've only had my arduino for a month or so, and the size of my code is 616X, so unfortunately I don't think that's related to my problem.

Gabe
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post a copy of the broken code somewhere so I can try to figure out the problem?  Or, if it's the same as the code you posted earlier, which function did you need to comment out for it to start working?
Logged

Pages: [1]   Go Up
Jump to: