button + arduino quetions

Hi OF forum

I am (still) making a puppet theater installation with a 4 puppets conected to 4 buttons that triggers a storylabyrinth, told thrue videos.

But i can’t get my code to work as i want to and i don’t know what to do other than ask in here.

I have two questions:

My button code looks like this, it is part of a button class:

  
bool buttonClass::Indian(){  
    bool changeToMovie = false;  
  
    //delete the oldest value  
    if( indieHis.size() >= 2) indieHis.erase(indieHis.begin(), indieHis.begin()+1);  
  
    int currentIndie = ard.getDigital(2);  
  
  
    if(currentIndie == -1)return false;  
  
    //add the newest value to the end  
    indieHis.push_back(currentIndie);  
   
    if( indieHis.size() >= 2){  
        if( indieHis[0] !=  indieHis[1] ){  
            changeToMovie = true;  
  
            printf("Indie is pressed!\n");  
  
            set.isWhichAct();  
            set.settings(0);  
  
            mov.varUpdates();  
            mov.loadMovie();  
            mov.playMovie();  
  
            set.isCounter();  
        }  
    }  
    return changeToMovie;  
}  

I quess i put my serial input thru a vector that updates all the time.

1. question: I need the buttons to trigger when ever it is pushed, not according to on or off. How is that possible?

  1. question: When the button triggers the videos it is not listning for any serial input while the video is playing, you can push the other buttons without anything is changing. But it seems like the button history is stored, so when the video is finished it goes on according to the stored history. Is it possible to clear the serialhistory or something like that?

Hi Johanne,

I’m not totally sure if this will help you, but if I understand it correctly you want to check if a certain button has been pressed. I normally create some sort of simple protocol like: “start_data_character”, “databyte”, “end_data_character”. Then when I press a button I send something over serial like: ‘$1%’, where ‘$’ is the start byte and ‘%’ is the end byte. Then in my openFrameworks code I add all new characters to a buffer and parse it until I find a valid data string.

like: if (buffer[i] == ‘$’ && buffer[i+2] == ‘%’) {
… found data
… int button = buffer[i+1];
}…

(you’ll need to add some checks).

I hope this helps you.

Roxlu

If all you’re doing is reading simple values from the Arduino you might want to just use the ofSerial class to send a signal when a button is pressed down or released. The firmata library stores the history of the buttons, so when you call getDigital you’re getting the last action. In order to make sure that you’re only getting states that are changed you could just poll the button, i.e. call getDigital(2) at a regular interval to look for changes. Otherwise, you might want to take a look at the the advancedEventsExample in the examples and use that to help you set up a listener for the EDigitalPinChanged event that the ofStandardFirmata dispatches.

@joshua – I think firmata is better for this, because you could rewire without having to recode the arduino…

simple answer to (1), use a boolean flag.

in pseudo code:

in h:

  
bool bPressedEventRegistered;  

in setup:

  
bPressedEventRegistered = false;  

in update:

  
if (button is NOT down){  
  bPressedEventRegistered = false  
} else {  
   if (bPressedEventRegistered == false){  
    // DO THE ONE TIME THING HERE  
    bPressedEventRegistered = true;  
  }  
}  

this is a good technique for getting the “change event” of an on / off state

I guess with two, as suggested, you should read the value every frame and you wont have old data…

take care!
zach