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 Push button to cycle through different modes
  • 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 1 reply
  • Subscribers 389 subscribers
  • Views 1390 views
  • Users 0 members are here
  • mode_switch
  • button
  • arduino
  • shift_register
Related

Push button to cycle through different modes

Former Member
Former Member over 10 years ago

Hello,

I am using the library from Elco Jacobs to control 8 RGB LEDs.

(ShiftPWM_Non_Blocking fromShiftPWM: the easiest software PWM library for Arduino | ElcoJacobs.com)

The sketch is awesome. It includ es 9 different modes for the LEDs and uses Serial.parseInt() to set the mode.

I built up the circuit on a perf board and want the whole system to be controlled by just 1 push button. I want the program to start when plugged in and stay in the first mode and switch to the second mode when the push button is pressed. Then, when pressed again, move to the 3rd mode then 4th ect.

I feel like this should be easy but I'm new to this stuff and I cannot find any help anywhere on this!

 

Thanks for any and all help.

 

 

/////This is the part of the sketch that selects the mode

 

void setup(){

  while(!Serial){

    delay(100);

  }

  Serial.begin(9600);

 

  // Sets the number of 8-bit registers that are used.

  ShiftPWM.SetAmountOfRegisters(numRegisters);

 

  // SetPinGrouping allows flexibility in LED setup.

  // If your LED's are connected like this: RRRRGGGGBBBBRRRRGGGGBBBB, use SetPinGrouping(4).

  ShiftPWM.SetPinGrouping(1); //This is the default, but I added here to demonstrate how to use the funtion

 

  ShiftPWM.Start(pwmFrequency,maxBrightness);

  printInstructions();

}

 

void loop()

{   

  if(Serial.available()){

    if(Serial.peek() == 'l'){

      // Print information about the interrupt frequency, duration and load on your program

      ShiftPWM.PrintInterruptLoad();

    }

    else if(Serial.peek() == 'm'){

      // Print instructions again

      printInstructions();

    }

    else{

      fadingMode = Serial.parseInt(); // read a number from the serial port to set the mode

      Serial.print("Mode set to ");

      Serial.print(fadingMode);

      Serial.print(": ");

      startTime = millis();

      switch(fadingMode){

      case 0:

        Serial.println("All LED's off");

        break;

      case 1:

        Serial.println("Fade in and out one by one");

        break;

      case 2:

        Serial.println("Fade in and out all LED's");

        break;

      case 3:

        Serial.println("Fade in and out 2 LED's in parallel");

        break;

      case 4:

        Serial.println("Alternating LED's in 6 different colors");

        break;

      case 5:

        Serial.println("Hue shift all LED's");

        break;

      case 6:

        Serial.println("Setting random LED's to random color");

        break;

      case 7:

        Serial.println("Fake a VU meter");

        break;

      case 8:

        Serial.println("Display a color shifting rainbow as wide as the LED's");

        break;        

      case 9:

        Serial.println("Display a color shifting rainbow wider than the LED's");

        break;    

      default:

        Serial.println("Unknown mode!");

        break;

      }

    }

    while (Serial.read() >= 0){

      ; // flush remaining characters

    }

  }

  switch(fadingMode){

  case 0:

    // Turn all LED's off.

    ShiftPWM.SetAll(0);

    break;

  case 1:

    oneByOne();

    break;

  case 2:

    inOutAll();

    break;

  case 3:

    inOutTwoLeds();

    break;

  case 4:

    alternatingColors();

    break;

  case 5:

    hueShiftAll();

    break;

  case 6:

    randomColors();

    break;

  case 7:

    fakeVuMeter();

    break;

  case 8:

    rgbLedRainbow(3000,numRGBLeds);

    break;

  case 9:

    rgbLedRainbow(10000,5*numRGBLeds);   

    break;  

  default:

    Serial.println("Unknown Mode!");

    delay(1000);

    break;

  }

}

  • Sign in to reply
  • Cancel
Parents
  • gadget.iom
    gadget.iom over 10 years ago

    Certainly should be possible.

     

    The following code sample should give you a good starting point. The number generated by this code can be used to represent a mode.

    Arduino - ButtonStateChange

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • gadget.iom
    gadget.iom over 10 years ago

    Certainly should be possible.

     

    The following code sample should give you a good starting point. The number generated by this code can be used to represent a mode.

    Arduino - ButtonStateChange

    • 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