if statement in void setup

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

What is it you are trying to achieve?

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.

Else {
return;
}

is not work for me.

Relays cannot write to Serial so what you say makes little sense

Please refer to the code. It seems more accurate than the poor person's explanation.

What does the code do that you don't expect?
What doesn't the code do that you do expect?

When If statement becomes true.
Perform serial write everytime if statement = true
However, there is no repetition.

coockoos:
However, there is no repetition.

No, that's the purpose of setup.
If you want repetition, put the code in loop()

Thanks but, I have to write it just once every time the value is true(Low) and immediately

Try the state-change example in the IDE.

Okay. What do you think about me referring to this article?

I have no opinion on the link.

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;
}

Is that anything like what is required?

Steve

I have no opinion on the link.

you are right.
thanks a lot.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.