Closed loop control on arduino UNO

The code is supposed to function as a closed loop that sends feedback through a current sensor. What I'm I doing wrong?

#include <avr/io.h>
#include <util/delay.h>

int analogIn= A0;
int mVperAmp = 66;
int SensorValue =0;
int Voltagevalue = 0;
int RawVoltage = 0;
int ACSoffset = 5;
int Voltsensor = A2;
int pwm = 3;
int val = 0;

double Voltage1 = 0;
double Voltage =0;
double Amps = 0;
double refcurr = 0.2;
double refvolt = 0.4; 
double sensornew1 = 0;

void setup() {

  Serial.begin(9600);
  pinMode(pwm, OUTPUT); // output pin for OCR2B
  
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS20);
OCR2A = 105;
OCR2B = 40;

}
void loop() 
{

  SensorValue = analogRead(analogIn);
  Voltagevalue = analogRead(Voltsensor);  
  Voltage = (SensorValue / 1023.0)*5;
  Voltage1 = (Voltagevalue/1023.0);
  Amps = (510.5 - SensorValue)*75.75757576/1023;

 

if(Amps < refcurr)
{
   OCR2B=OCR2B+2;
   Amps = (510.5 - SensorValue)*75.75757576/1023;
   Serial.print("\t Current = ");
   Serial.println (Amps,3);
  
}

else
   OCR2B=OCR2B-2;
   Amps = (510.5 - SensorValue)*75.75757576/1023;
   Serial.print("\t Current = ");
   Serial.println (Amps,3);
   
delay(1000);
}

Blaues_blut,

What does your program / circuit do?

For a start, you need brackets around the else statement.

else
{
   OCR2B=OCR2B-2;
   Amps = (510.5 - SensorValue)*75.75757576/1023;
   Serial.print("\t Current = ");
   Serial.println (Amps,3);
}

Seems like we already have a topic on this one...

http://forum.arduino.cc/index.php?topic=392590.15

I appears that the OP has ignored all calls for information that could help us help him or her solve this and instead just started a new thread with the same code and none of the needed information.

The goal is to sense the current flowing through a buck converter using a current sensor, also to sense the voltage across it using a voltage sensor that I built. Keep in mind I'm dealing with high power. But everything has to be closed loop so that when I multiply the value of resistor by the reference current I have in the code: whatever voltage value I get as output should not change even if I change the power supply value. The only time voltage change is when I change resistor. I hope this is not COMPLICATED.

Did you fix the brackets as cattledog suggests? That's definitely an error. Try that first.

Yes sir I did, but it still doesn't give me what I want

I think my major issue here is I'm not sure I understand how to write a closed loop program

Delta_G:
Seems like we already have a topic on this one...

Closed loop control on arduino UNO - Programming Questions - Arduino Forum

Which is itself already a merge of two from the weekend I think.

My bad, it was a stupid thing to do

TomGeorge

Here are my voltage and current closed loop control I designed on MATLAB Simulink. You can see the feedback loop at the buttom there with gain blocks.The device I'm using to measure output voltage is a multimeter. (Practically). The current sensor I'm using is the ACS712, voltage sensor I built myself to output about 3 volts and its ground connects to arduino ground. I hope this is what you asked for.

I forgot to add this

Now, go back and put all those pics inline.

Right click one of the file names under your post and save the name (copy link address), then go to Modify the post. Click the add image icon (next to the chain) and use the saved name us the url and the pic will appear in the post.

That saves others downloading.

blaues_blut:
I'm not I understand what you suggested

Put the pics in the post, like this:

Oh I see what you mean

Threads merged. Again. STOP CROSS-POSTING!

Hi,
Still not a schematic.
Why didn't you just say SMPS?

Tom... :slight_smile: