Arduino countdown with relay and pot

Odgovori
Message
Autoru
Korisnikov avatar
žežino
Početnik
Postovi: 744
Pridružio se: Pon Okt 09, 2017 8:25 pm
Lokacija: Sremska Mitrovica

Arduino countdown with relay and pot

#1 Post od žežino » Pet Maj 24, 2019 8:18 pm

Nasao sam ovaj kod koji je sa jednim releom meni treba sa 4 da se pali redom jedan posle drugog po isteku vremena ako moze neka pomoc

Kod: Obeleži sve

// start of settings for LCD1602 with I2C
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// end of settings for LCD1602 with I2C

#define relayPin 8 // the pin relay is connected
#define potPin  A0 // the pin potentiometer is connected
#define resetPin 3 // the pin where rest switch is connected
#define startPin 2 // the pin where start switch is connected

#define relayType 'L' // Relay trigger type. L for low and H for high
const int maxTime = 200;// maximum timer time in seconds
const int minTime = 2; // miniimum timer time in seconds

// do not change anything bellow here
long duration;// holds the duration of timer
int potValue;// holds the variable resistor (potentiometer) value
long rememTime;// holds current run time
int relayState =0;// holds relay state

void setup() {
  // initialize the LCD
  lcd.begin();
  lcd.backlight();// turn the backlight ON for the LCD
    
  pinMode(relayPin, OUTPUT);// define relayPin  as output
  pinMode(resetPin, INPUT_PULLUP);// define input pin with pullup (watch video)
  pinMode(startPin, INPUT_PULLUP);// define input pin with pullup (watch video)
  attachInterrupt(digitalPinToInterrupt(resetPin), reset, LOW);// Interrupt Service Routines. "reset" is a function in this code
    
  Serial.begin(9600);// initialize serial monitor with 9600 baud
  if(relayType == 'L')
  {
    digitalWrite(relayPin, HIGH);// turn the relay OFF and keep it OFF
      lcd.print("Robojax Relay1.1");
      lcd.setCursor(0,1);
      lcd.print("Type:LOW Trigger");     
      Serial.println("Robojax Relay 1.1"); 
      Serial.println("Type:LOW Trigger");      
  }else{
    digitalWrite(relayPin, LOW);// turn the relay OFF and keep it OFF
      lcd.print("Robojax Relay1.1");
      lcd.setCursor(0,1);
      lcd.print("Type:H Trigger");     
      Serial.println("Robojax Relay 1.1"); 
      Serial.println("Type:HIGH Trigger");  
  } 
  delay(3000);// change this line if you want not to wait
  lcd.clear();// clearn previous values from screen
}

void loop() {
  // Robojax.com Relay Timer V 1.1 with LCD1602 I2C display
    potValue = analogRead(potPin)/10;// reads the value of the potentiometer (value between 0 and 1023)

    int durationCalc = map(potValue, 0, 102, minTime, maxTime);// convert A0 value to time set at minTime and maxTime
    
    if(digitalRead(startPin) ==LOW)
    {
     duration = durationCalc;
     rememTime = millis()/1000;
     relayState = 1;
     controlRelay();// send command to turn the relay ON
           
    }
   if(  ( millis()/1000- rememTime) > duration )
   {
    relayState = 0;// change relay state to OFF
    controlRelay();// control relay with new relay state
    
   }
      Serial.print("Time set: ");
      Serial.print(duration);
      Serial.print (" S ");  
 
            
    if(relayState ==1){  

      lcdDisplay(0, "Time Set: ", duration, "S"); 
      lcdDisplay(1, "Remaining: ", getRtime(), "S");         
      Serial.print(" remain: ");
      Serial.print(getRtime());//
      Serial.print(" S");  
    }else{
      lcdDisplay(0, "Time Set: ", durationCalc, "S"); 
      lcdDisplay(1, "Relay OFF          ", 0, " ");         
    }
    Serial.println();  
          
 delay(50); // wait for 200 milliseconds       
}// loop end


/*
 * 
 * @brief Turns the relay ON or OFF 
 * @param none
 * @return no return value
 */

 void controlRelay()
 {
  // Robojax.com Relay Timer V 1.1 with LCD1602 I2C display
  if(relayType == 'L')
  {
     if(relayState == 1)
     {
    digitalWrite(relayPin, LOW);// turns Low-Trigger relay ON
      Serial.print("LT-Relay ON for ");
      Serial.print(duration);// display in seconds
      Serial.println(" Seconds");
     }else{
    digitalWrite(relayPin, HIGH); // turns Low-Trigger relay OFF
      Serial.println("====Relay is OFF");
     }
      
  }else{
    // Robojax.com Relay Timer V 1.1 with LCD1602 I2C display
     if(relayState == 1)
     {    
      digitalWrite(relayPin, HIGH);// turns High-Trigger relay ON
      Serial.print("HT-Relay ON for ");
      Serial.print(duration);// display in seconds
      Serial.println(" Seconds");  
     }else{
      digitalWrite(relayPin, LOW); // turns High-Trigger relay OFF
      Serial.println("==Relay OFF");  
     }  
  }

}//controlRelay end


/*
 * 
 * @brief resets the timer and turns OFF relay
 * @param none
 * @return no return value
 */

void reset()
{
  // Robojax.com Relay Timer V 1.1 with LCD1602 I2C display
  duration =0;
   if(relayType == 'L')
  {
    digitalWrite(relayPin, HIGH);// turn OFF High trigger relay     
  }else{
    digitalWrite(relayPin, LOW);// turn OFF Low trigger relay
  
  } 
  Serial.println("Relay OFF"); 

}//reset()


/*
 * 
 * @brief returns the remaining time set 
 * @param none
 * @return the remaining time as integet second
 */
int getRtime()
{
  // Robojax.com Relay Timer V 1.1 with LCD1602 I2C display
  return duration - (millis()/1000- rememTime);
}

/*
 * lcdDisplay(int tc, int tr, String title, int vc, int vr, float int)
  for example to display
 * tc  is character number  (0)
 * tr is row in the lcd (1)
 * title is the text (Voltage:)
 * vcn character number for value 
 * vr row number for the value
 * value is the value (13.56)
 * 
 * @brief prints text on the screen on specific row and character
 * @param tc=text character number, trc=text row nubmer, 
 * @return the remaining time as integet second
 * usage example for example 
 * to print "Time Set: 34s" on line 2
 * titleText = Time Set:
 * valueText = 34
 * value2Text = S. print 's" as second
 * type this:
 * lcdDisplay(0,"Time Set: ",  34,   "S");
 * 
 */
void lcdDisplay(int rowNum, String titleText, int valueText, String value2Text)
{
    clearRow(rowNum);
    String myStr;
    myStr = String(valueText);

    int titleTextLength = titleText.length();
   // Robojax.com Relay Timer V 1.1 with LCD1602 I2C display
   lcd.setCursor (0,rowNum); //
   lcd.print(titleText);
   
   lcd.setCursor (titleTextLength,rowNum); //
   lcd.print(myStr);
   
   lcd.setCursor (myStr.length()+titleTextLength,rowNum); //
   lcd.print(value2Text);   
 
}

/*
 * 
 * @brief clears only one row of display
 * @param r, the row number integer
 * @return no return value
 */
void clearRow(int r)
{
  //
  for(int i=0; i<16; i++)
  {
   lcd.setCursor (i,r); //
   lcd.print(" ");
  }
}//clearRow end
KO ZNA TAJ I UME

Korisnikov avatar
zokce
Početnik
Postovi: 952
Pridružio se: Sre Okt 18, 2017 7:57 pm
Lokacija: Juzna Srbija

Re: Arduino countdown with relay and pot

#2 Post od zokce » Sub Maj 25, 2019 11:33 am

Ako ces bez ispisa na ekran, onda je malo jednostavnije. Ako treba da ispisuje, onda je malo komplikovanije.
Ne bih da ti nudim laznu nadu, mozda se angazujem ovih dana. Koliko vidim ovo je pretabaceni ili Fortran ili Pascal, sa vrlo neznatnim izmenama.

Jos nesto, da li vreme treba da bude razlicito za svaki relej ili isto, i da li treba da postoji i kolika treba da bude pauza izmedju iskljucenja prethodnog i ukljucenja narednog releja?
Baci link gde si ovo nasao, mozda ima neceg korisnog u tekstu, a pominje se i neki video.
Без старца нема ударца!

Korisnikov avatar
žežino
Početnik
Postovi: 744
Pridružio se: Pon Okt 09, 2017 8:25 pm
Lokacija: Sremska Mitrovica

Re: Arduino countdown with relay and pot

#3 Post od žežino » Sub Maj 25, 2019 12:16 pm

Treba da se vidi na lcdu, i vreme izmedju dva relea je nebitno moze i 3 sec mada cak ni nemora,https://www.youtube.com/watch?v=BhEIFzFSMuE
sve ostaje isto samo da se umnozi i da se pale redom jedan za drugim, ja cu probati pa se javim , mislio sam ako neko ima ideju da nelutam puno.Sto se tice relea svaki posebno treba da se steluje potenciometrom.
KO ZNA TAJ I UME

Korisnikov avatar
zokce
Početnik
Postovi: 952
Pridružio se: Sre Okt 18, 2017 7:57 pm
Lokacija: Juzna Srbija

Re: Arduino countdown with relay and pot

#4 Post od zokce » Sub Maj 25, 2019 4:50 pm

Treba da stavis petlju sa odbrojavanjem (unazad ili unapred, svejedno) i da redni broj ponavljanja povezes sa brojem odredjenog releja. Znaci da ce trebati 4 izlaza iz arduina, a to znaci da ces morati na pocetku da to definises. Eto ti ideja.
Без старца нема ударца!

Korisnikov avatar
žežino
Početnik
Postovi: 744
Pridružio se: Pon Okt 09, 2017 8:25 pm
Lokacija: Sremska Mitrovica

Re: Arduino countdown with relay and pot

#5 Post od žežino » Ned Maj 26, 2019 9:57 am

ok hvala
KO ZNA TAJ I UME

Odgovori

Ko je OnLine

Korisnika u ovom forumu: Nema registrovanih korisnika i 1 gost