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
Parents
  • 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
Reply
  • 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
Children
No Data
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