Blink & Button Objects - WIP


i starting move demo projects c++ classes. haven't messed classes since 1999.

but works far ioblinker class blinky led objects , iobutton class make button objects. buttons debounce. sketch reads button find output state may -1=undecided, 0=off, 1=on, 2=changed off since last read, 3=changed on since last. sketch doesn't need remember last read, it's taken care of states 2 , 3.

in future plan add other demo interfaces classes, including serial user io.

but need advice. want stick arduino ide if add library files in ide change , save them, save in sketchbook folder instead of ide library folder. open editor, more work me.  

and here's (already improved) demo. requires arduino , 2 jumpers or buttons.
now added setpin function led object , demo goes buttons array.

code: [select]

// ioclasses demo io_events_2  dec 21, 2014 goforsmoke
/*
  sketch uses 2 classes ioclasses.h make led13
 blink , grounded jumpers on pins 6 & 7 control rate.
 ground pin 6 , rate adds 100ms. pin 7 subtracts 100ms.
 
 button output state value use in sketch code.
 -1, undecided, pin state changed @ least debounce-time ago.
 0, button still not pressed.
 1, button still pressed.
 2, button released since last checked sketch.
 3, button pressed since last checked sketch.
 the sketch doesn't have track previous output state.
 */

#include <ioclasses.h>

// led13( blink-rate, arduino-pin, port, mask-to-toggle-pin-reg );
ioblinker led13( 500, 13, 'b', 0x20 ); // pin 13 port b pin 5
// button-name( arduino-pin, millis-debounce );
iobutton  goslower( 6, 20ul );
iobutton  gofaster( 7, 20ul );
iobutton  *buttons[ 2 ] = {
  &goslower, &gofaster };
byte buttonidx, buttonread;

unsigned long val;


void setup( void )
{
  serial.begin( 115200 );
  serial.println( );
  serial.println( f( "\n  startup\n" ));
  // object starts set pinmodes , states
  led13.startblinker( );
  led13.setpin( low );
  gofaster.startbutton();
  goslower.startbutton();
}

void loop( void )
{
  // gotta run them
  led13.runblinker( );

  // running 1 button per loop(), many buttons don't load it.
  buttons[ buttonidx ]->runbutton( ); // running 1 button per loop

  // using button data change blink rate
  if ( !buttonidx ) // check button run
  {
    buttonread = buttons[ buttonidx ]->readoutput();
    if ( buttonread == 3 ) // 1st read since pressed
    {
      serial.print( f( "slower " ));
      val = led13.getwait();
      if ( val < 9900ul )
      {
        val += 100ul;
      }   
      else
      {
        val = 10000ul;
      }
      led13.setblinker( val );
      serial.println( val );
    }
  }

  if ( buttonidx ) // check button run
  {
    // using button data change blink rate
    byte buttonread = buttons[ buttonidx ]->readoutput();
    if ( buttonread == 3 ) // 1st read since pressed
    {
      serial.print( f( "faster " ));
      val = led13.getwait();
      if ( val > 100ul )
      {
        val -= 100ul;
      }   
      else
      {
        val = 0;
      }
      led13.setblinker( val );
      serial.println( val );
    }
  }

  buttonidx ^= 1; // toggles bit 0
}





Arduino Forum > Using Arduino > Programming Questions > Blink & Button Objects - WIP


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code