I am having a problem with programming code to establish a baseline int value from a potentiometer for the initial and default value to monitoring another variable int value for control of a servo.
The project is as follows: I have made an arduino based controller which monitor an electrical signal for Hz frequency. I want to adjust a servo to control the throttle on a generator and maintain a 60 Hz output.
Below is the code I have created but it isnt reliable and is somewhat sluggish. What I am trying to achieve with the code is to take the value of a potentiometer to use as a baseline value and incrementally increase or decrease that value to control a servo based on the value derived from an interupt signal from a digital pin.
In the code you will see reference to 2 different int inputs. I have two pots on the boards and have been experimenting with such. Ideally the second pot signal will control another servo with a different function.
Oh yeah and all of this has to maintain a certain timing for the input signal to be timed or adjusted in the calculation to determine frequency.
All suggestions or advice is appreciated. Here is the code Im working with:
#include <Servo.h>
int vibr_Pin = 7;
int potpin = 0;
int potpin1 = 3;
int potpin2 = 6;
int butval2;
int val;
int val1;
int val2 = 0;
int val3 = 0;
const int relaygas=A1;
const int relaystart=A2;
int posup=0;
int posup1=0;
int posdn=0;
int posdn1=0;
int vib_data = 0;
volatile byte freqcount;
int frequency;
unsigned long timeold;
int frequency1;
Servo myservo;
Servo myservo2;
Servo myservo3;
void freq_fun()
{
//ac cycle, this interrupt function is run twice, so take that into //consideration for
//calculating frequency
//Update count
freqcount++;}
void setup()
{
pinMode(vibr_Pin, INPUT); //set vibr_Pin input for measurment
Serial.begin(9600); //init serial 9600
Serial.begin(9600);
myservo.attach(3); // attaches the servo on pin 3 to the //servo object
myservo2.attach(9);
myservo3.attach(10);
attachInterrupt(0, freq_fun, FALLING);
freqcount = 0;
frequency = 0;
timeold = 0;
}
void loop() {
intstrt:;{
{val2 = analogRead(A7); // reads the value of the //potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 30, 180); // scale it to use it with the //servo (value between 0 and 180)
}
strt:;
Serial.println("starting loop");
//val = map(val2, 0, 1023, 65, 130);
Serial.print("initial POT value");
Serial.println(val2);
myservo.write(val2) ;
delay(10);
val3 = analogRead(A4); // reads the value of the //potentiometer (value between 0 and 1023)
val3 = map(val3, 1, 1023, 30, 180); // scale it to use it with the //servo (value between 30 and 180)
Serial.print("gas ");
Serial.println(val3);
posup=val3;
}
//digitalWrite(relaygas, LOW);
// digitalWrite(relaystart, HIGH);
freqchk:;{
// long measurement =TP_init();{
// delay(50);
// Serial.print("measurment = ");
// Serial.println(measurement);
// if (measurement > 10000){
// Serial.println("Warning");
// }
// else{
// digitalWrite(LED_Pin, LOW);
// Serial.println("Nominal");
// }
//}
delay(625);
Serial.println("delay value");
Serial.println("625");
detachInterrupt(0);
frequency = 60*42 /(millis() - timeold)*freqcount;
timeold = millis();
freqcount = 0;
attachInterrupt(0, freq_fun, FALLING);
Serial.print("Freq: ");
frequency1 = frequency/4;
Serial.print(frequency1);
Serial.print(" ");
Serial.print("AC Line Volts: ");
Serial.println(frequency/1);
val = analogRead(A7);
val = map(val, 0, 1023, 30, 165);
Serial.println("POT remapping values");
Serial.println(val);
Serial.println(val2);
Serial.println(val3);
if (val <= 31) {goto intstrt;}
if (val >= 163) {goto error1;}
//freqmap:;{
Serial.println("Analyzing Freq1 data");
if (posup >=179) {goto strt;}
if (posup <=0) {posup=val3;}
if (frequency1 == 0) {goto systemchk;}
if (frequency1 == 60) {goto freqchk;}
if (frequency1 <= 40) {goto error1;}
if (frequency1 <= 59) {goto up;}
if (frequency1 >= 68) {goto error2;}
if (frequency1 >= 61) {goto dn;}
else Serial.println("error evaluating");
//}
}
up:;
{ Serial.println ("LOW");
//posup++;
//posup=(val+posup);
myservo.write(++posup);
Serial.println(posup);
goto freqchk;
}
dn:;
{
Serial.println ("HIGH");
//posdn--;
// posdn=(val-posdn);
myservo.write(--posup);
Serial.println(posup);
goto freqchk; }
error1:;
{
{Serial.println ("error reset");}
val = analogRead(A7);
val = map(val, 0, 1023, 30, 165);
myservo.write(val);
delay(50);
goto strt;
}
error2:;
{
{Serial.println ("error master reset");}
myservo.write(val3);
//delay(6000);
goto intstrt;
}
systemchk:;{
myservo.write(val3);
delay(500);
Serial.println("system check");
goto freqchk;
}
}
// long TP_init(){
// delay(10);
// long measurement=pulseIn (vibr_Pin, HIGH); //wait for the pin //to get HIGH and returns measurement
// return measurement;}