Where's The Code?

I'm using the small font to save space.
To make reading easier, just copy the text and paste it into your IDE.



pbmicros_varDuty_01c
NON-BLOCKING, NO delay()
variable duty, fixed frequency


//  pbmicros_varDuty_01c
//
//  same as pbmicros_varDuty_01b
//  BUT uses Port Manipulation instead of digitalWrite
//  roundabout 1.84 kc
//  no big improvement in jitter
//  source seems to be consequence of period (Off) termination ??
//  pb clicks to one of 5 pulse widths

unsigned long markTime;
unsigned long elapsedTime;
unsigned long lockoutmark;  // pb-related
const unsigned long debounceTime = 300000; // debounceTime

const unsigned long pulseTime [] = {5,75,100,300,400};  // ON duration
const unsigned long period = 500;  // total time, ON & OFF
byte speedindex;  // blinkSpeed[speedindex]

boolean lockout;  // if true then PB is not checked
byte pbstate;
const byte pbpin = 2;

boolean ledstate;
const byte ledpin = 3;

boolean pulsing;

void setup ()
{
  pinMode(pbpin,INPUT_PULLUP);
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin,HIGH);
  speedindex = 0;
  lockout = false;
  markTime = micros();  // get start of period to time (Pulse/HIGH; what's left/LOW)
  pulsing = true;
}

void loop ()
{
  elapsedTime = micros();  // grab the current "system time"
  if (lockout == false)  // ok to read
  {
    pbstate = digitalRead(pbpin);
    if (pbstate == 0)  // pb made active
    {
      lockoutmark = micros();  // record micros when pb went LOW
      speedindex++;
      lockout = true;
      // new pulse initiated
      markTime = micros();
      //digitalWrite(ledpin,HIGH);
      PORTD = PORTD | B00001000;
      pulsing = true;
    }
    if (speedindex > 4)
    {
      speedindex = 0;
    }
  }

  elapsedTime = micros();  // grab the current "system time"
  if ((lockout == true) && ((elapsedTime - lockoutmark) >= debounceTime))
  {
    lockout = false; // lockout done, threshold met
  }

  elapsedTime = micros();  // grab the current "system time"
  // find end of Pulse time
  if (((elapsedTime - markTime) >= pulseTime[speedindex]) && (pulsing == true)) //
  {   
    //digitalWrite(ledpin,LOW);
    PORTD = PORTD | B11110111;
    markTime = micros();
    elapsedTime = micros();  // grab the current "system time"
    pulsing = false;
  }

  // find end of cycle
  if (((elapsedTime - markTime) >= (period - pulseTime[speedindex]) 
  && (pulsing == false))) 
  {
    //digitalWrite(ledpin,HIGH);
    PORTD = PORTD | B00001000;;
    markTime = micros(); 
    elapsedTime = micros();  // grab the current "system time"
    pulsing = true;
  }
}


pbmillisRoutines_01
NON-BLOCKING, NO delay()
blink patterns, 4 LEDs
/*
  1 pushbutton and 4 LEDs  
  LED blink patterns selected with pushbutton actuation
*/
// 
//  pbmillisRoutines_01
// 
//  Working
//  2016.09.05
//
//  pattern remains active if held down
//

unsigned long markTime;
unsigned long elapsedTime;
unsigned long lockoutmark;  // pb-related
const unsigned long debounceTime = 500; // debounceTime

byte pinsArray [4] = {8,9,10,11};

const byte frame [] = {B00000011, B00001100,
                       B00000101, B00001010,
                       B00000110, B00001001,
                       B00000000, B11111111,
                       B00000001, B00000010, B00000100, B00001000,
                       B00000000, B00000001, B00000011, B00000111,
                       B00001111, B00001110, B00001100, B00001000}; 
                        
const byte routinePlan [6][2] = {0,1,   // first, last
                                 2,3,   // first, last
                                 4,5,   // first, last
                                 6,7,   // first, last
                                 8,11,  // first, last
                                 12,19};
byte first;
byte last;                      
byte routineIndex = 0; 
byte iteration = 0;

const unsigned long blinkSpeed = 500;

byte pbstate;
boolean lockout;  // if true then PB doesn't get checked
boolean tripped;  // pb hit, prep Update for new pointers etc.
const byte pbpin = 2;
byte index;  // general application, utility

void setup ()
{
  //Serial.begin(9600);
  pinMode(pbpin,INPUT_PULLUP);

  for(index=0; index<4; index ++)
  {
    pinMode(pinsArray[index],OUTPUT);
    digitalWrite(pinsArray[index],LOW);
  } 
 
  routineIndex = 0;
  lockout = false;
  markTime = millis();
 
  tripped = true;
  Update();
}

void loop ()
{
  elapsedTime = millis();
 
  if(lockout == false)  // ok to read
  {
    pbstate = digitalRead(pbpin);
    if (pbstate == 0)  // pb made active
    {
      lockoutmark = millis();  // record millis when pb went LOW
      routineIndex++;
      lockout = true;
      tripped = true;
      if(routineIndex > 5)  // rollover
      {
        routineIndex = 0;
      }
    }
  }
 
  if(tripped == true)
  {
    Update();   // brings a 'tripped' with it
  } 
 
  elapsedTime = millis();
  if ((lockout == true) && ((elapsedTime - lockoutmark) >= debounceTime))
  {
    lockout = false; // lockout done, threshold met
  }
 
  elapsedTime = millis();
  if((elapsedTime - markTime) >= blinkSpeed) // determine whether Update() is due
  {
    markTime = millis();  // new mark
    Update();
  }
}

void Update ()
{
  if(tripped == true) // if tripped == false then we got here because IT'S TIME
  {
    // new line, new frame
    first = routinePlan[routineIndex][0];
    last = routinePlan[routineIndex][1];
    tripped = false;
    iteration = 0;
  }
 
  if ((iteration + first) > last)
  {
    iteration = 0;
  }
 
  byte bitmask = 1;
 
  for(byte progress = 0; progress < 4; progress++)
  {
    if((frame[iteration + first] & bitmask) == 0)
    {
      digitalWrite(pinsArray[progress],LOW);
    }
    else
    {
      digitalWrite(pinsArray[progress],HIGH);
    }
    bitmask = bitmask * 2;
  }
  iteration++;
  elapsedTime = millis();
}


pbmillis3Speeds_01
NON-BLOCKING, NO delay()
/* 
    1 pushbutton, 1 LED
    1 pushbutton switch affecting 1 LED's blink rate
*/
//  pbmillis3Speeds_01
//  pb/input is accurate
//
//  works 2016.09.03
//
// if the pb is kept active then
// the speeds get cycled

//
// 

unsigned long markTime;
unsigned long elapsedTime;
unsigned long lockoutmark;  // pb-related
const unsigned long debounceTime = 250; // debounceTime

const unsigned long blinkSpeed [3] = {50,200,1000};
byte speedchoice;  // blinkSpeed[speedchoice]

boolean lockout;  // if true then PB is not checked
byte pbstate;
const byte pbpin = 2;

boolean ledstate;
const byte ledpin = 13;


void setup ()
{
  pinMode(pbpin,INPUT_PULLUP);
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin,LOW);
  speedchoice = 0;
  lockout = false;
  markTime = millis();
}

void loop ()
{
  elapsedTime = millis();
  if(lockout == false)  // ok to read
  {
    pbstate = digitalRead(pbpin);
    if (pbstate == 0)  // pb made active
    {
      lockoutmark = millis();  // record millis when pb went LOW
      speedchoice++;
      lockout = true;
    }
    if(speedchoice > 2)
    {
      speedchoice = 0;
    }
    elapsedTime = millis();
  }
 
  elapsedTime = millis();
  if ((lockout == true) && ((elapsedTime - lockoutmark) >= debounceTime))
  {
    lockout = false; // lockout done, threshold met
  }
 
  elapsedTime = millis();
  if((elapsedTime - markTime) >= blinkSpeed[speedchoice]) //
  {
    markTime = millis();  // new mark
    ledstate = !ledstate;
    if (ledstate == true)
    {
      digitalWrite(ledpin,HIGH);
    }
    else
    {
      digitalWrite(ledpin,LOW);
    }
  }
}


pb_timer_4pb_01
NON-BLOCKING, NO delay()
//  pb_timer_4pb_01
// 
//  looks like it works 2017.07.02 20h42
/*
Four pushbuttons, each initiates its own countdown timer -- turning its LED on for 30 seconds.
Actuating a pushbutton during a timing cycle aborts that cycle.
All in 'non-blocking' code.
*/

unsigned long markTime1;
unsigned long markTime2;
unsigned long markTime3;
unsigned long markTime4;

unsigned long elapsedTime;
unsigned long lockoutmark;  // pb-related
const unsigned long debounceTime = 400; // debounceTime

const byte T1 = 8;
const byte T2 = 9;
const byte T3 = 10;
const byte T4 = 11;

const unsigned long testTime = 30000;  // 30 sec.

boolean lockout;  // if true then PB doesn't get checked
boolean tripped;  // pb hit, prep Update for new pointers etc.

const byte pb1pin = 2;
byte pb1state;
boolean engaged1;

const byte pb2pin = 3;
byte pb2state;
boolean engaged2;

const byte pb3pin = 4;
byte pb3state;
boolean engaged3;

const byte pb4pin = 5;
byte pb4state;
boolean engaged4;

// ------------------------------------
void setup ()
{
  //Serial.begin(9600);
  pinMode(pb1pin,INPUT_PULLUP);
  pinMode(pb2pin,INPUT_PULLUP);
  pinMode(pb3pin,INPUT_PULLUP);
  pinMode(pb4pin,INPUT_PULLUP);

  pinMode(T1, OUTPUT);
  pinMode(T2, OUTPUT);
  pinMode(T3, OUTPUT);
  pinMode(T4, OUTPUT);   
 
  lockout = false;
  tripped = true;

  engaged1 = false;
  engaged2 = false;
  engaged3 = false;
  engaged4 = false;

  markTime1 = millis();
  markTime2 = millis();
  markTime3 = millis();
  markTime4 = millis();
}

void loop ()
{
  elapsedTime = millis(); 
  if(lockout == false)  // ok to read
  {
    pb1state = digitalRead(pb1pin);
    pb2state = digitalRead(pb2pin);
    pb3state = digitalRead(pb3pin);
    pb4state = digitalRead(pb4pin);
   
    if ((pb1state == 0) || (pb2state == 0) || (pb3state == 0) || (pb4state == 0))  // a pb was activated
    {
      lockoutmark = millis();  // record millis when a PB went LOW
      lockout = true;
      tripped = true;

      if (pb1state == 0)
      {
        if (engaged1 == true)
        {
          digitalWrite(T1, LOW);
          engaged1 = false;
        }
        else
        {
          digitalWrite(T1, HIGH);
          markTime1 = millis();
          engaged1 = true;
        }
      }
      if (pb2state == 0)
      {
        if (engaged2 == true)
        {
          digitalWrite(T2, LOW);
          engaged2 = false;
        }
        else
        {
          digitalWrite(T2, HIGH);
          markTime2 = millis();
          engaged2 = true;
        }
      }
   
      if (pb3state == 0)
      {
        if (engaged3 == true)
        {
          digitalWrite(T3, LOW);
          engaged3 = false;
        }
        else
        {
          digitalWrite(T3, HIGH);
          markTime3 = millis();
          engaged3 = true;
        }    
      }

      if (pb4state == 0)
      {
        if (engaged4 == true)
        {
          digitalWrite(T4, LOW);
          engaged4 = false;
        }
        else
        {
          digitalWrite(T4, HIGH);
          markTime4 = millis();
          engaged4 = true;
        }
      }   
    }
  }
 
  // -------------------------------------------------
   
  elapsedTime = millis();
  if ((lockout == true) && ((elapsedTime - lockoutmark) >= debounceTime))
  {
    lockout = false; // lockout done, threshold met
    tripped = false;
  }
  // ----------------------------------------------------
  if (engaged1 == true)
  {
    elapsedTime = millis();
    if((elapsedTime - markTime1) >= testTime) // is it soup yet?
    {
      digitalWrite(T1,LOW);
      engaged1 = false;
    }
  }

  if (engaged2 == true)
  {
    elapsedTime = millis();
    if((elapsedTime - markTime2) >= testTime) // is it soup yet?
    {
      digitalWrite(T2,LOW);
      engaged2 = false;
    }
  }

  if (engaged3 == true)
  {
    elapsedTime = millis();
    if((elapsedTime - markTime3) >= testTime) // is it soup yet?
    {
      digitalWrite(T3,LOW);
      engaged3 = false;
    }
  }

  if (engaged4 == true)
  {
    elapsedTime = millis();
    if((elapsedTime - markTime4) >= testTime) // is it soup yet?
    {
      digitalWrite(T4,LOW);
      engaged4 = false;
    }
  }
}

lightstring03

//   lightstring03
//   for LED christmas light strings and so on
//   needs work, but it works
//   '03: renamed gradualfadefast to fadeslow
//        adding fadefast
//        adding fastblinker
//   OK 20DEC2014

byte MOC_logic = 3;  // alias output
int Intensity;      // conduction angle
int startPosition;  // delay time till start
int index = 0;
volatile boolean zeroCrossed = false;  // Flag
byte scenario = 0;    // which pattern
byte iteration = 0;   // iterations

void setup()
{
  pinMode(MOC_logic, OUTPUT);
  attachInterrupt(0,cueStart,RISING); // Ext_Int on D2
}

// ***** ***** ***** ***** //
void loop ()
{
  if (zeroCrossed == true)
  {
    enableOutput();
  }
}
// ***** ***** ***** ***** //

// ----- The Interrupt -----
void cueStart ()
{
  zeroCrossed = true;   //  !!!
}
// ---- ---- ---- ---- -----
// ---- ---- ---- ---- -----

void enableOutput ()
{
  // scenario 0 fadeslow
  // scenario 1 blinks
  // scenario 2 solid on
  if (scenario == 0)    // fadeslow
  {
    fadeslow();
    optohandler();
    index++;
    if (index > 367)
    {
      iteration ++;
      index = 0;
    }
    if (iteration == 5)
    {
      scenario = 1;    // the next scenario #
      iteration = 0;
    }
  }  
  
  else if (scenario == 1)  // blinks
  {
    blinker();
    optohandler();
    index++;
    if (index > 60)
    {
      iteration ++;
      index = 0;
    }
    if (iteration == 10)
    {
      scenario = 2;      // the next scenario #
      iteration = 0;     // reset
    }
  }
  
  else if (scenario == 2)  // fadefast
  {
    fadefast();
    optohandler();
    index++;
    if (index > 186)
    {
      iteration ++;
      index = 0;
    }
    if (iteration == 9)
    {
      scenario = 3;    // the next scenario #
      iteration = 0;
    }
  }  
  
  else if (scenario == 3)  // fastblinks
  {
    fastblinker();
    optohandler();
    index++;
    if (index > 30)
    {
      iteration ++;
      index = 0;
    }
    if (iteration == 10)
    {
      scenario = 0;      // the next scenario #
      iteration = 0;     // reset
    }
  }  
}

// ----------------------------

void blinker ()
{
  if (index < 30)                           // dot
  {Intensity = 7000;}
  else if ((index >= 30) && (index < 60))   //
  {Intensity = 0;}    
}

// ----------------------------
void fadeslow ()   // gradual fast ramp
{
  if (index < 6)
  {Intensity = 6000;}
  else if ((index >= 6) && (index <= 11))
  {Intensity = 5850;}    
  else if ((index >= 12) && (index <= 17))
  {Intensity = 5700;}  
  else if ((index >= 18) && (index <= 23))
  {Intensity = 5550;}
  else if ((index >= 24) && (index <= 29))
  {Intensity = 5400;}
  else if ((index >= 30) && (index <= 35))
  {Intensity = 5250;}  
  else if ((index >= 36) && (index <= 41))
  {Intensity = 5100;}
  else if ((index >= 42) && (index <= 47))
  {Intensity = 4950;}  
  else if ((index >= 48) && (index <= 53))
  {Intensity = 4800;}  
  else if ((index >= 54) && (index <= 59))
  {Intensity = 4650;}  
  else if ((index >= 60) && (index <= 65))
  {Intensity = 4500;}  
  else if ((index >= 66) && (index <= 71))
  {Intensity = 4350;}
  else if ((index >= 72) && (index <= 77))
  {Intensity = 4200;}  
  else if ((index >= 78) && (index <= 83))
  {Intensity = 4050;}  
  else if ((index >= 84) && (index <= 89))
  {Intensity = 3900;}    
  else if ((index >= 90) && (index <= 95))
  {Intensity = 3750;}  
  else if ((index >= 96) && (index <= 101))
  {Intensity = 3500;}  
  else if ((index >= 102) && (index <= 107))
  {Intensity = 3350;}    
  else if ((index >= 108) && (index <= 113))
  {Intensity = 3200;}  
  else if ((index >= 114) && (index <= 119))
  {Intensity = 3050;}  
  else if ((index >= 120) && (index <= 125))  //
  {Intensity = 2900;}    
  else if ((index >= 126) && (index <= 131))
  {Intensity = 2750;}  
  else if ((index >= 132) && (index <= 137))
  {Intensity = 2600;}  
  else if ((index >= 138) && (index <= 143))
  {Intensity = 2450;}  
  else if ((index >= 144) && (index <= 149))
  {Intensity = 2300;} 
  else if ((index >= 150) && (index <= 155))
  {Intensity = 2150;}  
  else if ((index >= 156) && (index <= 161))
  {Intensity = 2000;}  
  else if ((index >= 162) && (index <= 167))
  {Intensity = 1850;}   
  else if ((index >= 168) && (index <= 173))
  {Intensity = 1700;}  
  else if ((index >= 174) && (index <= 179))
  {Intensity = 1550;}    
  else if ((index >= 176) && (index <= 181))
  {Intensity = 1400;}
  else if ((index >= 182) && (index <= 187))
  {Intensity = 1250;}   //   ** midpt **
// ramp back down
  else if ((index >= 188) && (index <= 193))
  {Intensity = 1400;}
  else if ((index >= 194) && (index <= 199))
  {Intensity = 1550;}
  else if ((index >= 200) && (index <= 205))
  {Intensity = 1700;}
  else if ((index >= 206) && (index <= 211))
  {Intensity = 1850;}
  else if ((index >= 212) && (index <= 217))
  {Intensity = 2000;}
  else if ((index >= 218) && (index <= 223))
  {Intensity = 2150;}
  else if ((index >= 224) && (index <= 229))
  {Intensity = 2300;}
  else if ((index >= 230) && (index <= 235))
  {Intensity = 2450;}
  else if ((index >= 236) && (index <= 241))
  {Intensity = 2600;}
  else if ((index >= 242) && (index <= 247))
  {Intensity = 2750;}
  else if ((index >= 248) && (index <= 253))
  {Intensity = 2900;}
  else if ((index >= 254) && (index <= 259))
  {Intensity = 3050;}
  else if ((index >= 260) && (index <= 265))
  {Intensity = 3200;}
  else if ((index >= 266) && (index <= 271))
  {Intensity = 3350;}
  else if ((index >= 272) && (index <= 277))
  {Intensity = 3500;}
  else if ((index >= 278) && (index <= 283))
  {Intensity = 3750;}
  else if ((index >= 284) && (index <= 289))
  {Intensity = 3900;}
  else if ((index >= 290) && (index <= 295))
  {Intensity = 4050;}
  else if ((index >= 296) && (index <= 301))
  {Intensity = 4200;}
  else if ((index >= 302) && (index <= 307))
  {Intensity = 4350;}
  else if ((index >= 308) && (index <= 313))
  {Intensity = 4500;} 
  else if ((index >= 314) && (index <= 319))
  {Intensity = 4650;}
  else if ((index >= 320) && (index <= 325))
  {Intensity = 4800;}
  else if ((index >= 326) && (index <= 331))
  {Intensity = 4950;} 
  else if ((index >= 332) && (index <= 337))
  {Intensity = 5100;}
  else if ((index >= 338) && (index <= 343))
  {Intensity = 5250;}
  else if ((index >= 344) && (index <= 349))
  {Intensity = 5400;}
  else if ((index >= 350) && (index <= 355))
  {Intensity = 5550;}
  else if ((index >= 356) && (index <= 361))
  {Intensity = 5700;}
  else
  {Intensity = 5850;}  // index 362-367
}
  
// ----------------------------  
void optohandler ()
{
  if (Intensity == 0)  // OFF = (Intensity = 0)
  {
    delay (1);  // so same rise & dbl trig
  }
  else
  {
    startPosition = (8000 - Intensity); // "delay angle"
    delayMicroseconds (startPosition);
    digitalWrite(MOC_logic, HIGH);   
    delayMicroseconds (Intensity);  // "conduction angle"
    digitalWrite(MOC_logic, LOW);
  }
  zeroCrossed = false;  // Reset
}  

//-------------------------------
void fadefast ()   // gradual fast ramp
{
  if (index < 3)
  {Intensity = 6000;}
  else if ((index >= 4) && (index <= 6))
  {Intensity = 5850;}    
  else if ((index >= 7) && (index <= 9))
  {Intensity = 5700;}  
  else if ((index >= 10) && (index <= 12))
  {Intensity = 5550;}
  else if ((index >= 13) && (index <= 15))
  {Intensity = 5400;}
  else if ((index >= 16) && (index <= 18))
  {Intensity = 5250;}  
  else if ((index >= 19) && (index <= 21))
  {Intensity = 5100;}
  else if ((index >= 22) && (index <= 24))
  {Intensity = 4950;}  
  else if ((index >= 25) && (index <= 27))
  {Intensity = 4800;}  
  else if ((index >= 28) && (index <= 30))
  {Intensity = 4650;}  
  else if ((index >= 31) && (index <= 33))
  {Intensity = 4500;}  
  else if ((index >= 34) && (index <= 36))
  {Intensity = 4350;}
  else if ((index >= 37) && (index <= 39))
  {Intensity = 4200;}  
  else if ((index >= 40) && (index <= 42))
  {Intensity = 4050;}  
  else if ((index >= 43) && (index <= 45))
  {Intensity = 3900;}    
  else if ((index >= 46) && (index <= 48))
  {Intensity = 3750;}  
  else if ((index >= 49) && (index <= 51))
  {Intensity = 3500;}  
  else if ((index >= 52) && (index <= 54))
  {Intensity = 3350;}    
  else if ((index >= 55) && (index <= 57))
  {Intensity = 3200;}  
  else if ((index >= 58) && (index <= 60))
  {Intensity = 3050;}  
  else if ((index >= 61) && (index <= 63))  //
  {Intensity = 2900;}    
  else if ((index >= 64) && (index <= 66))
  {Intensity = 2750;}  
  else if ((index >= 67) && (index <= 69))
  {Intensity = 2600;}  
  else if ((index >= 70) && (index <= 72))
  {Intensity = 2450;}  
  else if ((index >= 73) && (index <= 75))
  {Intensity = 2300;} 
  else if ((index >= 76) && (index <= 78))
  {Intensity = 2150;}  
  else if ((index >= 79) && (index <= 81))
  {Intensity = 2000;}  
  else if ((index >= 82) && (index <= 84))
  {Intensity = 1850;}   
  else if ((index >= 85) && (index <= 87))
  {Intensity = 1700;}  
  else if ((index >= 88) && (index <= 90))
  {Intensity = 1550;}    
  else if ((index >= 91) && (index <= 93))
  {Intensity = 1400;}
  else if ((index >= 94) && (index <= 96))
  {Intensity = 1250;}   //   ** midpt **
// ramp back down
  else if ((index >= 97) && (index <= 99))
  {Intensity = 1400;}
  else if ((index >= 100) && (index <= 102))
  {Intensity = 1550;}
  else if ((index >= 103) && (index <= 105))
  {Intensity = 1700;}
  else if ((index >= 106) && (index <= 108))
  {Intensity = 1850;}
  else if ((index >= 109) && (index <= 111))
  {Intensity = 2000;}
  else if ((index >= 112) && (index <= 114))
  {Intensity = 2150;}
  else if ((index >= 115) && (index <= 117))
  {Intensity = 2300;}
  else if ((index >= 118) && (index <= 120))
  {Intensity = 2450;}
  else if ((index >= 121) && (index <= 123))
  {Intensity = 2600;}
  else if ((index >= 124) && (index <= 126))
  {Intensity = 2750;}
  else if ((index >= 127) && (index <= 129))
  {Intensity = 2900;}
  else if ((index >= 130) && (index <= 132))
  {Intensity = 3050;}
  else if ((index >= 133) && (index <= 135))
  {Intensity = 3200;}
  else if ((index >= 136) && (index <= 138))
  {Intensity = 3350;}
  else if ((index >= 139) && (index <= 141))
  {Intensity = 3500;}
  else if ((index >= 142) && (index <= 144))
  {Intensity = 3750;}
  else if ((index >= 145) && (index <= 147))
  {Intensity = 3900;}
  else if ((index >= 148) && (index <= 150))
  {Intensity = 4050;}
  else if ((index >= 151) && (index <= 153))
  {Intensity = 4200;}
  else if ((index >= 154) && (index <= 156))
  {Intensity = 4350;}
  else if ((index >= 157) && (index <= 159))
  {Intensity = 4500;} 
  else if ((index >= 160) && (index <= 162))
  {Intensity = 4650;}
  else if ((index >= 163) && (index <= 165))
  {Intensity = 4800;}
  else if ((index >= 166) && (index <= 168))
  {Intensity = 4950;} 
  else if ((index >= 169) && (index <= 171))
  {Intensity = 5100;}
  else if ((index >= 172) && (index <= 174))
  {Intensity = 5250;}
  else if ((index >= 175) && (index <= 177))
  {Intensity = 5400;}
  else if ((index >= 178) && (index <= 180))
  {Intensity = 5550;}
  else if ((index >= 181) && (index <= 183))
  {Intensity = 5700;}
  else
  {Intensity = 5850;}  // index 184-186
}

void fastblinker ()
{
  if (index < 15)                           // dot
  {Intensity = 7000;}
  else if ((index >= 15) && (index < 30))   //
  {Intensity = 0;}    
}


echopper_PBs_04

//  echopper_PBs_04
// 
//  External Interrupt on D2  !!
//
//  Commented out Display_It and its calls 062412
//  Levels = 7 ? ? ?

byte speed_idx = 0;      //
byte currentLevel = 0;   // comparison
byte newLevel = 0;       // comparison
byte Accel_Btn = 4;      // alias - Accelerate btn
byte Decel_Btn = 5;      // alias - Decelerate btn
byte Zeropwr = 6;        // alias - "kill" button
byte motorpin = 11;      // pin alias, PWM to gate/MOT
byte Level [6] = {0,20,40,100,175,250};
// Consider incl a Level between 40 and 100, which is a jump.
// byte Level [7] = {0,25,50,75,100,150,250};  ? ? ?
volatile boolean Detect = false;  // Interrupt Note


void setup()
{
  pinMode(motorpin, OUTPUT);
  pinMode(Zeropwr, INPUT);
  pinMode(Accel_Btn, INPUT);
  pinMode(Decel_Btn, INPUT);
  //Serial.begin(9600);
  attachInterrupt(0,Activity,RISING);  // Ext_Int on D2
}


void loop()
{
  if (Detect == true)
  {
    Decode();
  }
  if (currentLevel != newLevel)
  {
    analogWrite(motorpin,Level[speed_idx]);
    currentLevel = newLevel;
  }
}

void Activity()   // this is the Interrupt !
{
  Detect = true;
}

void Decode()
{
  Detect = false;          // clear out "Detect"
  if (digitalRead(Zeropwr) == HIGH)
  {
    speed_idx = 0;
    newLevel = 0;
    //Display_it();
  }
  else if (digitalRead(Accel_Btn) == HIGH)
  {
    Accelerate();
  }
  else if (digitalRead(Decel_Btn) == HIGH) // Activity detected wasn't Zeropwr or Accel_Btn
  {
    Decelerate();   // default = (A' & Z')
  }
}

void Accelerate()

  if (speed_idx < 5)
  {
    speed_idx ++;
  }
  newLevel = Level[speed_idx];
  //Display_it();
}

void Decelerate()
{
  if (speed_idx >= 1)
  {
    speed_idx --;
  }
  newLevel = Level[speed_idx];
  //Display_it();
}



time two events
/* 
   two PB switches, two LEDs
   push a button and its associated LED lights for 30 seconds
   pushing the button during the timing sequence cancels the operation
   (only for the one, not the other)
   pushing either PB lockouts out both for the debounce time (400msec)
   possibly dial back on that for more responsivity 
*/

//  pb_timer_2pb_01
// 
//  2 buttons with cancel option
// 
//  looks like it works 2017.07.02
//

unsigned long markTime1;
unsigned long markTime2;
unsigned long elapsedTime;
unsigned long lockoutmark;  // pb-related
const unsigned long debounceTime = 400; // debounceTime

const byte T1 = 8;
const byte T2 = 11;
//const byte T3 = 10;
//const byte T4 = 11;

const unsigned long testTime = 30000;  // 30 sec.

boolean lockout;  // if true then PB doesn't get checked
boolean tripped;  // pb hit, prep Update for new pointers etc.

const byte pb1pin = 2;
byte pb1state;
boolean engaged1;
const byte pb2pin = 3;
byte pb2state;
boolean engaged2;

void setup ()
{
  //Serial.begin(9600);
  pinMode(pb1pin,INPUT_PULLUP);
  pinMode(pb2pin,INPUT_PULLUP);

  pinMode(T1, OUTPUT);
  pinMode(T2, OUTPUT);
  //pinMode(T3, OUTPUT);
  //pinMode(T4, OUTPUT);   
 
  lockout = false;
  tripped = true;

  engaged1 = false;
  engaged2 = false;

  markTime1 = millis();
  markTime2 = millis();
}

void loop ()
{
  elapsedTime = millis(); 
  if(lockout == false)  // ok to read
  {
    pb1state = digitalRead(pb1pin);
    pb2state = digitalRead(pb2pin);
    if (pb1state == 0)  // pb made active
    {
      lockoutmark = millis();  // record millis when pb went LOW
      lockout = true;
      tripped = true;
      if (engaged1 == true)
      {
        digitalWrite(T1, LOW);
        engaged1 = false;
      }
      else
      {
        digitalWrite(T1, HIGH);
        markTime1 = millis();
        engaged1 = true;
      }
    }
    if (pb2state == 0)  // pb made active
    {
      lockoutmark = millis();  // record millis when pb went LOW
      lockout = true;
      tripped = true;
      if (engaged2 == true)
      {
        digitalWrite(T2, LOW);
        engaged2 = false;
      }
      else
      {
        digitalWrite(T2, HIGH);
        markTime2 = millis();
        engaged2 = true;
      }
    }
  }
// ------------------------------------------------- 
  elapsedTime = millis();
  if ((lockout == true) && ((elapsedTime - lockoutmark) >= debounceTime))
  {
    lockout = false; // lockout done, threshold met
    tripped = false;
  }
// ----------------------------------------------------
  if (engaged1 == true)
  {
    elapsedTime = millis();
    if((elapsedTime - markTime1) >= testTime) // determine whether an Update() is due
    {
      digitalWrite(T1,LOW);
      engaged1 = false;
    }
  }

  if (engaged2 == true)
  {
    elapsedTime = millis();
    if((elapsedTime - markTime2) >= testTime) // determine whether an Update() is due
    {
      digitalWrite(T2,LOW);
      engaged2 = false;
    }
  }



time four events
(as above, expanded to 4 stations

//  pb_timer_4pb_01
// 
//  GO FOR 4 !!
//  looks like it works 2017.07.02 20h42
//

unsigned long markTime1;
unsigned long markTime2;
unsigned long markTime3;
unsigned long markTime4;

unsigned long elapsedTime;
unsigned long lockoutmark;  // pb-related
const unsigned long debounceTime = 400; // debounceTime

const byte T1 = 8;
const byte T2 = 9;
const byte T3 = 10;
const byte T4 = 11;

const unsigned long testTime = 30000;  // 30 sec.

boolean lockout;  // if true then PB doesn't get checked
boolean tripped;  // pb hit, prep Update for new pointers etc.

const byte pb1pin = 2;
byte pb1state;
boolean engaged1;

const byte pb2pin = 3;
byte pb2state;
boolean engaged2;

const byte pb3pin = 4;
byte pb3state;
boolean engaged3;

const byte pb4pin = 5;
byte pb4state;
boolean engaged4;

// ------------------------------------
void setup ()
{
  //Serial.begin(9600);
  pinMode(pb1pin,INPUT_PULLUP);
  pinMode(pb2pin,INPUT_PULLUP);
  pinMode(pb3pin,INPUT_PULLUP);
  pinMode(pb4pin,INPUT_PULLUP);

  pinMode(T1, OUTPUT);
  pinMode(T2, OUTPUT);
  pinMode(T3, OUTPUT);
  pinMode(T4, OUTPUT);   
 
  lockout = false;
  tripped = true;

  engaged1 = false;
  engaged2 = false;
  engaged3 = false;
  engaged4 = false;

  markTime1 = millis();
  markTime2 = millis();
  markTime3 = millis();
  markTime4 = millis();
}

void loop ()
{
  elapsedTime = millis(); 
  if(lockout == false)  // ok to read
  {
    pb1state = digitalRead(pb1pin);
    pb2state = digitalRead(pb2pin);
    pb3state = digitalRead(pb3pin);
    pb4state = digitalRead(pb4pin);
   
    if ((pb1state == 0) || (pb2state == 0) || (pb3state == 0) || (pb4state == 0))  // a pb was activated
    {
      lockoutmark = millis();  // record millis when a PB went LOW
      lockout = true;
      tripped = true;

      if (pb1state == 0)
      {
        if (engaged1 == true)
        {
          digitalWrite(T1, LOW);
          engaged1 = false;
        }
        else
        {
          digitalWrite(T1, HIGH);
          markTime1 = millis();
          engaged1 = true;
        }
      }
      if (pb2state == 0)
      {
        if (engaged2 == true)
        {
          digitalWrite(T2, LOW);
          engaged2 = false;
        }
        else
        {
          digitalWrite(T2, HIGH);
          markTime2 = millis();
          engaged2 = true;
        }
      }
   
      if (pb3state == 0)
      {
        if (engaged3 == true)
        {
          digitalWrite(T3, LOW);
          engaged3 = false;
        }
        else
        {
          digitalWrite(T3, HIGH);
          markTime3 = millis();
          engaged3 = true;
        }    
      }

      if (pb4state == 0)
      {
        if (engaged4 == true)
        {
          digitalWrite(T4, LOW);
          engaged4 = false;
        }
        else
        {
          digitalWrite(T4, HIGH);
          markTime4 = millis();
          engaged4 = true;
        }
      }   
    }
  }
 
  // -------------------------------------------------
   
  elapsedTime = millis();
  if ((lockout == true) && ((elapsedTime - lockoutmark) >= debounceTime))
  {
    lockout = false; // lockout done, threshold met
    tripped = false;
  }
  // ----------------------------------------------------
  if (engaged1 == true)
  {
    elapsedTime = millis();
    if((elapsedTime - markTime1) >= testTime) // is it soup yet?
    {
      digitalWrite(T1,LOW);
      engaged1 = false;
    }
  }

  if (engaged2 == true)
  {
    elapsedTime = millis();
    if((elapsedTime - markTime2) >= testTime) // is it soup yet?
    {
      digitalWrite(T2,LOW);
      engaged2 = false;
    }
  }

  if (engaged3 == true)
  {
    elapsedTime = millis();
    if((elapsedTime - markTime3) >= testTime) // is it soup yet?
    {
      digitalWrite(T3,LOW);
      engaged3 = false;
    }
  }

  if (engaged4 == true)
  {
    elapsedTime = millis();
    if((elapsedTime - markTime4) >= testTime) // is it soup yet?
    {
      digitalWrite(T4,LOW);
      engaged4 = false;
    }
  }
}
 

No comments:

Post a Comment