Multiple button presses


I was thinking today that I would love to be able to do more than two things with the button per set of code. Would it be possible to add a command to the API that would allow that?

Say one button press starts the code. After that, any short button presses adds an LED flash. When you want to release all the stored up flashes, do a long button press. Another long button press restarts the code back to the beginning. Could that work using if statements or something (if flashes are stored up long button press makes them happen
else long button press restarts code)? I don’t know if that’s how one would do it, seeing as I don’t know much of the code behind the Jewelbots code, but it seems like it should be possible.


Hey !

I think I made something that does what you describe!
However, I’m not entirely sure if I fully understood what you wanted though.

From what I am assuming, you want to be able to have the two buttons interact with each other?
So let’s say you press the short button 5 times, then the long button will flash blue 5 times. Something like that?

If that’s what you meant, then try the code below! (if it’s not, you can still try it! LOL)

LED led;
Buzzer buzzer;
Timer timer;
int x = 0; //increment starting value

void setup() {
  // put your setup code here, to run once:

void loop() {
  // put your main code here, to run repeatedly:

void button_press(void){
    // Define what happens when the Magic Button is pressed quickly
    // (when the Jewelbot is not plugged into power)

    x = x+1; //increases the value of x by 1 each time you press the button

void button_press_long(void) {
    // Define what happens when the Magic Button is held for 2 seconds
    // (when the Jewelbot is not plugged into power)

    for(int y = 1; y <= x; y++){ 
      led.flash_all(BLUE, 1000);  

       //once y is equal to x (the number of times you pressed the short button), 
       //it starts over the value of x back to 0.
       if (y == x)
          x = 0;


Ohooooooo, this looks promising! I tried so hard to make this and I finally decided it must not be possible. I didn’t know how to make loops in Jewelbot code yet! (I can make a loop in Python with my eyes closed, but this is just enough different that I couldn’t figure it out. I totally should have googled it, but…)

I have yet to try this code, as my mom is on the computer, but I’ll let you know for sure how it works when I do. I might not get back to you until tomorrow, 'cause it’s almost dinner time and then bedtime. Looking at the code, it seems like it should do the right thing.


If you want to learn more about the for loop that I used, you can check out the forum post here: :smiley:

Definitely post how you use this code concept when you get the chance!! I’m always super excited by your creativity :blush: !!!

I also forgot to mention that if you want the long button to do something specific the number of times you press the short button, then make sure it’s within the for loop function !


This certainly does work! Thank you very much. I’m currently working on adapting it to do something else even cooler, but that’s requiring some more work. :slight_smile: