Go Down

Topic: Will this program work? (Read 819 times) previous topic - next topic

krackyz

HI I'm new to arduino and programming in general, but I want to use it for my senior design.  I have an AC circuit which produces ac voltage to a variable resistor. I am planning to place in a voltage divider so that i can feed in a maximum of 5 volts AC to the board and have it read on an analog pin of the UNO. Then i want it to remain at 5 volts and if the case where it drops below i want an analog output pin to slowly feed it voltage/current until the input pin once again reads 5 volts will this program work?

Quote

int y;

void setup(){
  Serial.begin(9600);
  pinMode(9,OUTPUT);
}

void loop(){
  int sensorvalue = analogRead(A0);
  y = map(sensorvalue,0,1023,0,255);  // Scales the factor down
  Serial.println(y,DEC);
  
  while (y< 255){
  analogWrite(9,5);
  delay(30);
  }
}


Graynomad

Firstly, what is the analogueWrite() controlling.

Secondly, if you enter the while loop with y < 255 you will never exit. The program will hang in the loop.

Thirdly, if you are reading AC you will have trouble, you'll have to rectify and smooth the voltage.

So the short answer is no, it won't work.
______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

krackyz

Analog write is actually suppose to control current sent to field coils of an alternator which will output a voltage to be read. Because if the voltage drops below a certain value, then I'd like for the for the analog write to send a voltage to the coils so that they will induce a bigger voltage raise the value  thereby increasing the value of analogRead.

secondly, i should change the while loop to be something of a better scaled factor such as while(y<130).

thirdly, for the AC voltage, i understand that the voltage will be jumping around from value to value, so can't i just hold some values with delay and then act on it?

Graynomad

#3
Mar 13, 2011, 10:14 am Last Edit: Mar 13, 2011, 12:03 pm by Graynomad Reason: 1
Controlling of field coils is well out my area of expertise so I'll believe you on that. You are aware however that analogWrite() doesn't actually produce an analogue voltage, it does a PWM and you have to smooth that yourself. Also will 0-5v be enough for your coils?

Quote
i should change the while loop to be something of a better scaled factor such as while(y<130).

Then it will hang if the value is < 130. No you have to test the value and do something according to the result.

if (y < 255)
  analogWite (9,5);

However the value looks a little strange, where does 5 come from, or do you mean 255 for 5v.

But the trouble is that you never write a different value, so I don't see any proportional control happening. At some point you'll do a analogWrite (9,255) and that's where the output voltage will stay forever. If this was OK then you'd just hardwire it to 5v.

Presumably you have to write a small value if the voltage is too high, and a large value if it's too low.

Quote
for the AC voltage, i understand that the voltage will be jumping around from value to value, so can't i just hold some values with delay and then act on it?

In theory you could sample say 100 times and pick the highest value, BUT if your sampling frequency is very close to the AC frequency you will have a "beat" situation in which it could take minutes of even hours to get a correct reading. It's doable is you know the frequencies involved and sample correctly.

______
Rob


Rob Gray aka the GRAYnomad www.robgray.com

krackyz

Quote
You are aware however that analogWrite() doesn't actually produce an analogue voltage, it does a PWM and you have to smooth that yourself.


No i was never really sure of that. How do you smooth it out yourself? with a capacitor?

Quote
Also will 0-5v be enough for your coils?


My coils when applied with a 1amp will output 9 volts from the alternator. Do you think that will be enough? I have included a picture of the amount of field current i apply verses the voltage outputted from the alternator. Do you think that 1 amp will be able to be supplied from 5 volt output of the Arduino?

Quote
if (y < 255)
   analogWite (9,5);

However the value looks a little strange, where does 5 come from, or do you mean 255 for 5v.


Well i originally assumed that as long as the value of y is less than 255 (aka 5V)
Then the analog write will keep outputting an increasing value of +5 from 0-255 until the while loop detects that y is finally = 255(aka 5V) maybe its suppose to be programmed like the following?


int x=5;
while (y <255)
x= x+5;
analogwrite(9,x);


Quote
In theory you could sample say 100 times and pick the highest value, BUT if your sampling frequency is very close to the AC frequency you will have a "beat" situation in which it could take minutes of even hours to get a correct reading. It's doable is you know the frequencies involved and sample correctly.



I have a oscilliscope so i can track the frequency of the output of the alternator, as far as sampling correctly can you tell me how? Lets say i find the frequency of the alternator and i can determine how fast the Arduino Uno samples at. How can avoid the "beat" situation?



Go Up