// https://wokwi.com/projects/469909085937513473
// https://forum.arduino.cc/t/help-with-a-build-and-programming/1452412
/*----------------------------------------------------
Dome Steurung via Button oder RCCIASCOM
Engelbert Vollmer
2025 02 02 nach Anschluss der Motoren
*/
String serialin; //incoming serial data
String str; //store the state of opened/closed/pins
//relays
#define close 2 // PIN_BUTTON_DOWN
#define open 3 // PIN_BUTTON_UP
//input sensors
#define openedUS 4 // roof open sensor UppSegOpened needs to be inverted for consistency
#define closedUS 5 // roof closed sensor UppSegClosed
#define openedLS 7 // roof open sensor LowSegOpened also see hack
#define closedLS 6 // roof closed sensor LowSegClosed
int motor1pin1 = 10;
int motor1pin2 = 11;
int motor2pin1 = 13; //... these were flipped, too lazy to fix the wiring mistake
int motor2pin2 = 12; //...
String StatusUS;
String StatusLS;
//... begin hack
byte myDigitalRead(int pin) {
if (pin == openedUS || pin == openedLS) return (digitalRead(pin) == LOW ? HIGH : LOW);
return digitalRead(pin);
}
# define digitalRead myDigitalRead
//... end hack. sue me
void setup() {
//Begin Serial Comunication(configured for 9600baud)
Serial.begin(9600);
//pin relay as OUTPUT
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
//pins as INPUT
pinMode(open, INPUT); // these are pulled down externally
pinMode(close, INPUT); // to make the logic work as intended pressed reads HIGH
pinMode(closedUS, INPUT_PULLUP);
pinMode(openedUS, INPUT_PULLUP);
pinMode(closedLS, INPUT_PULLUP);
pinMode(openedLS, INPUT_PULLUP);
//Initialize Output states
//... digitalWrite(open, LOW); this is a mystery, why turn off the pullups you just turned on?
//... digitalWrite(close, LOW);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
Serial.write("RRCI#"); //init string
}
void loop() {
Operate_Dome_By_Button(); // blocks, so
Operate_Dome_by_RCCI();
// lamp motor proxies
// lampTest();
// UI and limit switch test
// Print_Schalter_Status();
delay(20); // gack! poor man's global debounce
}
//---------------------------------------------------------------
// Operate by Button
//---------------------------------------------------------------
void Operate_Dome_By_Button() {
// --------------- Close Dome ----------------
if (digitalRead(close) == HIGH && digitalRead(open) == LOW) {
Serial.println("asking to close");
//Both Segments
while (digitalRead(close) == HIGH && digitalRead(closedUS) == LOW && digitalRead(closedLS) == LOW) {
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
}
Stopp_Motors();
//Upper Segment if Lower Segment is closed before
while (digitalRead(close) == HIGH && digitalRead(closedUS) == LOW) {
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
}
Stopp_MotorsUS();
//Lower Segment if Upper Segment is closed before
while (digitalRead(close) == HIGH && digitalRead(closedLS) == LOW) {
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
}
Stopp_MotorsLS();
}
// --------------- Open Dome --------------------------
if (digitalRead(open) == HIGH && digitalRead(close) == LOW) {
Serial.println("asking to open");
//Both Segments
while (digitalRead(open) == HIGH && digitalRead(openedUS) == HIGH && digitalRead(openedLS) == HIGH) {
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
}
Stopp_Motors();
//Upper Segment if Lower Segment is opened before
while (digitalRead(open) == HIGH && digitalRead(openedUS) == HIGH) {
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
}
Stopp_MotorsUS();
//Lower Segment if Upper Segment is opened before
while (digitalRead(open) == HIGH && digitalRead(openedLS) == HIGH) {
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
}
Stopp_MotorsLS();
}
}
//----------------------------------------------------------------------------
// Operate via ASCOM
//----------------------------------------------------------------------------
void Operate_Dome_by_RCCI() {
//Verify connection by serial
if (Serial.available() > 0) {
//Read Serial data and alocate on serialin
serialin = Serial.readStringUntil('#');
if (serialin != "") {
Serial.print(" serial command ");
Serial.println(serialin);
}
if (serialin == "y") {
Stopp_Motors();
}
if (serialin == "open") {
digitalWrite(motor1pin1, LOW); //Upper Segment
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW); // Lower Segmenet
digitalWrite(motor2pin2, HIGH);
StatusUS = "opening";
StatusLS = "opening";
}
if (serialin == "close") {
digitalWrite(motor1pin1, HIGH); //Upper Segment
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH); // Lower Segmene
digitalWrite(motor2pin2, LOW);
StatusUS = "closing";
StatusLS = "closing";
}
}
if (digitalRead(closedUS) == HIGH && StatusUS == "closing") { Stopp_MotorsUS(); }
if (digitalRead(closedLS) == HIGH && StatusLS == "closing") { Stopp_MotorsLS(); }
if (digitalRead(openedUS) == LOW && StatusUS == "opening") { Stopp_MotorsUS(); }
if (digitalRead(openedLS) == LOW && StatusLS == "opening") { Stopp_MotorsLS(); }
if (serialin == "Parkstatus") { // external query command to fetch RRCI data
Serial.println("0#");
serialin = "";
}
if (serialin == "get") { // external query command to fetch RRCI data - Two Pipelines(||) to make a boolean OR Comparission
if ((digitalRead(closedUS) == HIGH && digitalRead(openedUS) == HIGH) && (digitalRead(closedLS) == HIGH && digitalRead(openedLS) == HIGH)) { str += "AA closed,not_moving_o#"; }
if ((digitalRead(closedUS) == LOW && digitalRead(openedUS) == LOW) || (digitalRead(closedLS) == LOW && digitalRead(openedLS) == LOW)) { str += "BB opened,not_moving_c#"; }
if ((digitalRead(openedUS) == LOW && digitalRead(closedUS) == LOW) && (digitalRead(openedLS) == LOW && digitalRead(closedLS) == LOW)) {str += "CC opened,not_moving_c#";}
if ((digitalRead(closedUS) == HIGH && digitalRead(openedUS) == HIGH) || (digitalRead(closedLS) == HIGH && digitalRead(openedLS) == HIGH)) {str += "DD - WTF?";}
if (str == "") {
str = "no satisfied condition";
Serial.print("closedUS "); Serial.println(digitalRead(closedUS));
Serial.print("openedUS "); Serial.println(digitalRead(openedUS));
Serial.print("closedLS "); Serial.println(digitalRead(closedLS));
Serial.print("openedLS "); Serial.println(digitalRead(openedLS));
Serial.println("");
}
Serial.println(str); //send serial data
serialin = "";
str = "";
}
if (serialin == "Status") { Serial.println("RoofOpen#"); }
serialin = "";
//str = "";
}
//--------------------------
// Motor stop
//--------------------------
void Stopp_Motors() {
Stopp_MotorsUS();
Stopp_MotorsLS();
}
void Stopp_MotorsUS() {
Serial.println("stop UPPER segment motor"); //... make it talk
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
StatusUS = "";
}
void Stopp_MotorsLS() {
Serial.println("stop LOWER segment motor"); //... make it talk
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
StatusLS = "";
}
//--------------------------
// Status of Button during tests
//--------------------------
void Print_Schalter_Status()
{
static uint8_t previous = 0xFF; // Force first print.
uint8_t state = (PIND >> 2) & 0x3F; // D2..D7 -> bits 0..5
if (state == previous)
return;
previous = state;
Serial.print(F("UC UO LC LO CB OB 0x"));
if (state < 16)
Serial.print('0');
Serial.println(state, HEX);
for (int bit = 0; bit < 6; bit++) {
Serial.print((state >> bit) & 1);
Serial.print(" ");
}
Serial.println();
Serial.println();
}
void Print_Schalter_Status0() {
Serial.print("UpSegCl UpSeg Op LoSegCl LoSegOp ButCl ButOp...");
Serial.print(digitalRead(closedUS));
Serial.print("..");
Serial.print(digitalRead(openedUS));
Serial.print("..");
Serial.print(digitalRead(closedLS));
Serial.print("..");
Serial.print(digitalRead(openedLS));
Serial.print("..");
Serial.print(digitalRead(close));
Serial.print("..");
Serial.println(digitalRead(open));
delay(200);
}
//...
/*
motor lamp test
*/
void lampTest()
{
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
delay(250);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
delay(250);
}
/*
//... helper for upside down inconsistencies
const auto PRESSED = LOW;
const auto RELEASED = HIGH;
*/
/*
action buttons close when pressed, and read LOW when
limit switches open when hit, and read HIGH when, I've wired the wokwi switches using the
contacts that are closed when you crea the switch, left and middle
:: reset switches and rest UNO (wokwi flaw?)
so at reset+reset, the limit switches should not impede motoring
*/
.LIMITS.
..OPEN..
when hit
dome closed
lower
.half
upper
.half
dome opened
switches are to the left
in between limits
like this one