element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Arduino Troubleshooting
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 7 replies
  • Subscribers 391 subscribers
  • Views 693 views
  • Users 0 members are here
  • mega
  • troubleshooting
  • trouble
  • troubleshoot
  • mega2560
  • arduino
  • arduino_mega
Related

Arduino Troubleshooting

dukeofmarshall
dukeofmarshall over 10 years ago

Good morning,

I am working on my first project and one of the things I am learning is interrupts. Specifically an interrupt from a button. I have connected up four LEDs on a breadboard to an Arduino Mega 2560. The sketch is simple enough. There are two patterns to light up the LEDs. One goes through each LED individually and the second pattern does both red LEDs at once and then switches to both green LEDs at once. Just something simple. I connected a button as an interrupt and this is meant to switch between patterns.

 

The basic premise works. The sketch starts out with one pattern and on the button push switches to the other pattern. However, the issue that I am running into is that after a short period one of the patterns gets "stuck" for some reason. You can view the issue in the video and the sketch is below as well as the serial output.

 

Another issue I am having is the initial serial output. For some reason the output is garbled and I have no idea why.

 

So is something wrong with the sketch or is there something possibly wrong with my Arduino? Thanks in advance for your time and advice.


You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

#define RED1 9

#define GREEN1 7

#define RED2 5

#define GREEN2 3

volatile int sequence = 0;

long last = 0;

volatile long last_used_millis = 0;

int delay_seconds = 1;

int sequence_number = 0;

 

 

void setup() {

  // put your setup code here, to run once:

    Serial.begin(9600);

    pinMode(RED1, OUTPUT);

    pinMode(RED2, OUTPUT);

    pinMode(GREEN1, OUTPUT);

    pinMode(GREEN2, OUTPUT);

    attachInterrupt(2, button, FALLING);

    Serial.println("Starting Program.............");

}

 

 

void loop() {

    int current_millis = millis();

 

    if(sequence == 1){

        if(current_millis - last_used_millis > (delay_seconds * 500)){

            switch(sequence_number){

                case 0:

                    digitalWrite(GREEN2, LOW);

                    digitalWrite(RED1, HIGH);

                    sequence_number = 1;

                    break;

                case 1:

                    digitalWrite(RED1, LOW);

                    digitalWrite(GREEN1, HIGH);

                    sequence_number = 2;

                    break;

                case 2:

                    digitalWrite(GREEN1, LOW);

                    digitalWrite(RED2, HIGH);

                    sequence_number = 3;

                    break;

                case 3:

                    digitalWrite(RED2, LOW);

                    digitalWrite(GREEN2, HIGH);

                    sequence_number = 0;

                    break;

            }

 

 

            last_used_millis = current_millis;

        }

    }else{

        if(current_millis - last_used_millis > (delay_seconds * 1000)){

            switch(sequence_number){

                case 0:

                    digitalWrite(GREEN1, LOW);

                    digitalWrite(GREEN2, LOW);

                    digitalWrite(RED1, HIGH);

                    digitalWrite(RED2, HIGH);

                    sequence_number = 1;

                    break;

                case 1:

                    digitalWrite(RED1, LOW);

                    digitalWrite(RED2, LOW);

                    digitalWrite(GREEN1, HIGH);

                    digitalWrite(GREEN2, HIGH);

                    sequence_number = 0;

                    break;

                default:

                    digitalWrite(GREEN1, LOW);

                    digitalWrite(GREEN2, LOW);

                    digitalWrite(RED1, HIGH);

                    digitalWrite(RED2, HIGH);

                    sequence_number = 1;

                    break;

            }

 

 

            last_used_millis = current_millis;

        }

    }

}

 

 

void button(){

    long current_millis = millis();

 

 

    if(current_millis - last > 1000){

        Serial.println("Button pressed");

 

 

        Serial.println("Current Millis is: "+String(current_millis));

        Serial.println("Last Millis is:    "+String(last));

        Serial.println("Difference is:     "+String(current_millis-last));

        if(sequence == 1){

            sequence = 0;

            Serial.println("Sequence 0");

        }else{

            sequence = 1;

            Serial.println("Sequence 1");

        }

        last = current_millis;

    }

}

 

Serial Output:

image

  • Sign in to reply
  • Cancel
  • neilk
    neilk over 10 years ago

    Hi dukeofmarshall

     

    I think you need to look at this: https://www.arduino.cc/en/Reference/AttachInterrupt, which includes the following:

     

    "inside the attached function, delay() won't work and the value returned by millis() will not increment............... See the section on ISRs below for more information"

     

    I also followed the link labelled "Nick Gammon's Notes". Nick gives some simple rules about writing ISRs, including: "Don't Use Serial Prints"

     

    My own experience of using ISRs is to Set a flag inside the ISR and use the flag externally to indicate the need to execute a new section of code.

     

    Hope this helps

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 10 years ago

    A couple of questions:

    Are you using the default bootloader that comes with the board?

    Are you using the Arduino IDE to code with?

    If so, what version of the IDE are you using?

    Do you know what version of the AVR GCC compiler you're using?  This is assuming that you're, in fact, using the AVR GCC,

     

    I do notice that with at least I can see of the code you're not using one of the "Interrupts" libraries.  Obviously, I don't know if this was a conscious choice, or if you weren't aware of them.  I've occasionally seen where the AVR GCC doesn't use a destructor effectively (and sometimes not at all).  The ooPinChangeInt (and/or the arduino_pins, for the Atmega 128) library can make life easier.

     

    Most of the old problems with interrupts have been ironed out over time (and versions).  Some still crop up, however.  I wouldn't think this was a board problem, but someone with more expertise than myself might have a different opinion.  It would seem more like a memory leak that eventually crashes the programming.  I don't know how consistent the timing of the problem is (based upon that same number of state changes).  If the timing is fairly consistent then I'd assume it was a memory leak before thinking about a bad subckt on the chip.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • dukeofmarshall
    dukeofmarshall over 10 years ago in reply to Former Member

    Default bootloader is being used.

    Arduino IDE is being used (1.6.5)

    AVR GCC? I have no clue. I can't even tell you what AVR GCC is right now.

     

    I didn't even know there were interrupt libraries. Seemed like a fairly basic and straightforward thing. I'll have to look more into that.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • dukeofmarshall
    dukeofmarshall over 10 years ago in reply to neilk

    Yeah, I was aware of the delay thing and that millis wouldn't increment. However, I thought that the process was moving through so fast that it wouldn't matter in an interrupt like that. Plus some of the examples that I ran across online for interrupts used pretty much the same process.

     

    So you think it might be with the Serial.println in the interrupt function?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • dukeofmarshall
    dukeofmarshall over 10 years ago

    Here's the serial output I forgot to include in the original post.

     

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 10 years ago in reply to dukeofmarshall

    Thanks Todd.

     

    I would echo what Neil Kenyon said about both the references and the Serial.PrintIn function.

     

    Just FYI: AVR GCC is the AVR flavor of the Gnu C Compiler.  I've not kept up on the particulars of under-the-covers on the Arduino IDE, but back in the "old days" (ca. 2011) I had some freaky problems with a Mega that ended up being related to the AVR GCC.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • neilk
    neilk over 10 years ago in reply to dukeofmarshall

    hi dukeofmarshallHow about making your ISR as short as possible - eg do nothing apart from setting a flag to show that an interrupt has been received. Then in your main code - which is continually looping  - check the status of the flag and use that to decide whether or not to execute the code that currently sits in your ISR, remembering to clear the flag before you return to the main code.

     

    Hope this helps

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube