/* jbutton nottubj */

# include "jbutton.h"  // check debounce time
# include <ezButton.h>

# include <Adafruit_NeoPixel.h>

ezButton ezA(A0);

jButton pA;
jButton pB;

# define NINE       9
# define LED_PIN    8
# define LED_COUNT  NINE

Adafruit_NeoPixel disaply(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);


char shotCounter;
const unsigned long runTime = 3000L;
const byte runFactor = 3;

unsigned long now;    // always the time each loop
unsigned long aTimer;

bool running;

# define stabLED  10
# define STAB 333

unsigned char myReadPFP(unsigned char xx)
{
  return digitalRead(xx) == LOW ? 0 : 1;
}

void setup()
{
  mySetup(115200);
  xprintf("\nhello world.\n"); 

  pA.begin(A1);
  pB.begin(A2, myReadPFP);

  ezA.setDebounceTime(20);

  pinMode(10, OUTPUT);
  digitalWrite(stabLED, LOW);

  pinMode(11, OUTPUT);
  digitalWrite(11, LOW);

  disaply.begin();
  disaply.setPixelColor(1, 0xff00ff);
  disaply.show();
  delay(333);

  shotCounter = 9;
}

void loop()
{
  now = millis();

  pA.update();
  pB.update();

  ezA.loop();

  bool ezAPressed = ezA.isPressed();
  bool pAPressed = pA.uolPress();

if (pB.uolPress()) Serial.println("        I saw that.");

// blink per event
  static unsigned long stabStart;
  if (ezAPressed) {
    stabStart = now;
    digitalWrite(stabLED, HIGH);
  }
  if (now - stabStart >= STAB) digitalWrite(stabLED, LOW); 

// rule 0
  if (pAPressed || ezAPressed)
    shotCounter++;

// rule 1
  if (!running && shotCounter >= runFactor) {
      xprintf("turning on now\n");
      digitalWrite(11, HIGH);
      running = true;

      aTimer = now;
  }

// rule 2
  if (running && now - aTimer > runTime) {
    shotCounter -= runFactor; 
    if (shotCounter <= 0) {
      xprintf("turning off now\n");
      digitalWrite(11, LOW);
      shotCounter = 0;
      running = false;
    }
    else
      aTimer = now;
  }

//
  static int lastCount = -1;
  if (lastCount != shotCounter) {
    xprintf("and shotz = %d\n", shotCounter);
    lastCount = shotCounter;
  }

// separate redundat motor output section
  static bool motorControl;
  if (running && !motorControl) {
    Serial.println("Start the ");
    motorControl = true;
  }
  if (!running && motorControl) {
    Serial.println(" Stop the ");
    motorControl = false;
  }
}




// 

// programmer misses printf...

void xprintf(const char *format, ...)
{
  char buffer[256];
  va_list args;
  va_start(args, format);
  vsprintf(buffer, format, args);
  va_end(args);
  Serial.print(buffer);
}

// programmer forgets day of week, version

void mySetup(unsigned long bandRate)
{
  Serial.begin(bandRate);
   
  char s[] = __FILE__;
  byte b = sizeof(s);
  while ( (b > 0) && (s[b] != 47)) b--;
  char *u = s + b + 1;
 

  xprintf("%s  %s\n\n", __DATE__, u);

  xprintf("!\n");
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
led2:A
led2:C
neopixels1:GND
neopixels1:VCC
neopixels1:DIN
neopixels1:DOUT
led3:A
led3:C
AAA
LOGICAL .MOTOR
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
ezA
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r