i wrote this code to control proportional solenoid valve with analog input 0-5 v . i made ramp by multiply the input signal by constant k to change the input signal slightly to the final value. but if the signal changed after running the program it can't go into the for loop so i put a condition while (r!=rold) // check if the input changed .but it gives me error
Actually, they are warning, not error messages:
Warning: D:\Micro Controller\my projects\proportional valve amplifier\pwm2.c(107): local variable 'r' is used before its value is set
Warning: D:\Micro Controller\my projects\proportional valve amplifier\pwm2.c(107): local variable 'rold' is used before its value is set
They are alerting you to the fact the varialbes,
r and
rhold, are being accessed before being properly initialized with a value.
A more prudent code technique would initialize those variables when declared:
Instead of:
Try something like this:
Code:
long r = 0;
long rold = 0;
Or whatever value is appropriate.
A warning alerts the programmer of a possible issue, which could present a problem for the code to function as expected, semantics, however warnings typically do not prevent the code from completing compilation.
An error, on the other hand, alerts the programmer of syntactical violation of the programming language grammar, structure or rules, which will typically halt the completion of compilation and preventing the creation of a HEX file.
Another issue apparent in your code is, once code execution leaves the while(r != rold) loop, code execution eventually leaves the main() routine, which should not be permitted to happen.
Code:
while (1)
{
while (r!=rold) // check if the input changed
{
///////// ramp up or down
for(i=0;i<4 ;i++)
{
r=read_adc(0);
PORTD=r;
x=r/10; // as i will multiply by K on the next step r from 0-5v
y=k[i]*x;
OCR1A=y*800/1023;
delay_ms(10);
}
}
r=read_adc(0);
x=r/10; // as i will multiply by K on the next step
y=k[3]*x;// stable at final value of x
OCR1A=y*800/1023;
rold=r;
[COLOR="#FF0000"]// Nothing to prevent code execution from exiting the main() routine, once the above statements are executed
[/COLOR]
}
BigDog