I'm using a zero cross detector to determine when a signal is positive and negative. That part of the circuit works fine but in another section I need to control a relay based on the signal without using delay(). I want to set the relay LOW when the zero cross is HIGH and the relay HIGH when the zero cross is LOW. I need to keep the code efficient so is there a way I can set the relay state opposite to the zero cross state without using if statements.
int crosspin=2;
int crosstime;
int prestate=0;
int state;
int pretime=0;
int peakpin=A8;
int relay=8;
int peak;
int ctr=0;
void setup() {
// put your setup code here, to run once:
pinMode(crosspin,INPUT);
pinMode(peakpin,INPUT);
pinMode(relay,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
state=digitalRead(crosspin);
peak=analogRead(peakpin);
Serial.print("peak= ");
Serial.println(peak);
digitalWrite(relay,-state);
if(state==HIGH&&prestate==LOW){
crosstime=millis()-pretime;
Serial.print("time= ");
Serial.println(crosstime);
pretime=millis();
}
if(state==LOW&&prestate==HIGH){
crosstime=millis()-pretime;
Serial.print("time= ");
Serial.println(crosstime);
pretime=millis();
}
prestate=state;
}
Thanks for any help.