Hallo,
Ik zou graag onderstaande sketch willen indelen in verschillende tabbladen. Dus een hoofdtab en dan aparte onderdelen zodat ik een beter overzicht krijg van wat wat is. Kan/wil er mij op weg helpen? Hartelijk bedankt.
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
// Wiring: SDA pin is connected to A4 and SCL pin to A5.
// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // for 16x2 LCD.
const int motor1pin1 = 11;
const int motor1pin2 = 12;
const int startStopButtonPin = 4;
const int modeButtonPin = 5;
const int selectButtonPin = 6;
const int upButtonPin = 7;
const int downButtonPin = 8;
const int enaPin = 9;
const byte interruptPin = 2;
const int SPEC_SUMMARY = 0;
const int WINDINGS_EDIT = 1;
const int DIRECTION_EDIT = 2;
const int AUTO_STOP_EDIT = 3;
const int RUN_MODE = 42;
const int SPEC_MODE = 84;
const int CLOCKWISE = 1;
const int ANTI_CLOCKWISE = 2;
boolean runMode = true;
boolean shouldBeRunning = false;
boolean shouldRedraw = true;
boolean clockwise = true;
boolean autoStop = true;
unsigned long desiredWindingCount = 4500;
volatile unsigned long totalRevolutions = 0;
int currentSpecSelection = 0;
struct ButtonModeStruct {
int pin;
int buttonState;
int lastButtonState = LOW;
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 75; // the debounce time; increase if the output flickers
};
typedef struct ButtonModeStruct ButtonMode;
ButtonMode startStopButton;
ButtonMode modeButton;
ButtonMode selectButton;
ButtonMode upButton;
ButtonMode downButton;
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
// These two govern direction
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(enaPin, OUTPUT); //PWM for averaged voltage, therefore motor speed
pinMode(startStopButtonPin, INPUT);
startStopButton.pin = startStopButtonPin;
modeButton.pin = modeButtonPin;
selectButton.pin = selectButtonPin;
upButton.pin = upButtonPin;
downButton.pin = downButtonPin;
attachInterrupt(digitalPinToInterrupt(interruptPin), count, RISING);
}
void loop() {
checkModeButton(&modeButton);
if (runMode) {
currentSpecSelection = 0;
checkStartStopButton();
renderRevCount();
if (shouldBeRunning) {
startMotor();
} else {
stopMotor();
}
if (shouldRedraw) {
renderRunModeDisplay();
shouldRedraw = false;
}
// Use this for reset when in run mode
checkDownButton(&downButton);
} else {
stopMotor();
totalRevolutions = 0;
checkSelectButton(&selectButton);
checkUpButton(&upButton);
checkDownButton(&downButton);
if (shouldRedraw) {
renderSpecModeDisplay();
shouldRedraw = false;
}
}
delay(1);
}
void renderRevCount() {
lcd.setCursor(9, 1);
lcd.print("#:");
lcd.print(totalRevolutions);
lcd.print(" ");
}
void count() {
totalRevolutions++;
if (autoStop) {
if (totalRevolutions > desiredWindingCount) {
shouldBeRunning = false;
totalRevolutions = 0;
}
}
}
void renderAutoStop() {
//autostop
lcd.setCursor(10, 0);
lcd.print("Auto:");
if (autoStop) {
lcd.print("Y");
} else {
lcd.print("N");
}
}
void renderCount() {
//count
lcd.setCursor(0, 1);
lcd.print("No.:");
lcd.setCursor(4, 1);
lcd.print(" ");
lcd.setCursor(4, 1);
lcd.print(desiredWindingCount);
}
void renderDirection() {
//direction
lcd.setCursor(10, 1);
lcd.print("Dir:");
if (clockwise) {
lcd.print("CW");
} else {
lcd.print("CC");
}
}
void renderSpecModeLabel() {
lcd.setCursor(0, 0);
lcd.print("Mode:SPEC ");
}
void renderEditModeLabel() {
lcd.setCursor(0, 0);
lcd.print("Mode:EDIT ");
}
void renderSpecModeDisplay() {
if (currentSpecSelection == SPEC_SUMMARY) {
lcd.clear();
renderSpecModeLabel();
renderAutoStop();
renderCount();
renderDirection();
}
if (currentSpecSelection == WINDINGS_EDIT) {
lcd.clear();
renderEditModeLabel();
renderCount();
}
if (currentSpecSelection == DIRECTION_EDIT) {
lcd.clear();
renderEditModeLabel();
renderDirection();
}
if (currentSpecSelection == AUTO_STOP_EDIT) {
lcd.clear();
renderEditModeLabel();
renderAutoStop();
}
}
void renderRunModeDisplay() {
lcd.setCursor(0, 0);
lcd.print("Mode:RUN ");
renderCount();
}
void checkSelectButton(ButtonMode* button) {
// read the state of the switch into a local variable:
int reading = digitalRead(button->pin);
// If the switch changed, due to noise or pressing:
if (reading != button->lastButtonState) {
// reset the debouncing timer
button->lastDebounceTime = millis();
}
if ((millis() - button->lastDebounceTime) > button->debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != button->buttonState) {
button->buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (button->buttonState == HIGH) {
if (currentSpecSelection == 4) {
currentSpecSelection = 0;
} else {
currentSpecSelection = currentSpecSelection + 1;
}
shouldRedraw = true;
}
}
}
// save the reading. Next time through the loop, it'll be the lastStartStopButtonState:
button->lastButtonState = reading;
}
void handleUpButton() {
if (currentSpecSelection == WINDINGS_EDIT) {
desiredWindingCount = desiredWindingCount + 100;
if (desiredWindingCount < 100) {
desiredWindingCount = 100;
}
}
if (currentSpecSelection == DIRECTION_EDIT) {
clockwise = true;
}
if (currentSpecSelection == AUTO_STOP_EDIT) {
autoStop = true;
}
}
void handleDownButton() {
if (currentSpecSelection == WINDINGS_EDIT) {
desiredWindingCount = desiredWindingCount - 100;
if (desiredWindingCount < 100) {
desiredWindingCount = 100;
}
}
if (currentSpecSelection == DIRECTION_EDIT) {
clockwise = false;
}
if (currentSpecSelection == AUTO_STOP_EDIT) {
autoStop = false;
}
if (runMode) {
totalRevolutions = 0;
}
}
void checkDownButton(ButtonMode* button) {
// read the state of the switch into a local variable:
int reading = digitalRead(button->pin);
// If the switch changed, due to noise or pressing:
if (reading != button->lastButtonState) {
// reset the debouncing timer
button->lastDebounceTime = millis();
}
if ((millis() - button->lastDebounceTime) > button->debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != button->buttonState) {
button->buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (button->buttonState == HIGH) {
handleDownButton();
shouldRedraw = true;
}
}
}
// save the reading. Next time through the loop, it'll be the lastStartStopButtonState:
button->lastButtonState = reading;
}
void checkUpButton(ButtonMode* button) {
// read the state of the switch into a local variable:
int reading = digitalRead(button->pin);
// If the switch changed, due to noise or pressing:
if (reading != button->lastButtonState) {
// reset the debouncing timer
button->lastDebounceTime = millis();
}
if ((millis() - button->lastDebounceTime) > button->debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != button->buttonState) {
button->buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (button->buttonState == HIGH) {
handleUpButton();
shouldRedraw = true;
}
}
}
// save the reading. Next time through the loop, it'll be the lastStartStopButtonState:
button->lastButtonState = reading;
}
void checkModeButton(ButtonMode* button) {
// read the state of the switch into a local variable:
int reading = digitalRead(button->pin);
// If the switch changed, due to noise or pressing:
if (reading != button->lastButtonState) {
// reset the debouncing timer
button->lastDebounceTime = millis();
}
if ((millis() - button->lastDebounceTime) > button->debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != button->buttonState) {
button->buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (button->buttonState == HIGH) {
runMode = !runMode;
shouldRedraw = true;
if (runMode) {
Serial.println("run");
} else {
Serial.println("spec");
}
}
}
}
// save the reading. Next time through the loop, it'll be the lastStartStopButtonState:
button->lastButtonState = reading;
}
void checkStartStopButton() {
// read the state of the switch into a local variable:
int reading = digitalRead(startStopButton.pin);
// If the switch changed, due to noise or pressing:
if (reading != startStopButton.lastButtonState) {
// reset the debouncing timer
startStopButton.lastDebounceTime = millis();
}
if ((millis() - startStopButton.lastDebounceTime) > startStopButton.debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != startStopButton.buttonState) {
startStopButton.buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (startStopButton.buttonState == HIGH) {
shouldBeRunning = !shouldBeRunning;
}
}
}
// save the reading. Next time through the loop, it'll be the lastStartStopButtonState:
startStopButton.lastButtonState = reading;
}
void startMotor() {
if (clockwise) {
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
} else {
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
}
analogWrite(enaPin, 255);
}
void stopMotor() {
analogWrite(enaPin, 0);
}