Browse Source

save status values in EEPROM

master
gituser 10 months ago
parent
commit
2361b13e8b
4 changed files with 55 additions and 11 deletions
  1. 3
    0
      .cproject
  2. 5
    0
      .project
  3. 45
    10
      FSRemotePowerSwitch.ino
  4. 2
    1
      sloeber.ino.cpp

+ 3
- 0
.cproject View File

@@ -23,6 +23,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/libraries/AccelStepper/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/libraries/ArduinoJson/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/core/variant}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/libraries/EEPROM/src}&quot;"/>
</option>
<inputType id="io.sloeber.compiler.cpp.sketch.input.1496076099" name="CPP source files" superClass="io.sloeber.compiler.cpp.sketch.input"/>
</tool>
@@ -32,6 +33,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/libraries/AccelStepper/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/libraries/ArduinoJson/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/core/variant}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/libraries/EEPROM/src}&quot;"/>
</option>
<inputType id="io.sloeber.compiler.c.sketch.input.280688484" name="C Source Files" superClass="io.sloeber.compiler.c.sketch.input"/>
</tool>
@@ -41,6 +43,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/libraries/AccelStepper/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/libraries/ArduinoJson/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/core/variant}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/FSRemotePowerSwitch/libraries/EEPROM/src}&quot;"/>
</option>
<inputType id="io.sloeber.compiler.S.sketch.input.288275921" name="Assembly source files" superClass="io.sloeber.compiler.S.sketch.input"/>
</tool>

+ 5
- 0
.project View File

@@ -51,5 +51,10 @@
<type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/libraries/ArduinoJson/7.0.2</locationURI>
</link>
<link>
<name>libraries/EEPROM</name>
<type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/arduino/hardware/avr/1.8.4/libraries/EEPROM</locationURI>
</link>
</linkedResources>
</projectDescription>

+ 45
- 10
FSRemotePowerSwitch.ino View File

@@ -14,6 +14,7 @@
#include <AccelStepper.h>
#include <HardwareSerial.h>
#include <math.h>
#include <EEPROM.h>

const char *Version = "V1.3.0";

@@ -39,8 +40,14 @@ const String CMD_VAL_OFF = "Off";
const int PWR_RELAY_PIN = 5;
const int PWR_CAM_PIN = 6;

const int eepromAddrStatusRelay = 100;
const int eepromAddrStatusCam = 200;
const int eepromAddrStatusCamVel = 300;
const int eepromAddrActAzimut = 400;

JsonDocument msg;
String stepperVelocity;
int actAzimut;

// Define motor interface type
#define motorInterfaceType AccelStepper::MotorInterfaceType::HALF4WIRE
@@ -54,11 +61,32 @@ void setup() {
pinMode(PWR_RELAY_PIN, OUTPUT);
pinMode(PWR_CAM_PIN, OUTPUT);

setSlowStepperVelocity(); // Slow: Acc: 500 / Fast: MaxSpeed: 1000
// Slow: Acc: 200 / Fast: MaxSpeed: 500
int velocity = EEPROM.read(eepromAddrStatusCamVel);
if (velocity == 0) {
setSlowStepperVelocity(); // Slow: Acc: 200 / Slow: MaxSpeed: 500
} else {
setFastStepperVelocity(); // Fast: Acc: 500 / Fast: MaxSpeed: 1000
}

int powerRel = EEPROM.read(eepromAddrStatusRelay);
if (powerRel == 0) {
digitalWrite(PWR_RELAY_PIN, LOW);
} else {
digitalWrite(PWR_RELAY_PIN, HIGH);
}

int camRel = EEPROM.read(eepromAddrStatusCam);
if (powerRel == 0) {
digitalWrite(PWR_CAM_PIN, LOW);
} else {
digitalWrite(PWR_CAM_PIN, HIGH);
}

digitalWrite(PWR_RELAY_PIN, HIGH);
digitalWrite(PWR_CAM_PIN, HIGH);
actAzimut = EEPROM.read(eepromAddrActAzimut);
if (actAzimut > 180) {
actAzimut = 0;
}
stepper.setCurrentPosition(round((4076.0 / 360.0) * ((double) actAzimut) * -1));
}

/* The loop function runs over and and checks the serial port for receiving a Json of following format:
@@ -109,9 +137,11 @@ void loop() {
if (digitalRead(PWR_RELAY_PIN) == LOW) {
moveStepper();
sendRelayPowerStatus(CMD_VAL_ON);
EEPROM.write(eepromAddrStatusRelay, 0);
} else {
moveStepper();
sendRelayPowerStatus(CMD_VAL_OFF);
EEPROM.write(eepromAddrStatusRelay, 1);
}

} else if (sdr == CMD_SENDER_PC && cmd == CMD_STATUS_RELAY) {
@@ -137,9 +167,11 @@ void loop() {
if (digitalRead(PWR_CAM_PIN) == LOW) {
moveStepper();
sendCamPowerStatus(CMD_VAL_ON);
EEPROM.write(eepromAddrStatusCam, 0);
} else {
moveStepper();
sendCamPowerStatus(CMD_VAL_OFF);
EEPROM.write(eepromAddrStatusCam, 1);
}

} else if (sdr == CMD_SENDER_PC && cmd == CMD_STATUS_CAM) {
@@ -152,10 +184,6 @@ void loop() {
sendCamPowerStatus(CMD_VAL_OFF);
}

} else if (sdr == CMD_SENDER_PC && cmd == CMD_ZERO_CAM) {
stepper.setCurrentPosition(0);
sendZeroCamStatus(String(stepper.currentPosition()));

} else if (sdr == CMD_SENDER_PC && cmd == CMD_VELOCITY_CAM) {
if (val.equals(CMD_VELOCITY_FAST)) {
setFastStepperVelocity();
@@ -167,6 +195,11 @@ void loop() {
} else if (sdr == CMD_SENDER_PC && cmd == CMD_STATUS_VELOCITY_CAM) {
sendCamVelocityStatus();

} else if (sdr == CMD_SENDER_PC && cmd == CMD_ZERO_CAM) {
stepper.setCurrentPosition(0);
sendZeroCamStatus(String(stepper.currentPosition()));
EEPROM.write(eepromAddrActAzimut, 0);

} else if (sdr == CMD_SENDER_PC && cmd == CMD_ACT_AZIMUT_CAM) {
int actAzi = round(stepper.currentPosition() / (4076.0 / 360.0) * -1);
sendCamActAzimut(String(actAzi));
@@ -176,6 +209,7 @@ void loop() {
long target = round((4076.0 / 360.0) * val.toDouble() * -1);
stepper.moveTo(target);
stepper.run();
EEPROM.write(eepromAddrActAzimut, val.toInt());
}
}

@@ -210,6 +244,7 @@ void setFastStepperVelocity() {
stepper.setMaxSpeed(1000); // Slow: 500 / Fast: 1000
stepper.setAcceleration(500); // Slow: 200 / Fast: 500
stepper.setSpeed(1000);
EEPROM.write(eepromAddrStatusCamVel, 1);
}

void setSlowStepperVelocity() {
@@ -218,9 +253,9 @@ void setSlowStepperVelocity() {
stepper.setMaxSpeed(500); // Slow: 500 / Fast: 1000
stepper.setAcceleration(200); // Slow: 200 / Fast: 500
stepper.setSpeed(500);
EEPROM.write(eepromAddrStatusCamVel, 0);
}


void sendVersion() {
msg["Sender"] = CMD_SENDER_AN;
msg["Cmd"] = CMD_VER;
@@ -263,7 +298,7 @@ void sendCamActAzimut(String val) {

void sendCamVelocityStatus() {
msg["Sender"] = CMD_SENDER_AN;
msg["Cmd"] = CMD_VELOCITY_CAM;
msg["Cmd"] = CMD_STATUS_VELOCITY_CAM;
msg["Val"] = stepperVelocity;

serializeJson(msg, Serial);

+ 2
- 1
sloeber.ino.cpp View File

@@ -2,7 +2,7 @@
//This is a automatic generated file
//Please do not modify this file
//If you touch this file your change will be overwritten during the next build
//This file has been generated on 2025-01-22 22:57:08
//This file has been generated on 2025-01-23 16:55:38

#include "Arduino.h"
#include "Arduino.h"
@@ -10,6 +10,7 @@
#include <AccelStepper.h>
#include <HardwareSerial.h>
#include <math.h>
#include <EEPROM.h>

void setup() ;
void loop() ;

Loading…
Cancel
Save