Lots o' changes. Basically working as desired.
This commit is contained in:
155
src/main.cpp
155
src/main.cpp
@ -4,16 +4,22 @@
|
|||||||
#include <Servo.h>
|
#include <Servo.h>
|
||||||
#include <arduino-timer.h>
|
#include <arduino-timer.h>
|
||||||
|
|
||||||
|
const int LCD_TIMER=2500;
|
||||||
|
/*
|
||||||
|
TODO:
|
||||||
|
- Test buttons
|
||||||
|
- Non-blocking "pulse" function
|
||||||
|
*/
|
||||||
|
|
||||||
// Various variables.
|
// Various variables.
|
||||||
int iDeflection = 48;
|
int iDeflection = 48;
|
||||||
int iDelay = 500;
|
int iDelay = 500;
|
||||||
int iSpeed = 5;
|
int iHoldDelay = 500;
|
||||||
bool bRunning = false;
|
bool bRunning = false;
|
||||||
|
|
||||||
// LCD setup
|
// LCD setup
|
||||||
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
|
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
|
||||||
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
|
int iDisplayCycle = 0;
|
||||||
int displayCycle = 0;
|
|
||||||
|
|
||||||
// Keypad setup.
|
// Keypad setup.
|
||||||
const byte KEYPAD_ROWS = 2;
|
const byte KEYPAD_ROWS = 2;
|
||||||
@ -31,6 +37,8 @@ int servoPosition = 0;
|
|||||||
|
|
||||||
// Timer setup
|
// Timer setup
|
||||||
auto timer = timer_create_default(); // create a timer with default settings
|
auto timer = timer_create_default(); // create a timer with default settings
|
||||||
|
uintptr_t timerClickTask = 0;
|
||||||
|
uintptr_t lcdTimerTask = 0;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Shows the state of affairs to the serial interface.
|
** Shows the state of affairs to the serial interface.
|
||||||
@ -54,11 +62,11 @@ bool printSerialSetting(void *)
|
|||||||
Serial.print("Delay (2/5): ");
|
Serial.print("Delay (2/5): ");
|
||||||
Serial.println(iDelay);
|
Serial.println(iDelay);
|
||||||
|
|
||||||
Serial.print("Speed (3/6): ");
|
Serial.print("HoldDelay (3/6):");
|
||||||
Serial.println(iSpeed);
|
Serial.println(iHoldDelay);
|
||||||
|
|
||||||
Serial.print("Total cycle time (2 x (Speed * Deflection)) + Delay): ");
|
Serial.print("Total cycle time iHoldDelay + Delay): ");
|
||||||
Serial.println((iSpeed * iDeflection) + 15 + (iSpeed * iDeflection) + iDelay);
|
Serial.println(iHoldDelay + iDelay);
|
||||||
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
@ -73,7 +81,7 @@ bool printSerialSetting(void *)
|
|||||||
bool printLCDSettings(void *)
|
bool printLCDSettings(void *)
|
||||||
{
|
{
|
||||||
lcd.clear();
|
lcd.clear();
|
||||||
switch (displayCycle % 4)
|
switch (iDisplayCycle % 5)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
lcd.setCursor(0, 0);
|
lcd.setCursor(0, 0);
|
||||||
@ -84,9 +92,9 @@ bool printLCDSettings(void *)
|
|||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
lcd.setCursor(0, 0);
|
lcd.setCursor(0, 0);
|
||||||
lcd.print("Push Speed (2,5)");
|
lcd.print("Hold Delay (2,5)");
|
||||||
lcd.setCursor(0, 1);
|
lcd.setCursor(0, 1);
|
||||||
lcd.print(iSpeed);
|
lcd.print(iHoldDelay);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
@ -100,31 +108,33 @@ bool printLCDSettings(void *)
|
|||||||
lcd.setCursor(0, 0);
|
lcd.setCursor(0, 0);
|
||||||
lcd.print("Total Cycle: ");
|
lcd.print("Total Cycle: ");
|
||||||
lcd.setCursor(0, 1);
|
lcd.setCursor(0, 1);
|
||||||
lcd.print((iSpeed * iDeflection) + 15 + (iSpeed * iDeflection) + iDelay);
|
lcd.print(iHoldDelay + iDelay);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
lcd.setCursor(0, 0);
|
||||||
|
lcd.print("A start/stop.");
|
||||||
|
if (!bRunning)
|
||||||
|
{
|
||||||
|
lcd.setCursor(0, 1);
|
||||||
|
lcd.print("B to test");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
displayCycle++;
|
iDisplayCycle++;
|
||||||
|
|
||||||
return (true);
|
return (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Actually press the button.
|
** Actually press the button.
|
||||||
** TODO: Make this non-blockig.
|
|
||||||
*/
|
*/
|
||||||
void doPulse()
|
bool doPulse(void *)
|
||||||
{
|
{
|
||||||
for (int i = 0; i <= iDeflection; i++)
|
mainServo.write(iDeflection);
|
||||||
{
|
delay(iHoldDelay);
|
||||||
mainServo.write(i);
|
mainServo.write(servoPosition);
|
||||||
delay(iSpeed);
|
|
||||||
}
|
return (true);
|
||||||
delay(15);
|
|
||||||
for (int i = iDeflection; i > servoPosition; i--)
|
|
||||||
{
|
|
||||||
mainServo.write(i);
|
|
||||||
delay(iSpeed);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -146,10 +156,10 @@ void setup()
|
|||||||
lcd.setCursor(0, 0);
|
lcd.setCursor(0, 0);
|
||||||
lcd.print("ACNH AutoClicker");
|
lcd.print("ACNH AutoClicker");
|
||||||
|
|
||||||
displayCycle = 0;
|
iDisplayCycle = 0;
|
||||||
|
|
||||||
timer.every(10000, printSerialSetting);
|
timer.every(10000, printSerialSetting);
|
||||||
timer.every(2500, printLCDSettings);
|
lcdTimerTask = timer.every(LCD_TIMER, printLCDSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -157,41 +167,72 @@ void setup()
|
|||||||
*/
|
*/
|
||||||
void parseInput(char c)
|
void parseInput(char c)
|
||||||
{
|
{
|
||||||
|
if (!bRunning)
|
||||||
|
{
|
||||||
|
switch (c)
|
||||||
|
{
|
||||||
|
case '1':
|
||||||
|
iDeflection++;
|
||||||
|
iDisplayCycle = 0;
|
||||||
|
break;
|
||||||
|
case '4':
|
||||||
|
iDeflection--;
|
||||||
|
iDisplayCycle = 0;
|
||||||
|
break;
|
||||||
|
case '2':
|
||||||
|
iHoldDelay += 50;
|
||||||
|
iDisplayCycle = 1;
|
||||||
|
break;
|
||||||
|
case '5':
|
||||||
|
iHoldDelay -= 50;
|
||||||
|
iDisplayCycle = 1;
|
||||||
|
break;
|
||||||
|
case '3':
|
||||||
|
iDelay += 50;
|
||||||
|
iDisplayCycle = 2;
|
||||||
|
break;
|
||||||
|
case '6':
|
||||||
|
iDelay -= 50;
|
||||||
|
iDisplayCycle = 2;
|
||||||
|
break;
|
||||||
|
case 'B':
|
||||||
|
doPulse(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
case '1':
|
case 'A':
|
||||||
iDeflection++;
|
if (bRunning)
|
||||||
displayCycle = 0;
|
{
|
||||||
break;
|
bRunning = false;
|
||||||
case '4':
|
if (timerClickTask != 0)
|
||||||
iDeflection--;
|
{
|
||||||
displayCycle = 0;
|
timer.cancel(timerClickTask);
|
||||||
break;
|
timerClickTask = 0;
|
||||||
case '2':
|
}
|
||||||
iSpeed++;
|
}
|
||||||
displayCycle = 1;
|
else
|
||||||
break;
|
{
|
||||||
case '5':
|
bRunning = true;
|
||||||
iSpeed--;
|
timerClickTask = timer.every(iDelay + iHoldDelay, doPulse);
|
||||||
displayCycle = 1;
|
}
|
||||||
break;
|
|
||||||
case '3':
|
|
||||||
iDelay += 10;
|
|
||||||
displayCycle = 2;
|
|
||||||
break;
|
|
||||||
case '6':
|
|
||||||
iDelay -= 10;
|
|
||||||
displayCycle = 3;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the values we've set are within sane limtes.
|
// Make sure the values we've set are within sane limtes.
|
||||||
iDeflection = constrain(iDeflection, 0, 180);
|
iDeflection = constrain(iDeflection, 0, 180);
|
||||||
iSpeed = constrain(iSpeed, 1, 1000);
|
iHoldDelay = constrain(iHoldDelay, 200, 10000);
|
||||||
iDelay = constrain(iDelay, 10, 10000);
|
iDelay = constrain(iDelay, iHoldDelay, 10000);
|
||||||
|
|
||||||
// Update the LCD now. This is cheesed a bit by setting the displaycycle in the switch statement above. Sometimes this is a bit weird if the timer event triggers immediately after pressing a number on the keypad.
|
// Update the LCD now. This is cheesed a bit by setting the displaycycle in the switch statement above. Sometimes this is a bit weird if the timer event triggers immediately after pressing a number on the keypad.
|
||||||
printLCDSettings(0);
|
if (!bRunning)
|
||||||
|
{
|
||||||
|
timer.cancel(lcdTimerTask);
|
||||||
|
printLCDSettings(0);
|
||||||
|
lcdTimerTask = timer.every(LCD_TIMER, printLCDSettings);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
@ -264,16 +305,16 @@ void loop()
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'f':
|
case 'f':
|
||||||
iSpeed++;
|
iHoldDelay++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'v':
|
case 'v':
|
||||||
iSpeed--;
|
iHoldDelay--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
iDeflection = constrain(iDeflection, 0, 180);
|
iDeflection = constrain(iDeflection, 0, 180);
|
||||||
iDelay = constrain(iDelay, 10, 1000);
|
iDelay = constrain(iDelay, 10, 1000);
|
||||||
iSpeed = constrain(iSpeed, 0, 100);
|
iHoldDelay = constrain(iHoldDelay, 0, 100);
|
||||||
|
|
||||||
printSetting();
|
printSetting();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user