Voltage Stabilizer

Dear Respcted Sirs,

I am S.K,Sahoo from India, just started writing codes in Arduino few months back. it is wonderfull to write .
I am trying to develop a AC relay control voltage stabilizer using Arduino. I also written a code but sorry to say it gives some errors . Please help to solve the issues. Here i m posting the code .

Thanks


#define RL1Pin  4
#define RL2Pin  5
#define RL3Pin  6
#define RL4Pin  7
#define ledPin  8   // led for indication of timer
#define swPin   9

#define lowLimit  80
#define highLimit  250
#define lowOk  90
#define highOk  220

float m;
float n;
float mainV = 0;

unsigned int step=1;
unsigned int time=0,x;
int fault=0;
unsigned int rDelayTime=30;

/*********************************************************/
void setup() {
  pinMode (A0, INPUT);
  pinMode (RL1Pin, OUTPUT);
  pinMode (RL2Pin, OUTPUT);
  pinMode (RL3Pin, OUTPUT);
  pinMode (RL4Pin, OUTPUT);

  pinMode (ledPin, OUTPUT);
  pinMode (swPin, INPUT);
  Serial.begin (9600);
}
/********************************************************/

void loop () {
  initialise ();

  while (1) {
        startUp ();
      do
      {
        for (x=0: x<20; x++) {
          readAdc ();
        }
      }
  
  if (mainV >241) {
    step--;
     if (step<1) step--;
  }
  else if (mainV<90) {
    step++;
    if (step>6) step=6; 
   }
    relaySet ();
  }   
 while ((mainV>highLimit) && (mainV<lowLimit)); 
    fault = 1;
    rDelayTime = 0;
    
    if (mainV>=highLimit) mainHigh ();
        else if (mainV<=lowLimit); mainLow ();
      fault=0;
    }
}

/***************Initialize Function*******************/

void initialise () {
   digitalWrite ( RL4Pin, LOW);
    digitalWrite (RL1Pin, HIGH);
     digitalWrite (RL2Pin, HIGH);
      digitalWrite (RL3Pin, HIGH);
}

/**************StartUp Function***************************/

void startUp () {
  delay (100);
  readAdc ();
  delay (1000*25);
}

/**************readAdc Function*************************/

void readAdc() {
  m = analogRead (A0);
  n = (m*0.304177);
  Serial.print ("Analog Value");
  Serial.print (m);
  Serial.print ("   AC Voltage  ");
  mainV = (n/sqrt(2));
  Serial.print (mainV);
  Serial.println ();
  delay (1000);
}

/*****************mainHigh Function**********************************/

void mainHigh() {
  digitalWrite (RL4Pin, LOW);
   digitalWrite (RL3Pin, HIGH);
    digitalWrite (RL2Pin, HIGH);
     digitalWrite (RL1Pin, HIGH);
      delay (500);
    while (mainV >= highOk) {
      rDelayTime = 0;
      readAdc ();
    }
       
  }

/*****************mainLow Function*********************************/

void mainLow () {
   digitalWrite (RL4Pin, LOW);
    digitalWrite (RL3Pin, LOW);
     digitalWrite (RL2Pin, LOW);
      digitalWrite (RL1Pin, LOW);
       delay (500);

       while (mainV = lowOk) {
        rDelayTime = 0;
          readAdc ();
       }
   }

/**************relaySet Function**************************/

void relaySet () {
  if (step==1) {
    digitalWrite (RL1Pin,1);
    digitalWrite (RL2Pin,1);
    digitalWrite (RL3Pin,1);
  }
  else if (step==2) {
    digitalWrite (RL1Pin,1);
    digitalWrite (RL2Pin,1);
    digitalWrite (RL3Pin,0);
  }
  else if (step==3) {
    digitalWrite (RL1Pin,1);
    digitalWrite (RL2Pin,0);
    digitalWrite (RL3Pin,1);
  }
  else if (step==4) {
    digitalWrite (RL1Pin,1);
    digitalWrite (RL2Pin,0);
    digitalWrite (RL3Pin,0);
  }
  else if (step==5) {
    digitalWrite (RL1Pin,0);
    digitalWrite (RL2Pin,0);
    digitalWrite (RL3Pin,1);
  }
  else if (step==6) {
    digitalWrite (RL1Pin,0);
    digitalWrite (RL2Pin,0);
    digitalWrite (RL3Pin,0); }
    else {
}

/*******************************************/

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

You have too many closing braces on the loop() function

Auto Format the code in the IDE to see the problem more clearly

Dear Respcted Sirs,

I am S.K,Sahoo from India, just started writing codes in Arduino few months back. it is wonderfull to write .
I am trying to develop a AC relay control voltage stabilizer using Arduino. I also written a code but sorry to say it gives some errors . Please help to solve the issues. Here i m posting the code .

Thanks


#define RL1Pin  4
#define RL2Pin  5
#define RL3Pin  6
#define RL4Pin  7
#define ledPin  8   // led for indication of timer
#define swPin   9

#define lowLimit  80
#define highLimit  250
#define lowOk  90
#define highOk  220

float m;
float n;
float mainV = 0;

unsigned int step=1;
unsigned int time=0,x;
int fault=0;
unsigned int rDelayTime=30;

/*********************************************************/
void setup() {
 pinMode (A0, INPUT);
 pinMode (RL1Pin, OUTPUT);
 pinMode (RL2Pin, OUTPUT);
 pinMode (RL3Pin, OUTPUT);
 pinMode (RL4Pin, OUTPUT);

 pinMode (ledPin, OUTPUT);
 pinMode (swPin, INPUT);
 Serial.begin (9600);
}
/********************************************************/

void loop () {
 initialise ();

 while (1) {
       startUp ();
     do
     {
       for (x=0: x<20; x++) {
         readAdc ();
       }
     }
 
 if (mainV >241) {
   step--;
    if (step<1) step--;
 }
 else if (mainV<90) {
   step++;
   if (step>6) step=6; 
  }
   relaySet ();
 }   
while ((mainV>highLimit) && (mainV<lowLimit)); 
   fault = 1;
   rDelayTime = 0;
   
   if (mainV>=highLimit) mainHigh ();
       else if (mainV<=lowLimit); mainLow ();
     fault=0;
   }
}

/***************Initialize Function*******************/

void initialise () {
  digitalWrite ( RL4Pin, LOW);
   digitalWrite (RL1Pin, HIGH);
    digitalWrite (RL2Pin, HIGH);
     digitalWrite (RL3Pin, HIGH);
}

/**************StartUp Function***************************/

void startUp () {
 delay (100);
 readAdc ();
 delay (1000*25);
}

/**************readAdc Function*************************/

void readAdc() {
 m = analogRead (A0);
 n = (m*0.304177);
 Serial.print ("Analog Value");
 Serial.print (m);
 Serial.print ("   AC Voltage  ");
 mainV = (n/sqrt(2));
 Serial.print (mainV);
 Serial.println ();
 delay (1000);
}

/*****************mainHigh Function**********************************/

void mainHigh() {
 digitalWrite (RL4Pin, LOW);
  digitalWrite (RL3Pin, HIGH);
   digitalWrite (RL2Pin, HIGH);
    digitalWrite (RL1Pin, HIGH);
     delay (500);
   while (mainV >= highOk) {
     rDelayTime = 0;
     readAdc ();
   }
      
 }

/*****************mainLow Function*********************************/

void mainLow () {
  digitalWrite (RL4Pin, LOW);
   digitalWrite (RL3Pin, LOW);
    digitalWrite (RL2Pin, LOW);
     digitalWrite (RL1Pin, LOW);
      delay (500);

      while (mainV = lowOk) {
       rDelayTime = 0;
         readAdc ();
      }
  }

/**************relaySet Function**************************/

void relaySet () {
 if (step==1) {
   digitalWrite (RL1Pin,1);
   digitalWrite (RL2Pin,1);
   digitalWrite (RL3Pin,1);
 }
 else if (step==2) {
   digitalWrite (RL1Pin,1);
   digitalWrite (RL2Pin,1);
   digitalWrite (RL3Pin,0);
 }
 else if (step==3) {
   digitalWrite (RL1Pin,1);
   digitalWrite (RL2Pin,0);
   digitalWrite (RL3Pin,1);
 }
 else if (step==4) {
   digitalWrite (RL1Pin,1);
   digitalWrite (RL2Pin,0);
   digitalWrite (RL3Pin,0);
 }
 else if (step==5) {
   digitalWrite (RL1Pin,0);
   digitalWrite (RL2Pin,0);
   digitalWrite (RL3Pin,1);
 }
 else if (step==6) {
   digitalWrite (RL1Pin,0);
   digitalWrite (RL2Pin,0);
   digitalWrite (RL3Pin,0); }
   else {
}

/*******************************************/

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Arduino_Voltage_Stabilizer_4RLY.ino (3.24 KB)

Thank you Mr UKHeliBob.

you are so kind to answer so quickly.

Sorry I have never tried Auto format but after your advice i tried it by pressint Ctrl+T.
When i Auto Format it gives a msg "No changes necessary for Auto Format"

The error is showing ' initialise' was not declared in this scope" when i compile

Please help to guide me how to auto format.

Thanks a lot .

Well, it needs formatting when I download it.

Try it for yourself. Select the code you posted, copy it, paste it into an empty IDE screen then try Auto Format

Before Auto Format

      fault=0;
    }
}

/***************Initialize Function*******************/

void initialise () {
   digitalWrite ( RL4Pin, LOW);

After Auto Format

 fault = 0;
}
}

/***************Initialize Function*******************/

void initialise ()
{
  digitalWrite ( RL4Pin, LOW);

Duplicate of Voltage Stabilizer - Programming Questions - Arduino Forum

Hi,
You need to check ALL your { and }.
You do not need while(1).

Place initialise(); as the last line of setup, instead of the first of the loop, let the loop do the work for you.

Consider using switch.. case instead of all the nested if, else, while that you have.

https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

Tom... :slight_smile:

Hello Mr Bob and Mr George

Thanks for answering me.

Thanks again Mr Bob for teaching me about Auto Format, certainly i will try this Auto Format Command.
I have never tried this switch function Mr George, i will try and let you know .

I have modified my code in a different way and it is not showing any errors now.

Let you know after burning in the device and testing in the Hardware, whether it worked or not.

Thanks again both of you to be kind .

S.K.Sahoo

@sahoosk, do not hijack. Thread split.

@sahoosk, stop cross-posting. Other thread removed.

@sahoosk, stop cross-posting. Threads merged.

Sorry Every body,

I did not knew to attach the code , i paste the code.
i will never do it again.

Sorry.......
Thanks for intimating me.

Thanks
S.K.Sahoo

Hello

Actually i am following a PIC code and trying to use in Arduino.
In the original code , Timer interrupt is used.

After burning the code and testing in Hardware i found it is not working .

If you permit i can attach the Original PIC code here and will appreciate if some one will help me to write using Arduno .

Thanks
S.K.Sahoo