# include <ezButton.h>
# include <Stepper.h>
// sorry, just all the text so include it over in the ino.
# define DEBOUNCE 15 // resample lockout period
struct jButton {
unsigned char thePin;
unsigned char iPress, iRelease;
unsigned char raw;
unsigned char state;
unsigned char stable;
unsigned long lastTime;
unsigned int lookNumber;
// unsigned char thisPin = 99; // catch no such pin ever
unsigned char thisLength; // unused now, hard wired to 4
/*
JButton(unsigned char onPin, unsigned char length = 4) {
thePin = onPin;
}
void spill()
{
Serial.print(thisPin); Serial.print(" pin and ");
Serial.print(thisLength); Serial.print(" length.");
Serial.println("");
}
*/
void begin(byte wahtPin)
{
thePin = wahtPin;
pinMode(thePin, INPUT_PULLUP);
}
void updaxe() {
unsigned long now = millis();
if (now - lastTime < DEBOUNCE)
return; // too soon to even look
raw = !digitalRead(thePin); // 1 = pressed, button pulled up.
// printf("look number %d\n", lookNumber);
lookNumber++;
//printf("%d", raw);
if (raw == 1) {
//printf(" %d\n", state);
if (state == 0)
iPress = 1;
if (state == 1)
stable = 1;
state = 1;
}
else { // raw == 0
//printf(" %d\n", state);
if (state == 1)
iRelease = 1;
if (state == 0)
stable = 0;
state = 0;
}
lastTime = now;
}
}; // jButton;
jButton screenOnOff, bumpIt;
void testJButton() {
for (; ; ) {
screenOnOff.updaxe();
bumpIt.updaxe();
if (screenOnOff.iPress) {
Serial.println("I could toggle the OLED state!");
screenOnOff.iPress = 0;
}
if (bumpIt.iPress) {
Serial.println("I could advance the local state!");
bumpIt.iPress = 0;
}
}
}
const int stepsPerRevolution = 150; // Change this number to change how far stepper turns
const int stepsPerRevolution2 = 450;
Stepper myStepper = Stepper(stepsPerRevolution, 2, 4, 3, 5);
// Setup NRF24L01 communication
//RF24 radio(9, 10); // CE, CSN
// const byte address[6] = "00001";
jButton received, oneOfFour;
void setup() {
myStepper.setSpeed(55);
Serial.begin(115200);
Serial.println("wake up!\n");
// received.setDebounceTime(20);
// oneOfFour.setDebounceTime(20);
received.begin(6);
oneOfFour.begin(7);
/* Initialize NRF24L01
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_HIGH);
radio.setChannel(108); // Set a specific channel to avoid interference
radio.setDataRate(RF24_250KBPS); // Lower data rate for better reliability
radio.startListening();
*/
}
void loop() {
// Serial.print("loop..."); when it didn't even loop
received.updaxe();
oneOfFour.updaxe();
// Serial.println(" ...pool"); delay(1000 - 777);
if (received.isPress) {
static int counter;
Serial.print(counter); counter++;
Serial.print(". I see new data in! ");
int message[4];
Serial.println("And ");
// for (int ii = 0; ii = 3; ii++) message[ii] = LOW; OMG you idiot
for (int ii = 0; ii < 4; ii++) message[ii] = LOW;
Serial.println(" so ");
message[1] = oneOfFour.state;
// radio.read(&message, sizeof(message));
if (1) // chnage to if (0) to not print the message buffer
for (int ii = 0; ii < 4; ii++) {
Serial.print(message[ii] == LOW ? " L" : " H");
}
Serial.println(" . ");
// Check button states and move stepper accordingly
if (message[0] == HIGH) {
Serial.println("clockwise");
//... myStepper.step(stepsPerRevolution);
Serial.println(" clockwise");
}
if (message[1] == HIGH) {
Serial.println("counterclockwise");
// myStepper.step(-stepsPerRevolution);
delay(1000);
Serial.println(" counterclockwise");
}
if (message[2] == HIGH) {
Serial.println("clockwisetriple");
//... myStepper.step(stepsPerRevolution2);
}
if (message[3] == HIGH) {
Serial.println("counterclockwisetriple");
//... myStepper.step(-stepsPerRevolution2);
}
// Reset motor pins to LOW after movement
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
// delay(250); // Delay to ensure smooth operation
// radio.startListening();
}
}
/*
// jButtons are use or lose, we only get one press event true, so can't do this and the other thing
if (0) { // change to if (1) just to test the ezButtons
if (received.iPress) {
Serial.println(" XX new data in!\n");
}
if (oneOfFour.isPressed()) {
Serial.println("XX data is now pressed\n");
}
if (oneOfFour.isReleased()) {
Serial.println("XX data is now released\n");
}
// return;
}
*/