Go Down

Topic: looping problem (Read 861 times) previous topic - next topic

mipumat

hi there,

i have a rotating table which listens to an "S" from serial. this "S" comes from a scannersoftware. when the table gets an "S" it moves 12 times. during this, the rotating table sends some characters to the scannersoftware to trigger some of its functions. when it made full 360 degrees it stops. and waits for another "S" of the scannersoftware.
the program of the turntable works. if i send an "S" with serial monitor, the table behaves as it should.

my problem with my code is that if the table gets another "S", while he is working, it will never stop again. unluckyly the scannersoftware does send additional "S" during its scanning process and i have no influence to change this.

so i am sure there is a simple way of for example let arduino listen to serial only on certain times or flush the lasersoftware "S" out of the communication but i am not clever enough to find it.

i am asking you nice people out there to help me. have a nice day.


Code: [Select]
void loop(){

  if (Serial.available() >0){     // prüft ob Kommunikation möglich
    Ser = Serial.read();          // com Port auslesen
  }


  if (Ser == 'S'){           
    delay(5000);                  // ersten Scan abwarten
    Serial.println('3');          // auf Texturmenü springen
    delay(1500);                  // warten
    Serial.println('G');          // Textur aufnehmen
    delay(1000);                  // warten
    Serial.println('4');          // auf SL Menü springen
    delay(1500);                  // warten
    Serial.println('A');          // Scan zur Liste hinzufügen

    for(int i=0;i<pos;i++){   
      step(true,stepstogo,500);     // Schrittmotor zur nächsten Position bewegen
      Serial.println('S');          // nächsten Scan beginnen 
      delay(5000);                  // Scan abwarten
      Serial.println('3');          // für Textur vorbereiten
      delay(1500);                  // warten
      Serial.println('G');          // Textur aufnehmen
      delay(1000);                  // warten
      Serial.println('4');          // auf SL Menü springen
      delay(1500);                  // warten
      Serial.println('A');          // Scan zur Liste hinzufügen
    }

  }

}

AWOL

You could set a flag the first time you receive an 'S', then later, if the flag is set, you can ignore any further 'S' you receive.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

mipumat

thats what i also thought. i tried setting a variable Scan = 1 when the "S" was received. then i did put the whole turntable motion into an if Scan == 1 loop but it did not work.

is there another way to set a flag and work with it?

PaulS

Quote
then later, if the flag is set, you can ignore any further 'S' you receive

When you are ready for another 'S', be sure to reset the flag.

mipumat

i tried this but it does not work.

Code: [Select]
void loop(){

  if (Serial.available() >0){     // prüft ob Kommunikation möglich
    Ser = Serial.read();          // com Port auslesen
  }


  if (Ser == 'S'){           
  scan = 1;
  }
 
  if (scan = 1){

    delay(5000);                  // ersten Scan abwarten
    Serial.println('3');          // auf Texturmenü springen
    delay(1500);                  // warten
    Serial.println('G');          // Textur aufnehmen
    delay(1000);                  // warten
    Serial.println('4');          // auf SL Menü springen
    delay(1500);                  // warten
    Serial.println('A');          // Scan zur Liste hinzufügen

    for(int i=0;i<pos;i++){   
      step(true,stepstogo,500);     // Schrittmotor zur nächsten Position bewegen
      Serial.println('S');          // nächsten Scan beginnen 
      delay(5000);                  // Scan abwarten
      Serial.println('3');          // für Textur vorbereiten
      delay(1500);                  // warten
      Serial.println('G');          // Textur aufnehmen
      delay(1000);                  // warten
      Serial.println('4');          // auf SL Menü springen
      delay(1500);                  // warten
      Serial.println('A');          // Scan zur Liste hinzufügen
    }

  }
  scan = 0;
}



PaulS

Quote
i tried this but it does not work.

if (scan = 1){
Of course it doesn't.

dxw00d

As an aside, having spent many years programming in Pascal, I do think it is done better there, using = for comparisons, and having a separate assignment operator.

AWOL

Algol did it with a different assignment ( ":=" )operator long before Pascal, and, if you squint a little, looks a little like a mathematical assignment " <- "
Why K&R chose to ape Basic of all things with the "=" assignment, heaven knows.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

mipumat

but this (scan > 0) also does not

AWOL

Code: [Select]
scan = 0;
Is that in the correct place?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

RaphaelAmin

Maybe it's the case to add && inside the if ...

Something like:

Code: [Select]

 if (Ser == 'S' && scan != 1) {  
   scan = 1;
   ...
  }

mipumat

got help in another forum but have to share here for other poor souls :)

the problem was using arduino 1.0 where some changes were made using serial communication. after this users tip i tried the code with arduino 23 and now serial.flush() works as expected.

keep in mind that arduino 1.0 reacts differently to arduino 0.23 when it comes to serial!

AWOL

It is worth pointing out that at no point in this thread did you
A) mention which version you were using
B) mention that you were using a Serial.flush
C) post full code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up