Friday, December 31, 2021

Pool automation project


I started a new home project. I needed a motor with low RPM and high torque, to pull on both sides. I added two buttons to run the motor in either side instantaneously and another two to push till a certain time limit. Then I added a wait time before the trigger. While the project is still under construction, here is the arduino code, a crude circuit diagram and a demo of the motor run only.

It took me about 30 minutes to write this code. While I am certain it can be further optimized and improved, my energy is is better spent on other things. Considering it's going to run on a single chip, I am going to leave it as is for now.

it's a good example of what kind of code goes in embedded systems. lines of code grow exponentially as the conditions increase.










Update April-16-2022 : I ran the test and everything worked fine until the end.

Phase 1- connect ropes to allow one person to pull the cover without bunching up.


Phase 2 - connect a motor to reduce manual winding/unwinding.


Everything went well until the end, the reel wheel crumbled under pressure, literally. Now either I find a better way and use the other good end to connect the motor, or I purchase a heavy duty reel since a 16mil cover is too heavy for any reel to pull.










From left to right, Power supply, relay bank, Arduino, beeper, and an RC circuit to absorb transient surges.





Update: Dec-17-2022-vlog below





Final update - April 22nd-2023

The system is fully installed and functional. Videos below.

Introduction:


Opening the pool:


Closing the pool:




Arduino code below -April 16-2022:



#define relay1 2

#define relay2 3

#define relay3 4

#define beeper 5


#define button1 A1

#define button2 A2

#define button3 A3

#define button4 A4


bool button1state;

bool button2state;

bool button3state;

bool button4state;


bool readystate;


bool poolopenstate = false;


int beepercount = 0;


int button1value;

int button2value;

int button3value;

int button4value;


int runningstate = 0;


unsigned long runningtime = 0;


unsigned long fullruntimeopen = 10000;

unsigned long fullruntimeclose = 10000;


unsigned long waittimeopen = 2000;

unsigned long waittimeclose = 2000;


unsigned long timestamp1 = 0;


void setup() {

  Serial.begin (9600);

  delay(100);

  pinMode(relay1, OUTPUT);

  pinMode(relay2, OUTPUT);

  pinMode(relay3, OUTPUT);

  pinMode(beeper, OUTPUT);

  

  pinMode(button1, INPUT);

  pinMode(button2, INPUT);

  pinMode(button3, INPUT);

  pinMode(button4, INPUT);

  

  button1value = analogRead(button1);

  button2value = analogRead(button2);

  button3value = analogRead(button3);

  button4value = analogRead(button4);


  if((button1value<20)||(button1value<20)||(button1value<20)||(button1value<20))

  {

    readystate = false; 

    Serial.println("not ready");

  }

  else

  {

    readystate = true;

    Serial.println("ready");

  }

  timestamp1 = millis();

 

}




void loop() {

beepercount++;


switch(beepercount){

  case 0:

    break;

  case 1:

    digitalWrite(beeper, LOW);

    break;

  case 2:

    if (!poolopenstate)

    {

      digitalWrite(beeper, HIGH);

    }

    break;

  case 3:

    break;

  case 4:

    digitalWrite(beeper, HIGH);

    break;

  case 5:

    beepercount = 0;

    break;

}



if(!readystate)

{

  button1value = analogRead(button1);

  button2value = analogRead(button2);

  button3value = analogRead(button3);

  button4value = analogRead(button4);


  if((button1value<20)||(button1value<20)||(button1value<20)||(button1value<20))

  {

    readystate = false; 

  }

  else

  {

    readystate = true;

  }


}

else

{  

  timestamp1=millis();

  

     

  button1value = analogRead(button1);

  button2value = analogRead(button2);

  button3value = analogRead(button3);

  button4value = analogRead(button4);

  

  if(button1value<20){

    button1state = true;

  }

  else{

    button1state = false;

  

  }

  

  if(button2value<20){

    button2state = true;

  

  }

  else{

    button2state = false;

  

  }

  

  if(button3value<20){

    button3state = true;

  }

  else{

    button3state = false;

  

  }

  if(button4value<20){

    button4state = true;

  }

  else{

    button4state = false;

  

  }

  //Serial.println(String(button1value)+"|"+String(button2value)+"|"+String(button3value)+"|"+String(button4value));

  

  //Serial.println(String(button1state)+String(button2state)+String(button3state)+String(button4state)+":"+String(timestamp1)+"|"+String(runningtime)+"|"+String(runningstate));

  

  if((button2state == true) or (button3state == true))

  {

  

    if(button2state==true)

    {

        runningstate = 2;

    }

    else

    {

        runningstate = 3;

    }

  }

  else

  {

    if ((runningstate == 2) or (runningstate == 3))

    {

      runningstate = 0;

    }

  }

  

  

  if(button1state==true)

  {

    if (runningstate != 1)

    {

      runningstate = 1; 

      runningtime= millis();   

    }

    

  }

  

  if(button4state==true)

  {

    if (runningstate != 4)

    {

      runningstate = 4; 

      runningtime= millis();   

    }

    

  }

  

  

  if (runningstate == 0)

  {

    //turn motor off

    digitalWrite(relay1, LOW);

    digitalWrite(relay2, LOW);

    runningtime = 0;

    Serial.println("stopped");

  

  }

  

  if (runningstate == 1)

  {

    //turn motor forward until counter

    if((timestamp1-runningtime) < fullruntimeopen)

    {

      if((timestamp1-runningtime) > waittimeopen)

      {

        digitalWrite(relay1, HIGH);

        digitalWrite(relay2, LOW);    

        Serial.println("forward timer");      

      }

      else

      {

        Serial.println("forward waiting");      

      }

    }

    else

    {

      digitalWrite(relay1, LOW);

      digitalWrite(relay2, LOW);

      runningstate = 0;

      runningtime = 0;

      Serial.println("stopping from forward");

  

    }

  }

  

  if (runningstate == 2)

  {

    //turn motor forward

    digitalWrite(relay1, HIGH);

    digitalWrite(relay2, LOW);

    Serial.println("forward");

  

  }

  

  if (runningstate == 3)

  {

    //turn motor backwards (close)

    digitalWrite(relay1, LOW);

    digitalWrite(relay2, HIGH);

    Serial.println("backwards");

  

  }

  

  if (runningstate == 4)

  {

    //turn motor backwards until counter

    if((timestamp1-runningtime) < fullruntimeclose)

    {

      if((timestamp1-runningtime) > waittimeclose)

      {

        digitalWrite(relay1, LOW);

        digitalWrite(relay2, HIGH);

        Serial.println("backwards timer");        

      }

      else

      {

        Serial.println("backwards-waiting");        

      }

      

    }

    else

    {

      digitalWrite(relay1, LOW);

      digitalWrite(relay2, LOW);

      runningstate = 0;

      runningtime = 0;

      poolopenstate = true;

      Serial.println("stopping from backwards");

  

  

    }

  }

  

  

  

 }

 delay(200);

}


Updated code with BLE receiver on serial port - Dec-17-2022


#define relay1 2

#define relay2 3

#define relay3 4

#define beeper 5


#define button1 A1

#define button2 A2

#define button3 A3

#define button4 A4

byte byteread;

String  Inputdata;


bool button1state;

bool button2state;

bool button3state;

bool button4state;


bool waitingstatus = false;


bool readystate;


bool poolopenstate = false;


int frequency = 2000;

int initfrequency = 2000;


int serialdelay = 0;


int button1value;

int button2value;

int button3value;

int button4value;


int runningstate = 0;


int lowthreshold = 30;


unsigned long  runningtime = 0;


unsigned long fullruntimeopen = 20000; //including waittime

unsigned long fullruntimeclose = 20000; //including waittime


unsigned long waittimeopen = 10000;

unsigned long waittimeclose = 10000;


unsigned long timestamp1 = 0;


void setup() {

  Serial.begin (9600);

  delay(100);

  pinMode(relay1, OUTPUT);

  pinMode(relay2, OUTPUT);

  pinMode(relay3, OUTPUT);

  pinMode(beeper, OUTPUT);

  

  pinMode(button1, INPUT);

  pinMode(button2, INPUT);

  pinMode(button3, INPUT);

  pinMode(button4, INPUT);

  

  button1value = analogRead(button1);

  button2value = analogRead(button2);

  button3value = analogRead(button3);

  button4value = analogRead(button4);


  if((button1value>lowthreshold)||(button2value>lowthreshold)||(button3value>lowthreshold)||(button4value>lowthreshold))

  {

    readystate = false; 

    Serial.println("not ready");

     while( true)

      {

        frequency = frequency * 1.07;

        if (frequency > 4000){

          frequency = initfrequency;

          break;

        }

        tone(beeper, frequency); // Send 1KHz sound signal...   

        delay(10);

      }

      tone(beeper, 0); // Send 1KHz sound signal...   

      delay(100);


      while( true)

      {

        frequency = frequency * 1.07;

        if (frequency > 4000){

          frequency = initfrequency;

          break;

        }

        tone(beeper, frequency); // Send 1KHz sound signal...   

        delay(10);

      }

      tone(beeper, 0); // Send 1KHz sound signal...   

      delay(500);

      

       

       while( true)

      {

        frequency = frequency * 1.01;

        if (frequency > 4000){

          frequency = initfrequency;

          break;

        }

        tone(beeper, frequency); // Send 1KHz sound signal...   

        delay(10);

      }

      tone(beeper, 0); // Send 1KHz sound signal...   

      delay(100);

       while( true)

      {

        frequency = frequency * 1.01;

        if (frequency > 4000){

          frequency = initfrequency;

          break;

        }

        tone(beeper, frequency); // Send 1KHz sound signal...   

        delay(10);

      }

      tone(beeper, 0); // Send 1KHz sound signal...   

      

  }

  else

  {

    readystate = true;

    Serial.println("ready");

  }

  timestamp1 = millis();

 

}




void loop() {


    

    Serial.println(String(button1value)+"|"+String(button2value)+"|"+String(button3value)+"|"+String(button4value));



 while( true)

{

  frequency = frequency * 1.07;

  if (frequency > 4000){

    frequency = initfrequency;

    break;

  }

  tone(beeper, frequency); // Send 1KHz sound signal...   

  delay(10);

}

tone(beeper, 0); // Send 1KHz sound signal...   

delay(100);

while( true)

{

  frequency = frequency * 1.07;

  if (frequency > 4000){

    frequency = initfrequency;

    break;

  }

  tone(beeper, frequency); // Send 1KHz sound signal...   

  delay(10);

}

tone(beeper, 0); // Send 1KHz sound signal...   

delay(100);


if(waitingstatus)

 while( true)

{

  frequency = frequency * 1.01;

  if (frequency > 4000){

    frequency = initfrequency;

    break;

  }

  tone(beeper, frequency); // Send 1KHz sound signal...   

  delay(10);

}

tone(beeper, 0); // Send 1KHz sound signal...   

delay(100);

 while( true)

{

  frequency = frequency * 1.01;

  if (frequency > 4000){

    frequency = initfrequency;

    break;

  }

  tone(beeper, frequency); // Send 1KHz sound signal...   

  delay(10);

}

tone(beeper, 0); // Send 1KHz sound signal...   

delay(1000);


}


if(!readystate)

{

  button1value = analogRead(button1);

  button2value = analogRead(button2);

  button3value = analogRead(button3);

  button4value = analogRead(button4);


  if((button1value>lowthreshold)||(button2value>lowthreshold)||(button3value>lowthreshold)||(button4value>lowthreshold))

  {

    readystate = false; 

    Serial.println("Still not ready");

  }

  else

  {

    readystate = true;

  }


}

else

{  

  

  

     

  button1value = analogRead(button1);

  button2value = analogRead(button2);

  button3value = analogRead(button3);

  button4value = analogRead(button4);

  

  if(button1value>lowthreshold){

    button1state = true;

  }

  else{

    button1state = false;

  

  }

  

  if(button2value>lowthreshold){

    button2state = true;

  

  }

  else{

    button2state = false;

  

  }

  

  if(button3value>lowthreshold){

    button3state = true;

  }

  else{

    button3state = false;

  

  }

  if(button4value>lowthreshold){

    button4state = true;

  }

  else{

    button4state = false;

  

  }


if(Serial.available()) {

  

 // Serial.println("serial"); 

    while(Serial.available()) { 

    //  Serial.println("reading");

    byteread=Serial.read();

    //Serial.print(byteread);

    Inputdata +=byteread;

    delay(10);

    }

    Serial.println(Inputdata);

 if(Inputdata.indexOf("25511120102551112000")>=0){

  Serial.println("up");

  button2state = true;

  serialdelay = 3000;

 }

 if(Inputdata.indexOf("25511120202551112000")>=0){

  Serial.println("down");

  button3state = true;

  serialdelay = 3000;

 }

 if(Inputdata.indexOf("25511120402551112000")>=0){

  Serial.println("up");

  button2state = true;

  serialdelay = 3000;

 }


 if(Inputdata.indexOf("25511120802551112000")>=0){

  Serial.println("down");

  button3state = true;

  serialdelay = 3000;

 }


 

 if(Inputdata.equals("25511121002551112000")){

  Serial.println("Stop");

  button1state = false;

  button2state = false;

  button3state = false;

  button4state = false;

  serialdelay = 0;

 }

 

 Inputdata = "";


}

else{

  serialdelay = 0;

}



  

 // Serial.println(String(button1state)+String(button2state)+String(button3state)+String(button4state)+":"+String(timestamp1)+"|"+String(runningtime)+"|"+String(runningstate));

  

  if((button2state == true) or (button3state == true))

  {

    waitingstatus = false;

    if(button2state==true)

    {

        runningstate = 2;

    }

    else

    {

        runningstate = 3;

    }

  }

  else

  {

    if ((runningstate == 2) or  (runningstate == 3))

    {

      runningstate = 0;

    }

  }

  

  

  if(button1state==true)

  {

    if (runningstate != 1)

    {

      runningstate = 1; 

      runningtime= millis();   

    }

    

  }

  

  if(button4state==true)

  {

    if (runningstate != 4)

    {

      runningstate = 4; 

      runningtime= millis();   

    }

    

  }

  

  timestamp1=millis();

  

  if (runningstate == 0)

  {

    //turn motor off

    digitalWrite(relay1, LOW);

    digitalWrite(relay2, LOW);

    runningtime = 0;

    Serial.println("stopped");

  

  }

  

  if (runningstate == 1)

  {

          

    //turn motor forward until counter

    if((timestamp1-runningtime) < fullruntimeopen)

    {

      if((timestamp1-runningtime) > waittimeopen)

      {

        digitalWrite(relay1, HIGH);

        digitalWrite(relay2, LOW);    

        Serial.println("forward timer");      

        waitingstatus = false;

      }

      else

      {

        Serial.println("forward waiting");   

        waitingstatus = true;   

      }

    }

    else

    {

      digitalWrite(relay1, LOW);

      digitalWrite(relay2, LOW);

      runningstate = 0;

      runningtime = 0;

      Serial.println("stopping from forward");

  

    }

  }

  

  if (runningstate == 2)

  {

    //turn motor forward

    digitalWrite(relay1, HIGH);

    digitalWrite(relay2, LOW);

    Serial.println("forward");

  

  }

  

  if (runningstate == 3)

  {

    //turn motor backwards (close)

    digitalWrite(relay1, LOW);

    digitalWrite(relay2, HIGH);

    Serial.println("backwards");

  

  }

  

  if (runningstate == 4)

  {

    //turn motor backwards until counter

    if((timestamp1-runningtime) < fullruntimeclose)

    {

      if((timestamp1-runningtime) > waittimeclose)

      {

        digitalWrite(relay1, LOW);

        digitalWrite(relay2, HIGH);

        Serial.println("backwards timer");        

        waitingstatus = false;

      }

      else

      {

        Serial.println("backwards-waiting");        

        waitingstatus = true;

      }

      

    }

    else

    {

      digitalWrite(relay1, LOW);

      digitalWrite(relay2, LOW);

      runningstate = 0;

      runningtime = 0;

      poolopenstate = true;

      Serial.println("stopping from backwards");

  

  

    }

  }

  

  

  

 }

 delay(200);

 delay(serialdelay);

}


  


The power surfing project

  01-Apr-2023: Sometime ago I had posted a video of a 400w trolling motor on my paddle board. It's a good option for a leisurely stroll ...