Go Down

Topic: void loop cannot execute and returns values on Arduino Mega 2560 (Read 775 times) previous topic - next topic

Maslord

Hi
I have a sketch with the following structure:

Code: [Select]

void setup()
{
...
}

int main()
{
...
}

void A()
{
...
}

void B()
{
...
}

void C()
{
...
}

void D()
{
...
}

void loop()
{
...
}




So the idea is that the obtain values in void loop should be return to void D and after some math, the values of void D should return to void C. The problem is that the void loop never executed!!
Any suggestion?

Maslord

Complete sketch is as follows:

Code: [Select]


#define UN (400.0)
#define FN (50.0)
#define P (UN/FN)
#define T_PWM (0.000255)
#define T_MAX (4.0)
#define T_MIN (0.02)
#define K_MAX floor (T_MAX/T_PWM)
#define K_MIN ceil (T_MIN/T_PWM)
volatile static unsigned int length_tab_sin;
static unsigned int i = 0;
volatile static unsigned int main_counter = 0;

static unsigned int next_value_sin = 0;
static double t_param = 100;
static float t = T_PWM;
static float omega_t;
static float t_out;
static float U_o_param;

static unsigned int ocr3a, ocr3b, ocr1a;
static unsigned int ocr1b, ocr2a, ocr2b;
static double sin_in;
static double error = 1;
static unsigned int analog = 0;
static double U_in = 0;
static double U_rms_max;
static bool a = 0;

// Tachometer parameters: //////////////////////////////
long startTime = 0;
int state, prevState = 0;
int sensor = 0;
long counter = 0;
double Ff = 0;
long sample_time;
int magnet_number;      
int sensore_output_voltage = 1000;
int rpm;
//unsigned long startTime;
long samp = 0;
////////////////////////////////////////////////


void setup()
{
  Serial.begin(57600);
  pinMode(A7, OUTPUT);          //LED out pin
  pinMode(A1,OUTPUT);           //Sensor VCC pin
  pinMode(A0,INPUT);            //Sensor Input pin
  
}

int main()
{
  io_init();
  timers_init(); // initialization of PWM meters
  adc_init(); // initialization of the ADC transducer


  while (1) // infinite loop with the main program
  {


    if (i == 185)
    {
      chang_para();
      i = 0;
    }

    next_value_sin = main_counter % length_tab_sin;
    sin_in = omega_t*next_value_sin;
    
    ocr1a = round(error * (U_o_param * (sin(sin_in) + 1) * 254 / 2) + 1); // pin
    ocr1b = ocr1a - 1;
    ocr2a = round (error * (U_o_param * (sin(sin_in - 2.09) + 1) * 254 / 2) + 1); // pin
    ocr2b = ocr2a - 1;
    ocr3a = round(error * (U_o_param * (sin(sin_in + 2.09) + 1) * 254 / 2) + 1); // pin
    ocr3b = ocr3a - 1;
    
    cli();
    
    OCR1AL = ocr1a; // pin
    OCR1BL = ocr1b; // pin
    OCR2A = ocr2a; // pin
    OCR2B = ocr2b; // pin
    OCR3AL = ocr3a; // pin
    OCR3BL = ocr3b; // pin
    sei();
    i++;
  }

}
void adc_init()
{
  ADCSRA |= _BV(ADEN);
  ADCSRA |= _BV(ADPS2);
  ADCSRA |= _BV(ADPS1);
  ADCSRA |= _BV(ADPS0);
  ADMUX |= _BV(REFS0);
  ADMUX |= ADMUX &= 0b11110000; // choose the ADC0 input for measurement

}
void timers_init()
{
  cli();
  
  TCCR1A |= _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1) | _BV(WGM10);
  TCCR1B |= _BV(CS11); // preskaler 8  | _BV(WGM02)
  TIMSK1 |= _BV (TOIE1);                                                                      // flag from value 0 enabled //??????????
  
  TCCR2A |= _BV(COM2A1) | _BV(COM2B0) | _BV(COM2B1) | _BV(WGM20);
  TCCR2B |= _BV (CS21); // preskaler 8 //  | _BV(WGM02)
  
  TCCR3A |= _BV(COM3A1) | _BV(COM3B0) | _BV(COM3B1) | _BV(WGM30);
  TCCR3B |= _BV(CS31); // preskaler 8  | _BV(WGM02)
  
  TCNT1L = 0;
  TCNT2 = 0;
  TCNT3L = 0;

  sei();
}

void io_init()
{


  pinMode(5, OUTPUT);                          
  pinMode(2, OUTPUT);                          
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(14, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);
  pinMode(16, OUTPUT);

}

ISR(TIMER1_OVF_vect) // interrupt at 0 counter 0
{

  analog = ADC;
  if (a)
  {
    U_in = 0.0709 * analog;
    ADMUX |= _BV(MUX0); // choose the ADC1 input to measure the current
  }
  else
  {
    ADMUX |= ADMUX &= 0b11110000; // select the ADC0 input to measure the voltage
    if (analog > 579)
    {
      error = 0; // if the overload turn off the voltage generation
      digitalWrite(16, HIGH); // lighting the diode
    }
  }
  ADCSRA |= _BV(ADSC); // start reading the measurement
  a = a ^ 1; // the XOR gate negates the logical value a
  main_counter++;
  if (main_counter >= length_tab_sin) main_counter = 0;

}

void chang_para()
{

  U_rms_max = U_in * 0.62;
  bool up;
  bool down;
  up = digitalRead(14);
  down = digitalRead(15);
  if (!up) t_param--;
  if (!down) t_param ++;
  if (t_param < 0) t_param = 0;
  if (t_param > 100) t_param = 100;
  length_tab_sin = ceil((K_MAX - K_MIN) * t_param / 500 + K_MIN);
  t_out = T_PWM * length_tab_sin;
  omega_t = t * 2 * PI / t_out;
  U_o_param = (P / t_out) / U_rms_max;
  if (t_out > 1) U_o_param = 0.5 * (18.5 / U_rms_max);
  if (U_o_param > 1) U_o_param = 1;
  error = 1;
  
  digitalWrite(16, LOW); //diode lighting

                Serial.println(t_param);
}


void loop()
{
  //reset variables.
  sensor = 0;
  Ff = 0;
  state = 0;
  prevState = 0;
  counter = 0;
  
  startTime = millis();
  sample_time = 5000;        //Loop for sample duration of sec
  magnet_number = 4;       //is number of magnets per revulotion
  rpm = 0;
  //enable sensor and turn on LED when touched.
  digitalWrite(A1, HIGH);
   Serial.println("Measuring...");

  //Loop for sample duration of 10 sec
  while((millis() - startTime) < sample_time)
  {
    sensor =  analogRead(0);

//    Serial.print("sensor = "); Serial.println(sensor);
    if (sensor > sensore_output_voltage)  //sensore output voltage
    state = 1;
  
    else
    state = 0;
     digitalWrite(A7,state); // Blinking LED when sensor Activated
    
    //On change of state increment counter.
    //A change in state twice represents one revolution
    if(state != prevState)
    {
      counter++;
      prevState = state;
    }

  }
  
  counter = counter * 12 / 2; // 12*5sec=60sec
  
  rpm = counter / magnet_number;
                          
//  Ff=  rpm * 4.0 * 100 / 120;               // rpm=120*f/p   , 4 pole motor, 100 is float multiplier
  Ff=  rpm * 4.0 / 120;               // rpm=120*f/p   , 4 pole motor,
    
  //Trun off Sensor and LED
  digitalWrite(A7,LOW);
  digitalWrite(A1, LOW);
  
  //Display RPM

  Serial.print("count = "); Serial.println(counter);
  Serial.print("RPM = "); Serial.println(rpm);
  Serial.print("freq = "); Serial.println(Ff);
  
  
  delay(100); // delay 1 ms
}


pert

There is a main() function in the Arduino core library that calls setup() and loop(). When you define a function named main() in your sketch it overrides the main() in the core library and so setup() and loop() never get called.

It's not really clear what you're trying to accomplish in this code. Maybe it's just blind copy/paste?

Maslord

There is a main() function in the Arduino core library that calls setup() and loop(). When you define a function named main() in your sketch it overrides the main() in the core library and so setup() and loop() never get called.

It's not really clear what you're trying to accomplish in this code. Maybe it's just blind copy/paste?
It is not a blind copy paste, it was 2 separate codes that I modified them separately and tried to combine them. The whole code except the loop() part is a PWM variable frequency drive which should be drive a induction motor. The loop() section should read the speed of another rotating shaft and its speed should be return back to void chang_para() section as a coefficient to the final speed of the motor. Both code are running separately without any problem but when combine the loop part is not running. Is it possible to replace main or loop function to another commands to solve the problem?

MarkT

So a blind copy-paste as we suspected...

Move the code at the start of your main() into setup().

Move the body of the while loop in your main() into loop()

Delete your main() function which is now empty.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Maslord

So a blind copy-paste as we suspected...

Move the code at the start of your main() into setup().

Move the body of the while loop in your main() into loop()

Delete your main() function which is now empty.
Mark, for a expert like you it is a blind thing but for a newbie like me it is a lot of work. Also thank you for your help. As you said I made the change on code and it became like this:

 
Code: [Select]




#define UN (400.0)
#define FN (50.0)
#define P (UN/FN)
#define T_PWM (0.000255)
#define T_MAX (4.0)
#define T_MIN (0.02)
#define K_MAX floor (T_MAX/T_PWM)
#define K_MIN ceil (T_MIN/T_PWM)
volatile static unsigned int length_tab_sin;
static unsigned int i = 0;
volatile static unsigned int main_counter = 0;

static unsigned int next_value_sin = 0;
static double t_param = 100;
static float t = T_PWM;
static float omega_t;
static float t_out;
static float U_o_param;

static unsigned int ocr3a, ocr3b, ocr1a;
static unsigned int ocr1b, ocr2a, ocr2b;
static double sin_in;
static double error = 1;
static unsigned int analog = 0;
static double U_in = 0;
static double U_rms_max;
static bool a = 0;

// Tachometer parameters: //////////////////////////////
long startTime = 0;
int state, prevState = 0;
int sensor = 0;
long counter = 0;
double Ff = 0;
long sample_time;
int magnet_number;     
int sensore_output_voltage = 1000;
int rpm;
//unsigned long startTime;
long samp = 0;
////////////////////////////////////////////////


void setup()
{
  Serial.begin(57600);
 
  io_init();
  timers_init();            // initialization of PWM meters
  adc_init();              // initialization of the ADC transducer
 
}


void adc_init()
{
  ADCSRA |= _BV(ADEN);
  ADCSRA |= _BV(ADPS2);
  ADCSRA |= _BV(ADPS1);
  ADCSRA |= _BV(ADPS0);
  ADMUX |= _BV(REFS0);
  ADMUX |= ADMUX &= 0b11110000; // choose the ADC0 input for measurement

}
void timers_init()
{
  cli();
 
  TCCR1A |= _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1) | _BV(WGM10);
  TCCR1B |= _BV(CS11); // preskaler 8  | _BV(WGM02)
  TIMSK1 |= _BV (TOIE1);                                                                   
 
  TCCR2A |= _BV(COM2A1) | _BV(COM2B0) | _BV(COM2B1) | _BV(WGM20);
  TCCR2B |= _BV (CS21); // preskaler 8 //  | _BV(WGM02)
 
  TCCR3A |= _BV(COM3A1) | _BV(COM3B0) | _BV(COM3B1) | _BV(WGM30);
  TCCR3B |= _BV(CS31); // preskaler 8  | _BV(WGM02)
 
  TCNT1L = 0;
  TCNT2 = 0;
  TCNT3L = 0;

  sei();
}

void io_init()
{


  pinMode(5, OUTPUT);                         
  pinMode(2, OUTPUT);                         
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(14, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);
  pinMode(16, OUTPUT);
  pinMode(A7, OUTPUT);          //LED out pin
  pinMode(A1,OUTPUT);           //Sensor VCC pin
  pinMode(A0,INPUT);            //Sensor Input pin

}

ISR(TIMER1_OVF_vect) // interrupt at 0 counter 0
{

  analog = ADC;
  if (a)
  {
    U_in = 0.0709 * analog;
    ADMUX |= _BV(MUX0); // choose the ADC1 input to measure the current
  }
  else
  {
    ADMUX |= ADMUX &= 0b11110000; // select the ADC0 input to measure the voltage
    if (analog > 579)
    {
      error = 0; // if the overload turn off the voltage generation
      digitalWrite(16, HIGH); // lighting the diode
    }
  }
  ADCSRA |= _BV(ADSC); // start reading the measurement
  a = a ^ 1; // the XOR gate negates the logical value a
  main_counter++;
  if (main_counter >= length_tab_sin) main_counter = 0;

}

void chang_para()
{

  U_rms_max = U_in * 0.62;
  bool up;
  bool down;
  up = digitalRead(14);
  down = digitalRead(15);
  if (!up) t_param--;
  if (!down) t_param ++;
  if (t_param < 0) t_param = 0;
  if (t_param > 100) t_param = 100;
  length_tab_sin = ceil((K_MAX - K_MIN) * t_param / 500 + K_MIN);
  t_out = T_PWM * length_tab_sin;
  omega_t = t * 2 * PI / t_out;
  U_o_param = (P / t_out) / U_rms_max;
  if (t_out > 1) U_o_param = 0.5 * (18.5 / U_rms_max);
  if (U_o_param > 1) U_o_param = 1;
  error = 1;
 
  digitalWrite(16, LOW); //diode lighting

                Serial.println(t_param);
}


void loop()
{
     if (i == 185)
    {
      chang_para();
      i = 0;
    }

    next_value_sin = main_counter % length_tab_sin;
    sin_in = omega_t*next_value_sin;
   
    ocr1a = round(error * (U_o_param * (sin(sin_in) + 1) * 254 / 2) + 1); // pin
    ocr1b = ocr1a - 1;
    ocr2a = round (error * (U_o_param * (sin(sin_in - 2.09) + 1) * 254 / 2) + 1); // pin
    ocr2b = ocr2a - 1;
    ocr3a = round(error * (U_o_param * (sin(sin_in + 2.09) + 1) * 254 / 2) + 1); // pin
    ocr3b = ocr3a - 1;
   
    cli();
   
    OCR1AL = ocr1a; // pin
    OCR1BL = ocr1b; // pin
    OCR2A = ocr2a; // pin
    OCR2B = ocr2b; // pin
    OCR3AL = ocr3a; // pin
    OCR3BL = ocr3b; // pin
    sei();
    i++;



  //reset variables.
  sensor = 0;
  Ff = 0;
  state = 0;
  prevState = 0;
  counter = 0;
 
  startTime = millis();
  sample_time = 5000;        //Loop for sample duration of sec
  magnet_number = 4;       //is number of magnets per revulotion
  rpm = 0;
  //enable sensor and turn on LED when touched.
  digitalWrite(A1, HIGH);
   Serial.println("Measuring...");

  //Loop for sample duration of 10 sec
  while((millis() - startTime) < sample_time)
  {
    sensor =  analogRead(0);

//    Serial.print("sensor = "); Serial.println(sensor);
    if (sensor > sensore_output_voltage)  //sensore output voltage
    state = 1;
 
    else
    state = 0;
     digitalWrite(A7,state); // Blinking LED when sensor Activated
   
    //On change of state increment counter.
    //A change in state twice represents one revolution
    if(state != prevState)
    {
      counter++;
      prevState = state;
    }

  }
 
  counter = counter * 12 / 2; // 12*5sec=60sec
 
  rpm = counter / magnet_number;
                         
//  Ff=  rpm * 4.0 * 100 / 120;               // rpm=120*f/p   , 4 pole motor, 100 is float multiplier
  Ff=  rpm * 4.0 / 120;               // rpm=120*f/p   , 4 pole motor,
   
  //Trun off Sensor and LED
  digitalWrite(A7,LOW);
  digitalWrite(A1, LOW);
 
  //Display RPM

  Serial.print("count = "); Serial.println(counter);
  Serial.print("RPM = "); Serial.println(rpm);
  Serial.print("freq = "); Serial.println(Ff);
 
 
  delay(100); // delay 1 ms
}




now the problem is the functionality is totally destroyed, not PWM is working nor rpm counter. I tried to do more googling to learn more but nothing found. Any help?

jremington

You need to go through the original programs line by line until you understand what each line is supposed to do and why.

Only when you understand that, will it become possible to sensibly merge two programs.

Quote
Mark, for a expert like you it is a blind thing but for a newbie like me it is a lot of work.
"A lot of work" is the only way to become an expert. If you want a result without doing the work, post on the Gigs and Collaborations forum section, and be prepared to pay for the help.

Delta_G

Quote
So the idea is that the obtain values in void loop should be return to void D and after some math, the values of void D should return to void C.
void functions don't return values.  
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Maslord

You need to go through the original programs line by line until you understand what each line is supposed to do and why.

Only when you understand that, will it become possible to sensibly merge two programs.
"A lot of work" is the only way to become an expert. If you want a result without doing the work, post on the Gigs and Collaborations forum section, and be prepared to pay for the help.
You are right and I am trying to learn and understand it.


Slumpert

You have put a lot of stuff before - Void Setup()..
.


Maslord

You have put a lot of stuff before - Void Setup()..
.


The main problem is with timers, when I disable timers_init(), timer0 can do millis(). I do not know how to set timer0 for millis and timer 1,2,3 for PWM! I think this is the first problem.

Delta_G

 
The main problem is with timers, when I disable timers_init(), timer0 can do millis(). I do not know how to set timer0 for millis and timer 1,2,3 for PWM! I think this is the first problem.
Why are you trying to do that yourself?  Just let the Arduino core do that stuff for you.  Stick within the normal framework of an Arduino sketch, which just means not having a function called "main" and you get that with no effort.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

jremington

The Arduino IDE automatically sets up the timers to count milliseconds and produce PWM.

Maslord

So a blind copy-paste as we suspected...

Move the code at the start of your main() into setup().

Move the body of the while loop in your main() into loop()

Delete your main() function which is now empty.
After few days following your useful comment I finally could fix the code but the respond time  is very slow and the rpm counter is very noisy, do you have any idea please? if you have required hardware, could you please test the output frequency on oscilloscope?

Code: [Select]


#define UN (400.0) // motor rated voltage
#define FN (50.0) // motor nominal frequency
#define P (UN/FN) // associate Determining the ratio of voltage to nominal frequency
#define T_PWM (0.000255) // PWM signal period - set by the prescaler in the counters
#define T_MAX (4.0) // Specify the maximum output voltage period
#define T_MIN (0.02) // minimum output voltage
#define K_MAX floor (T_MAX/T_PWM) // number of period values for T_MAX
#define K_MIN ceil (T_MIN/T_PWM) // number of period values for T_MIN
volatile static unsigned int length_tab_sin; // variable containing the number of values in the full // period of the output voltage
static unsigned int i = 0; // auxiliary variable
volatile static unsigned int main_counter = 0; // variable in the interrupt
// ^ every T_PWM period increasing its value by 1
static unsigned int next_value_sin = 0; // variable which value sin should be calculated
static double t_param = 100; // parameter specifying the period of the output voltage
static float t = T_PWM; // T_PWM
static float omega_t; // pulsation of the output voltage multiplied by T_PWM
static float t_out; // output voltage period
static float U_o_param; // parameter specifying the size of the output voltage
// ^ calculated on the basis of t_out and U_in
static unsigned int ocr3a, ocr3b, ocr1a; // auxiliary variables to store obl. fillings
static unsigned int ocr1b, ocr2a, ocr2b; // ^
static double sin_in; // variable containing the parameter of the function sin
static double error = 1; // variable used to stop generating voltage when overloaded
static unsigned int analog = 0; // variable containing the measured value
static double U_in = 0; // variable holding the voltage measurement of the intermediate system
static double U_rms_max; // maximum currently possible to generate the effective voltage value
static bool a = 0; // logical variable for the implementation of two alternating measurements

/////////////////////////////////////////////////
unsigned long startTime = 0;
int state, prevState = 0;
int sensor = 0;
long counter = 0;
double Ff = 0;
long sample_time;
int magnet_number;       
int sensore_output_voltage = 1000;
int rpm;
long samp = 0;
/////////////////////////////////////////////////

void setup()
{
  Serial.begin(57600);
  io_init(); // initiate entry and exit
  timers_init(); // initialization of PWM meters
  adc_init(); // initialization of the ADC transducer
}

void loop()
{
 
    if (i == 185) // condition specifying the entry to the change function
    { // parameter of the discharge voltage, calling approx. 100ms
      chang_para(); // function to change the parameters of the output voltage
      i = 0;
    }
    next_value_sin = main_counter % length_tab_sin; // the next sine value to be calculated
    sin_in = omega_t*next_value_sin;
    // calculating the value to registers specifying the completion of the output signal /
    ocr1a = round(error * (U_o_param * (sin(sin_in) + 1) * 254 / 2) + 1); // pin
    ocr1b = ocr1a - 1;
    ocr2a = round (error * (U_o_param * (sin(sin_in - 2.09) + 1) * 254 / 2) + 1); // pin
    ocr2b = ocr2a - 1;
    ocr3a = round(error * (U_o_param * (sin(sin_in + 2.09) + 1) * 254 / 2) + 1); // pin
    ocr3b = ocr3a - 1;

//////////////////////////////////////////////
    sensor = 0;
  Ff = 0;
  state = 0;
  prevState = 0;
  counter = 0;
  startTime = millis();
  sample_time = 10;        //Loop for sample duration of sec
  magnet_number = 4;       //is number of magnets per revulotion
  rpm = 0;
  samp=0;
 
  //enable sensor and turn on LED when touched.
  digitalWrite(A5, HIGH);

//  Loop for sample duration of 10 sec
  while((millis() - startTime) < sample_time)
  {
    sensor =  analogRead(A3);
    samp=millis();

    if (sensor > sensore_output_voltage)  //sensore output voltage
    state = 1;
   
    else
    state = 0;
     digitalWrite(A7,state); // Blinking LED when sensor Activated
   
    //On change of state increment counter.
    //A change in state twice represents one revolution
    if(state != prevState)
    {
      counter++;
      prevState = state;
      Serial.println("sensor");
    }
  }
 
///////////////////////////////////////////////////

   
    // updating values in registers /
    cli(); // prohibition on obsloge interruptions in case if
    // an interrupt occurred during the upgrade
    OCR1AL = ocr1a; // pin
    OCR1BL= ocr1b; // pin
    OCR2A = ocr2a; // pin
    OCR2B = ocr2b; // pin
    OCR3AL = ocr3a; // pin
    OCR3BL = ocr3b; // pin
    sei(); // allow for interruption obsloge
    i++;
 

}


void adc_init()
{
  ADCSRA |= _BV(ADEN); // start the transmitter
  ADCSRA |= _BV(ADPS2); // setting the prescaler
  ADCSRA |= _BV(ADPS1); // ^
  ADCSRA |= _BV(ADPS0); // ^
  ADMUX |= _BV(REFS0); // reference voltage set as power supply
  ADMUX |= ADMUX &= 0b11110000; // choose the ADC0 input for measurement
}
void timers_init()
{
  cli(); // interrupt handler is forbidden
  // timer1 init
  TCCR1A |= _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1) | _BV(WGM10);
  TCCR1B |= _BV(CS11); // preskaler 8  | _BV(WGM02)
  TIMSK1 |= _BV (TOIE1); // flag from value 0 enabled //
  // timer2 init
  TCCR2A |= _BV(COM2A1) | _BV(COM2B0) | _BV(COM2B1) | _BV(WGM20);
  TCCR2B |= _BV (CS21); // preskaler 8 //  | _BV(WGM02)
  // timer3 init
  TCCR3A |= _BV(COM3A1) | _BV(COM3B0) | _BV(COM3B1) | _BV(WGM30);
  TCCR3B |= _BV(CS31); // preskaler 8  | _BV(WGM02)
  // resetting counter values
  TCNT1L = 0;
  TCNT3L = 0;
  TCNT2 = 0;

  /* the counter counts in g3re to 255, then in d3 ณ: / \ / \ / \
            at the value of 255 there is an interruption at which it occurs
            voltage and current measurements
*/
  sei(); // allow for interruption obsloge
}
void io_init()
{
  pinMode(2, OUTPUT); // OC0A Arduino Mega2560 pin 13
  pinMode(5, OUTPUT); // OC0B Arduino Mega2560 pin 4
  pinMode(11, OUTPUT); // OC1A Arduino Mega2560 pin 11
  pinMode(12, OUTPUT); // OC1B Arduino Mega2560 pin 12
  pinMode(10, OUTPUT); // OC2A Arduino Mega2560 pin 10
  pinMode(9, OUTPUT); // OC2B Arduino Mega2560 pin 9
 
  pinMode(16, OUTPUT); // Arduino Mega2560 16


    pinMode(A7, OUTPUT); //LED out pin
    pinMode(A5,OUTPUT); //Sensor VCC pin
    pinMode(A3,INPUT); //Sensor Input pin
    pinMode(A0,INPUT); //pot Input pin
}
ISR(TIMER1_OVF_vect) // interrupt at 0 counter 0
{
  analog = ADC;
  if (a)
  {
    U_in = 0.0709 * analog;
    ADMUX |= _BV(MUX0); // choose the ADC1 input to measure the current
  }
  else
  {
    ADMUX |= ADMUX &= 0b11110000; // select the ADC0 input to measure the voltage
    if (analog > 579)
    {
      error = 0; // if the overload turn off the voltage generation
      digitalWrite(16, HIGH); // lighting the diode
    }
  }
  ADCSRA |= _BV(ADSC); // start reading the measurement
  a = a ^ 1; // the XOR gate negates the logical value a
  main_counter++;
  if (main_counter >= length_tab_sin) main_counter = 0;
}
void chang_para()
{

t_param = map(analogRead(0),0,1230,0,100);
  U_rms_max = U_in * 0.62; // value 0.62 experimentally limited
  if (t_param < 0) t_param = 0; // protection of exceeding the extreme values
  if (t_param > 100) t_param = 100; // ^
  length_tab_sin = ceil((K_MAX - K_MIN) * t_param / 500 + K_MIN); // amount of values filled in one period
  t_out = T_PWM * length_tab_sin; // calculate the period of the output voltage
  omega_t = t * 2 * PI / t_out; // calculate the output voltage pulsation
  U_o_param = (P / t_out) / U_rms_max; // calculate the parameter determining the value of the output voltage
  if (t_out > 1) U_o_param = 0.5 * (18.5 / U_rms_max); // voltage at the output at low frequencies of 10v
  if (U_o_param > 1) U_o_param = 1; //protection of exceeding the extreme values
  error = 1; //if the overload turn off the voltage generation
 
  //if the overload turn off the voltage generation
  digitalWrite(16, LOW); //diode lighting
                Serial.println(t_param);

}




Go Up