Being in the beginning of a new project I would like to use enum instead of numerical codes but don't find enum in the reference.
Code:
#include<arduino.h>
//I2C for LCD
#include <AccelStepper.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
hd44780_I2Cexp mylcd; // declare lcd object: auto locate & config exapander chip
// LCD geometry
#define LCD_COLS 16
#define LCD_ROWS 2
boolean state, old_state;
byte mode = 0; //// 0 = Antal sidor, 1 = vinkel per steg
int faces = 4;
unsigned long nrOfStepsPerRev = 90L * 200 * 4; // microstep 4
#define cmd {increas,decrease,Go,Chg}
void setup() {
Serial.begin(115200);
//1Hz 90% dutycycle
pinMode(9, OUTPUT); // Set digital pin 9 (D9) to an output
TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11); // Enable the PWM output OC1A on digital pins 9 and invert output
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12); // Set fast PWM and prescaler of 256 on timer 1
ICR1 = 62499; // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
OCR1A = 6249; // Set the duty-cycle to 10%: 62499 / 10 = 6249
pinMode(10, OUTPUT); // 1 Hz pulse output on digital pin 9 (D9)
pinMode(13, OUTPUT); digitalWrite(13, LOW);// Make board LED go off
delay(10);//allow pwm timers to start
int status;
status = mylcd.begin(LCD_COLS, LCD_ROWS);
if (status) // non zero status means it was unsuccesful
{
status = -status; // convert negative status value to positive number
// begin() failed so blink error code using the onboard LED if possible
hd44780::fatalError(status); // does not return
}
mylcd.clear();
// Print start message to the LCD
mylcd.print("221101a Rotating");
delay(5000);
mylcd.print("Running");
delay(1000);
}
cmd readButtons(void)
{
if ((digitalIncreaseButton) == 0)
return (increase);
if ((digitalDecreaseButton) == 0)
return (decrease);
if ((digitalGoButton) == 0)
return (Go);
if ((digitalChgButton) == 0)
return (Go);
return (nill);
}
// The loop function is called in an endless loop
void loop()
{
boolean cmd = readButtons();// + - Go Chg mode
if (( millis() % 1000 ) > 499 )
{
state = true;
}
else
{
state = false;
}
if ( state) { //update twice per second;
if ( mode == 0) //number of surfaces
{
switch (cmd)
{
case increase:
{
faces++;
brake;
}
case decrease:
{
faces--;
if (faces < 1) faces = 1; //1 rotate one turn
brake;
}
case Chg:
{
mode = 1; //switch to angle mode
// goFlag = false;//
brake;
}
case Go:
{
AccelStepper(anglePos);
anglePos += nrOfStepsPerRev * 360 / faces;
brake;
}
default:
brake;
}
if (goFlag)
{
AccelStepper(anglePos);
}
}
else if ( state == 1 ) // Angle per step
{
}
}
}// End of loop
Unrelated, but if you have hd44780 library version 1.0.2 (released 2019-05-21) or newer, you no longer have to invert the return status from mylcd.begin() before you pass it to fatalError()
Haven't checked what version I use but it's an "old friend" and I just copy the main parts from project to project. Thanks anyway.
Just checked. It's Version-0.9.3-........
You may want to consider upgrading your hd44780 library.
There have been several updates & fixes to the hd44780 and hd44780_I2Cexp code since 0.9.3
Current release is 1.3.2
The library ships with documentation which includes a change log in the README file.
It can be accessed from the IDE GUI using the "Documentation" sketch and then clicking on the desired information.
You can also access it from the main github page here:
If you scroll down you can see the release date and change log for every revision of the library.