In setup(), try to run serial write() only once using the if statement.
If the relay in the If statement is contacted, it is a serial write, but it must write only once.
What method should I use?
The if statement is already in place.
thanks before your help.
unsigned long past = 0;
int flag = 0;
int E0 = 2;
int E1 = 8;
int E2 = 11;
void setup()
{
pinMode(E0, INPUT_PULLUP);
pinMode(E1, INPUT_PULLUP);
pinMode(E2, INPUT_PULLUP);
//we need to specify 2x the desired baud rate
//as the Arduino lib assumes 16MHz
//so for 150bps spec 300
Serial.begin(300); //we need to spec 2x the desired freq
noInterrupts();
CLKPR = (1 << CLKPCE); //enable prescaler write
CLKPR = (1 << CLKPS0); //write CLKPS0 to give 0001 or /2
interrupts();
unsigned long now = millis();
if (digitalRead(E1) == LOW) // && digitalRead(11)==HIGH)// wait for android send data to serial, and give HIGH status for pin 10
{
Serial.write( 0x01 );
Serial.write( 0x01 );
}
else {
}
}//setup
void loop()
{
unsigned long now = millis();
if (now - past >= 1500) {
past = now;
flag = 1;
}
if (flag == 1 && digitalRead(E0) == LOW) // && digitalRead(11)==HIGH)// wait for android send data to serial, and give HIGH status for pin 10
{
//write 0x55 and check w/scope
Serial.write( 0x03 );
flag = 0;
}
else {
}
if (flag == 1 && digitalRead(E2) == LOW) // && digitalRead(11)==HIGH)// wait for android send data to serial, and give HIGH status for pin 10
{
Serial.write( 0x02 );
flag = 0;
}
else {
}
//500mS delay between chars (delay works at half-speed now...)
}//loop
There are three relays. Two of these relays are overlapped and executed. When the relay is turned on, the serial write must be executed once in one of their relays. The other relay will continue to write serial.
Have a variable to tell you that you have performed the serial write.
bool SerialWriteDone = false; //this controls the serial write
if(relays activated == true)
{
//test if a serial write has been done
if(SerialWriteDone == false)
{
//we have not done a serial write yet
do the serial write on this line
//we have done a serial write, set the variable to prevent further writes
SerialWriteDone = true;
}