Pages: [1]   Go Down
Author Topic: looping problem  (Read 656 times)
0 Members and 1 Guest are viewing this topic.
austria
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24364
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

austria
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46103
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

austria
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i tried this but it does not work.

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

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46103
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i tried this but it does not work.
if (scan = 1){
Of course it doesn't.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24364
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

austria
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

but this (scan > 0) also does not
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24364
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
scan = 0;
Is that in the correct place?
Logged

"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.

Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Something like:

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

austria
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24364
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Pages: [1]   Go Up
Jump to: