Hello all,
I wrote the folowing code to create some pulses but I have one problem where I need some help. Every time I make a choice for a pulse, the pulse starts from "OFF" state. How can I wrote the code so pulse starts with "ON" state?
Thanks in advance.
Panagiotis
int highSince = 0;
int lowSince = 0;
int ledStatus = LOW;
int counter=0;
int now=0;
char ch=0;//gia energopoihsh
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
Serial.begin(9600);
}
void loop() {
if(Serial.available()>0){
ch=Serial.read();
}//end if
now=millis();
if (ch=='a'){//aeroporiki prosbolh
if(ledStatus == LOW && now - lowSince >= 1000)
{
highSince = now;
ledStatus = HIGH;
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
}
else if(ledStatus == HIGH && now - highSince >= 3000)
{
lowSince = now;
ledStatus = LOW;
digitalWrite(13, LOW);
digitalWrite(12, LOW);
}
}
if (ch=='b'){//epigeia prosbolh
if(ledStatus == HIGH && now - highSince >= 30000)
{
lowSince = now;
ledStatus = LOW;
digitalWrite(13, LOW);
digitalWrite(12, LOW);
}
else if(ledStatus == LOW && now - lowSince >= 30000)
{
highSince = now;
ledStatus = HIGH;
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
}
}
if (ch=='c'){//PBX prosbolh
if(ledStatus == HIGH && now - highSince >= 1000)
{
lowSince = now;
ledStatus = LOW;
digitalWrite(13, LOW);
digitalWrite(12, LOW);
}
else if (ledStatus == LOW && now - lowSince >= 1000)
{
highSince = now;
ledStatus = HIGH;
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
}
}
if (ch=='d'){//Polemiki kinitopoihsh
if(ledStatus == LOW && now - lowSince >= 0)
{
highSince = now;
ledStatus = HIGH;
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
}
else if(ledStatus == HIGH && now - highSince >= 180000)
{
lowSince = now;
ledStatus = LOW;
digitalWrite(13, LOW);
digitalWrite(12, LOW);
}
}
if (ch=='r'){//reset all
digitalWrite(13,LOW);
digitalWrite(12,LOW);
Serial.print("r\n");
}
if (ch=='e'){
if(ledStatus == LOW && now - lowSince >= 0)
{
highSince = now;
ledStatus = HIGH;
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
}
else if(ledStatus == HIGH && now - highSince >= 60000)
{
lowSince = now;
ledStatus = LOW;
digitalWrite(13, LOW);
digitalWrite(12, LOW);
}
Serial.print("e\n");
}
}//end of loop