Files
LattePanda-WDT-RFSwitch/Arduino-WDT/Arduino-WDT.ino
2024-05-10 09:47:39 -04:00

132 lines
3.4 KiB
C++

const int statePin = 3; // Connect LattePanda's S3 pin to Arduino's D3
const int resetPin = 4; // Connect LattePanda's RST pin to Arduino's D4
const int D10 = 10;
const int D11 = 11;
const int D12 = 12;
bool watchdogActive = false;
unsigned long lastReceivedTime;
unsigned long currentTime;
bool lastLEDState = LOW;
unsigned long lastLEDTime;
unsigned long currentLEDTime;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite (LED_BUILTIN, LOW);
lastLEDTime = millis ();
pinMode(statePin, INPUT); // Set as input to read the state pin's level
pinMode(resetPin, INPUT); // By default, the RST pin on LattePanda is 3.3V. Initialize Arduino pin as INPUT to match this level.
pinMode(D10, OUTPUT);
pinMode(D11, OUTPUT);
pinMode(D12, OUTPUT);
Serial.begin(9600);
}
void loop()
{
//-- Check to see if it's time to change the LED state.
currentLEDTime = millis ();
if (currentLEDTime > lastLEDTime) //-- Check for millis overflow
{
if (currentLEDTime - lastLEDTime > 1000U)
{
if (lastLEDState == LOW)
{
digitalWrite (LED_BUILTIN, HIGH);
lastLEDState = HIGH;
}
else
{
digitalWrite (LED_BUILTIN, LOW);
lastLEDState = LOW;
}
lastLEDTime = millis ();
}
}
else
lastLEDTime = currentLEDTime;
// Check if the watchdog should be activated
if (digitalRead (statePin) == HIGH)
{
if (Serial.available () > 0)
{
char receivedChar = Serial.read ();
if (receivedChar == 'A')
{
watchdogActive = true;
lastReceivedTime = millis();
}
else if (receivedChar == 'Q')
{
watchdogActive = false;
}
else if (receivedChar == '0')
{
digitalWrite (D10, LOW);
digitalWrite (D11, LOW);
digitalWrite (D12, LOW);
}
else if (receivedChar == '1')
{
digitalWrite (D10, LOW);
digitalWrite (D11, LOW);
digitalWrite (D12, LOW);
digitalWrite (D10, HIGH);
}
else if (receivedChar == '2')
{
digitalWrite (D10, LOW);
digitalWrite (D11, LOW);
digitalWrite (D12, LOW);
digitalWrite (D11, HIGH);
}
else if (receivedChar == '3')
{
digitalWrite (D10, LOW);
digitalWrite (D11, LOW);
digitalWrite (D12, LOW);
digitalWrite (D10, HIGH);
digitalWrite (D11, HIGH);
}
else if (receivedChar == '4')
{
digitalWrite (D10, LOW);
digitalWrite (D11, LOW);
digitalWrite (D12, LOW);
digitalWrite (D12, HIGH);
}
}
}
else
{
watchdogActive = false;
}
// If watchdog is active and 's' character isn't received within 1 minute, reboot LattePanda board
currentTime = millis ();
if (currentTime > lastReceivedTime) //-- Check for millis overflow and wait for the next cycle if it has
{
if (watchdogActive && (currentTime - lastReceivedTime > 60000U))
{
pinMode(resetPin,OUTPUT); // Set pin to OUTPUT mode
digitalWrite(resetPin, LOW); // Pull down the RST pin to reboot the LattePanda board
delay(50); // Maintain a LOW level for 50ms
pinMode(resetPin, INPUT); // Set pin back to INPUT mode to match default 3.3V level of LattePanda's RST pin
watchdogActive = false; // Stop the watchdog function after rebooting the LattePanda board
}
}
}