(SOLVED) Solar System voltage switch some relay modules (Problem)

Seeking help to finish my project. I can measure my voltage, but i am not able to switch my relay modules by a specific voltage on and off.
For example switch relay 1 on when voltage is 10v and off by 14v.

This is my existing code :

// Define analog input
#define ANALOG_IN_PIN A5 // Voltage measurment
int RelayControl4 = 4; // K1 not used Digital Arduino Pin used to control the relais
int RelayControl5 = 5; // K2 cuttoff lifepo4 by 12.4volt
int RelayControl6 = 6; // K3 cuttoff solar panel if Battery charge goes higher 14.8v
int RelayControl7 = 7; // K4 extra charge when Battery is low (extra charger extern)

// Floats for ADC voltage & Input voltage
float adc_voltage = 0.0;
float in_voltage = 0.0;

// Floats for resistor values in divider (in ohms)
float R1 = 30000.0;
float R2 = 7500.0;

// Float for Reference Voltage
float ref_voltage = 5.0;

// Integer for ADC value
int adc_value = 0;

void setup() {
// Setup Serial Monitor
Serial.begin(9600);
//-------------------------------------------------------------------------------------------
// pinMode(RelayControl4, OUTPUT);
// pinMode(RelayControl5, OUTPUT);
// pinMode(RelayControl6, OUTPUT);
pinMode(RelayControl4, OUTPUT); // K4
}

void loop() {
// Read the Analog Input
adc_value = analogRead(ANALOG_IN_PIN);

// Determine voltage at ADC input
adc_voltage = (adc_value * ref_voltage) / 1024.0;

// Calculate voltage at divider input
in_voltage = adc_voltage / (R2 / (R1 + R2)) + 0.2; // OFFSET +0.2

// Print results to Serial Monitor to 2 decimal places
// Serial.print("Input Voltage = ");
// Serial.println(in_voltage, 2);
//----------------------------------------------------------------------------------------------
in_voltage >= 13.0; digitalWrite; RelayControl4, HIGH; // NO7 K4 and COM X Connected (LED off)
in_voltage <= 12.0; digitalWrite; RelayControl4, LOW; // NO7 K4 and COM X

Serial.print("in_voltage = ");
Serial.println(in_voltage, 2);

delay(500);
}

The relay goes on but it doesnt change by changing the voltage.

Thanks for any help in adcance

Did you forget an "if"?

Please remember to use code tags when posting code

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

When you posted your code without code tags did you receive a warning message ?

sorry i dont know what code tags means.English is not my language.

i tryed If/else and it didnt work
maybe you can send me a code sample related or edit my code

i tryed :

if (in_voltage >= 13.0); digitalWrite; RelayControl4, HIGH; // NO7 K4 and COM X Connected (LED off)
if (in_voltage <= 12.0); digitalWrite; RelayControl4, LOW; // NO7 K4 and COM X Connected (LED on)

Relay is still active/on 13.4v

Is not the way to do the if thing.

Try this.

C++ with Qt Tutorial: If Then Else Control Structure (alternative-computer-programming.com)

which may help with the if thingy how to do thing.

or this

C++ If ... Else (w3schools.com)

or this

C++ if...else statement (tutorialspoint.com)

Here I properly use an if thingy.


void fReadCurrent( void * parameter )
{
  const TickType_t xFrequency = 1000; //delay for mS
  const float mVperAmp        = 185.0f;
  float    ADbits             = 4096.0f;
  float    ref_voltage        = 3.3f;
  float    mA                 = 0.0f;
  float    adcValue           = 0.0f;
  float    Voltage            = 0.0f;
  float    Power              = 0.0f;
  float    offSET             = 0.0f;
  int      printCount         = 0;
  uint64_t TimePastKalman     = esp_timer_get_time(); // used by the Kalman filter UpdateProcessNoise, time since last kalman calculation
  SimpleKalmanFilter KF_I( 1.0f, 1.0f, .01f );
  /*
     185mv/A = 5 AMP MODULE
     100mv/A = 20 amp module
     66mv/A = 30 amp module
  */
  String powerInfo = "";
  powerInfo.reserve( 150 );
  while ( !MQTTclient.connected() )
  {
    vTaskDelay( 250 );
  }
  TickType_t xLastWakeTime = xTaskGetTickCount();
  for (;;)
  {
    adc1_get_raw(ADC1_CHANNEL_3); // read once discard reading
    adcValue = ( (float)adc1_get_raw(ADC1_CHANNEL_3) );
    //log_i( "adcValue I = %f", adcValue );
    Voltage = ( (adcValue * ref_voltage) / ADbits ) + offSET; // Gets you mV
    mA = Voltage / mVperAmp; // get amps
    KF_I.setProcessNoise( (esp_timer_get_time() - TimePastKalman) / 1000000.0f ); //get time, in microsecods, since last readings
    mA = KF_I.updateEstimate( mA ); // apply simple Kalman filter
    TimePastKalman = esp_timer_get_time(); // time of update complete
    printCount++;
    if ( printCount == 60 )
    {
      xSemaphoreTake( sema_CalculatedVoltage, portMAX_DELAY);
      Power = CalculatedVoltage * mA;
      //log_i( "Voltage=%f mA=%f Power=%f", CalculatedVoltage, mA, Power );
      printCount = 0;
      powerInfo.concat( String(CalculatedVoltage, 2) );
      xSemaphoreGive( sema_CalculatedVoltage );
      powerInfo.concat( ",");
      powerInfo.concat( String(mA, 4) );
      powerInfo.concat( ",");
      powerInfo.concat( String(Power, 4) );
      xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY );
      MQTTclient.publish( topicPower, powerInfo.c_str() );
      xSemaphoreGive( sema_MQTT_KeepAlive );
      powerInfo = "";
    }
    xLastWakeTime = xTaskGetTickCount();
    vTaskDelayUntil( &xLastWakeTime, xFrequency );
  }
  vTaskDelete( NULL );
} //void fReadCurrent( void * parameter )
///

The if statement terminates at the semicolor, so it effectively says "if in_voltage is >= 13.0, do nothing".
digitalWrite; does nothing, it is a reference to the digitalWrite function, but does not call the function because there is no () after it, and even with () it still does nothing, because the pin number and HIGH/LOW must be inside the ().
RelayControl4, HIGH; does nothing, the compiler replaces that with the corresponding numerical values, so you end up with a line of code that has "4, 1;" which does nothing.

The proper syntax would be

if (in_voltage >= 13.0) digitalWrite(RelayControl4, HIGH);

if there is more than one statement after the if, it needs to be in curly brackets

if (in_voltage >= 13.0) {
  digitalWrite(RelayControl4, HIGH);
  //additional lines of code
}

i tryed also :


if (in_voltage >= 13.0);
{
digitalWrite; RelayControl4, HIGH; // NO7 K4 and COM X Connected (LED off)
}
if (in_voltage <= 12.0);
{
digitalWrite; RelayControl4, LOW; // NO7 K4 and COM X Connected (LED on)
}


that doesnt work too. Somehow it should work. your sample is match to high for me.
But anyway i appeciate your try

Code tags -

Either copy your code into a post here then select it all and click the </> icon above the edit window to add the code tags

Even easier, right click in the IDE and click "Copy for forum" and the code, complete with code tags will be put on the clipboard ready to paste here

Using code tags makes it easier to read code here in a scrolling window and to copy it with a single click for examination in an editor

i did the change, but nothing changes :roll_eyes:

if (in_voltage >= 13.0) {

digitalWrite; RelayControl4, HIGH;        

}
if (in_voltage <= 12.0) {

digitalWrite; RelayControl4, LOW;        

}

Read what I posted very carefully, you left out the parenthesis for digitalWrite.

digitalWrite; RelayControl4, HIGH; 

See digitalWrite() - Arduino Reference

Problem solved thank you very match :smiley:

[quote="tolentino2510, post:9, topic:967368"]
if (in_voltage >= 13.0) ; <<<<what's that doing?
[/quote]

What does this if statement have that all other working if statements do not have?

Solved

Would you care to share your fixed sketch for the benefits of others

Seems to work.Final test tomorrow


// Define analog input
#define ANALOG_IN_PIN A5 // Voltage measurment
int RelayControl4 = 4; // K1 not used Digital Arduino Pin used to control the relais
int RelayControl5 = 5; // K2 cuttoff lifepo4 by 12.4volt
int RelayControl6 = 6; // K3 cuttoff solar panel if Battery charge goes higher 14.8v
int RelayControl7 = 7; // K4 extra charge when Battery is low (extra charger extern)

// Floats for ADC voltage & Input voltage
float adc_voltage = 0.0;
float in_voltage = 0.0;

// Floats for resistor values in divider (in ohms)
float R1 = 30000.0;
float R2 = 7500.0;

// Float for Reference Voltage
float ref_voltage = 5.0;

// Integer for ADC value
int adc_value = 0;

void setup() {
// Setup Serial Monitor
Serial.begin(9600);
//-------------------------------------------------------------------------------------------
pinMode(4, OUTPUT); // K4
pinMode(5, OUTPUT); // K3
pinMode(6, OUTPUT); // K2
pinMode(7, OUTPUT); // K1
digitalWrite(4, HIGH); // NO7 K4 and COM X Connected (LED off) by start Arduino
digitalWrite(5, HIGH); // NO7 K3 and COM X Connected (LED off) by start Arduino
digitalWrite(6, HIGH); // NO7 K2 and COM X Connected (LED off) by start Arduino
digitalWrite(7, HIGH); // NO7 K1 and COM X Connected (LED off) by start Arduino
}

void loop() {
// Read the Analog Input
adc_value = analogRead(ANALOG_IN_PIN);

// Determine voltage at ADC input
adc_voltage = (adc_value * ref_voltage) / 1024.0;

// Calculate voltage at divider input
in_voltage = adc_voltage / (R2 / (R1 + R2)) + 0.2; // OFFSET +0.2
//----------------------------------------------------------------------------------------------
if (in_voltage > 14.50){
digitalWrite(4, HIGH); // K4 and COM X Connected (LED off)
} // K4 Extern Charge Powersupply
if (in_voltage < 11.00){ // K4 NO Contact
digitalWrite(4, LOW); // K4 and COM X Connected (LED on)
}
//---------------------------------------------------------------------------------------------
if (in_voltage > 14.75){
digitalWrite(5, LOW); // K3 and COM X Connected (LED off)
} // K3 Extern Charge Powersupply
if (in_voltage < 13.70){ // K3 NC Contact
digitalWrite(5, HIGH); // K3 and COM X Connected (LED on)
}
//---------------------------------------------------------------------------------------------
if (in_voltage > 12.60){
digitalWrite(6, HIGH); // K2 and COM X Connected (LED off)
} // K2 Cut Off Lipo4 Battery discharge to low/mixed Batterys lead acid/Lipo4
if (in_voltage < 12.20){ // K2 NC Contact
digitalWrite(6, LOW); // K2 and COM X Connected (LED on)
}
//---------------------------------------------------------------------------------------------
Serial.print("Battery Voltage = ");
Serial.println(in_voltage, 2);

delay(1000);
}

Did you decide to take no notice of the request to use code tags when posting code or did you just forget ?

Please edit your latest post and add them.

If you gave the pins names, you wouldn't have to keep repeating the dumb comments.