Blink & Button Objects - WIP

I am starting to move my demo projects into C++ Classes. I haven’t messed with Classes since 1999.

But this works so far with an ioblinker class for blinky led objects and an iobutton class to make button objects. The buttons debounce. Your sketch reads the button to find the output state which may be -1=undecided, 0=off, 1=on, 2=changed to off since last read, 3=changed to on since last. Your sketch doesn’t even need to remember the last read, it’s taken care of by states 2 and 3.

In future I plan to add my other demo interfaces as classes, including serial user io.

But I need some advice. I want to stick with Arduino IDE but if I add the library files in the IDE then change and save them, they save in the sketchbook folder instead of the IDE library folder. What I do now is open those with an editor, more work for me.

And here’s the (already improved) demo. it requires an Arduino and 2 jumpers or buttons.
Now added is a setPin function for the led object and the demo goes into buttons as array.

// ioclasses demo io_events_2  Dec 21, 2014 by GoForSmoke
/*
  This sketch uses 2 classes from ioclasses.h to make led13
 blink and grounded jumpers on pins 6 & 7 to control the rate.
 Ground pin 6 and the rate adds 100ms. Pin 7 subtracts 100ms.
 
 Button output is a state value for use in sketch code.
 -1, Undecided, pin state changed at least debounce-time ago.
 0, button currently still not pressed.
 1, button currently still pressed.
 2, button released since last checked by sketch. 
 3, button pressed since last checked by sketch.
 The sketch doesn't have to 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 is 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 and states
  led13.startBlinker( );
  led13.setPin( LOW );
  gofaster.startButton();
  goslower.startButton();
}

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

  // by only running 1 button per loop(), many buttons don't load it.
  buttons[ buttonIdx ]->runButton( ); // only running 1 button per loop

  // now using button data to change blink rate
  if ( !buttonIdx ) // only check the button that was 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 ) // only check the button that was run
  { 
    // now using button data to 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
}

IOClasses.cpp (2.76 KB)

IOClasses.h (1.06 KB)