Stop the sweep ?

Hi !
First, sorry for my english :slight_smile:

I try to do a little bot that could search an obstacle, and when it find, play a random melody.
My bot can turn with a micro servo and have an ultrasonic sensor to search an obstacle.
I’have try to use the “sweep” program, but the bot end the “sweep” fonction before find the obstacle…
I would like make the sweep fonction off when something is detect by the ultrasonic sensor.

That’s my code :

#include "pitches.h"
#include <Servo.h> 
#define NO_SOUND 0
Servo myservo;
const int trigPin = 4;
const int echoPin = 3;
int pos = 0;
long num;
boolean trouve = false;
int led = 2;

void setup() {
myservo.attach(5);
pinMode(led, OUTPUT);  
}

void loop() {
  long duration, inches, cm;
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  delay(100);
         if (cm == 10 || cm == 11 || cm == 12 || cm == 13 || cm == 14 || cm == 15 || cm == 16 || cm == 17 || cm == 18 || cm == 19 || cm == 20) 
    {
      trouve = true;
      trouver();
    }
        else if (pos == 0)
    {
      if (pos == 0) {
    myservo.write(90);
    return; }
    if (pos == 90) {
    myservo.write(0);
    return; }
    }
}


void trouver () {
    if (trouve)
 {
   num = random(4);
   digitalWrite(led, HIGH);
    if (num == 1)
  {
    melodie1();
  }
    if (num == 2)
  {
    melodie2();
  }
      if (num == 3)
  {
    melodie3();    
  }
}
trouve = false;
digitalWrite(led, LOW);
}

void melodie1() {
  int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);


    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

void melodie2() {
  int melody[] = {
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4,NO_SOUND
};

int noteDurations[] = {

  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,2,8,8,1,3
};
  for (int thisNote = 0; thisNote < 38; thisNote++) {

    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.50;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

void melodie3() {
  
  int melody[] = {

  NOTE_C4,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,
  NO_SOUND,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,
  NOTE_C4,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,
  NO_SOUND,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,
};

int noteDurations[] = {

  8,8,8,8,16,16,8,8,8,
  8,16,16,8,16,16,8,8,8,8,
  8,8,8,8,16,16,8,8,8,
  8,16,16,8,8,16,16,16,16,16,16,16,16,
};
  for (int thisNote = 0; thisNote < 41; thisNote++) {


    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.5;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

This is the 'sweep' code from your sketch, laid out slightly differently so I can make sense of it:

int pos = 0;
...
if (pos == 0)
{
    if (pos == 0)
    {
        myservo.write(90);
        return;
    }
    if (pos == 90)
    {
        myservo.write(0);
        return;
    }
}

I don't see how that would do anything other than move the servo to 90 degrees the first time it runs, and then leave it there.

If you want the servo to move incrementally and take distance readings at regular intervals then I suggest that instead of making it jump to 90 degrees in one go you increment the variable pos by a small amount and then send the servo to the resulting position. This means the servo would move a small distance each time loop() executed. You could control the servo speed by the amount you increment pos by, and how long loop() takes to complete. After you have got the servo speed under control you will need to deal with reversing the servo direction at the end of its travel. A simple but crude option would be to make it jump back to 0 (and then pause briefly to allow it to get there) before resuming the original increment. There are lots of more elegant solutions, but looking at the code so far I guess that simple and minimal changes are more important to you at this point.

Thanks for your answer :slight_smile:

I’ve try this but it’s not work…

#include "pitches.h"
#include <Servo.h> 
#define NO_SOUND 0
Servo myservo;
const int trigPin = 4;
const int echoPin = 3;
int pos = 0;
long num;
boolean trouve = false;
int led = 2;

void setup() {
myservo.attach(5);
pinMode(led, OUTPUT);  
}

void loop() {
  long duration, inches, cm;
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  delay(100);
         if (cm == 10 || cm == 11 || cm == 12 || cm == 13 || cm == 14 || cm == 15 || cm == 16 || cm == 17 || cm == 18 || cm == 19 || cm == 20 || cm == 21 || cm == 22 || cm == 23 || cm == 24 || cm == 25 || cm == 26 || cm == 27 || cm == 28 || cm == 29 || cm == 30) 
    {
      trouve = true;
      trouver();
    }
        else
    {
      if (pos == 0) {
    myservo.write(10);
    return; }
    if (pos == 10) {
    myservo.write(20);
    return;}
    if (pos == 20) {
    myservo.write(30);
    return;}
    if (pos == 30) {
    myservo.write(40);
    return;}
    if (pos == 40) {
    myservo.write(50);
    return;}
    if (pos == 50) {
    myservo.write(60);
    return;}
        if (pos == 60){
    myservo.write(0);
    return;}
  }
}



void trouver () {
    if (trouve)
 {
   num = random(4);
   digitalWrite(led, HIGH);
    if (num == 1)
  {
    melodie1();
  }
    if (num == 2)
  {
    melodie2();
  }
      if (num == 3)
  {
    melodie3();    
  }
}
trouve = false;
digitalWrite(led, LOW);
}

void melodie1() {
  int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);


    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

void melodie2() {
  int melody[] = {
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4,NO_SOUND
};

int noteDurations[] = {

  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,2,8,8,1,3
};
  for (int thisNote = 0; thisNote < 38; thisNote++) {

    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.50;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

void melodie3() {
  
  int melody[] = {

  NOTE_C4,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,
  NO_SOUND,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,
  NOTE_C4,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,
  NO_SOUND,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,
};

int noteDurations[] = {

  8,8,8,8,16,16,8,8,8,
  8,16,16,8,16,16,8,8,8,8,
  8,8,8,8,16,16,8,8,8,
  8,16,16,8,8,16,16,16,16,16,16,16,16,
};
  for (int thisNote = 0; thisNote < 41; thisNote++) {


    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.5;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

I've try this but it's not work...

It most certainly does work. It does something. That is work. What it does might not be what you want it to do, but we can't help you, since you did bother saying what it does and how that differs from what you want.

        if (cm == 10 || cm == 11 || cm == 12 || cm == 13 || cm == 14 || cm == 15 || cm == 16 || cm == 17 || cm == 18 || cm == 19 || cm == 20 || cm == 21 || cm == 22 || cm == 23 || cm == 24 || cm == 25 || cm == 26 || cm == 27 || cm == 28 || cm == 29 || cm == 30)Try printing the value of cm before doing this test. Does it ever equal one of the values that you are testing for ?

Then fix the code so that it tests for a value greater than or equal to 10 and less than or equal to 30. Imagine what would happen if you wanted to test 100 values or 1000 values. Would it be reasonable to do it the way that you have ?

So first, I’ve replace the original “if” :
if (cm >= 10 && cm <= 30)
The bot can detect correctly and play the melody,
But, when the bot search, it’s stay at the same pos, it’s not moving pos 10 to pos 20 for exemple…
I don’t know why…
My code is now like this :

#include "pitches.h"
#include <Servo.h> 
#define NO_SOUND 0
Servo myservo;
const int trigPin = 4;
const int echoPin = 3;
int pos = 0;
long num;
boolean trouve = false;
int led = 2;

void setup() {
myservo.attach(5);
pinMode(led, OUTPUT);  
}

void loop() {
  long duration, inches, cm;
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  delay(100);
         if (cm >= 10 && cm <= 30)
    {
      trouve = true;
      trouver();
    }
        else
    {
      if (pos == 0) {
    myservo.write(10);
    return; }
    if (pos == 10) {
    myservo.write(20);
    return;}
    if (pos == 20) {
    myservo.write(30);
    return;}
    if (pos == 30) {
    myservo.write(40);
    return;}
    if (pos == 40) {
    myservo.write(50);
    return;}
    if (pos == 50) {
    myservo.write(60);
    return;}
        if (pos == 60){
    myservo.write(0);
    return;}
  }
}



void trouver () {
    if (trouve)
 {
   num = random(4);
   digitalWrite(led, HIGH);
    if (num == 1)
  {
    melodie1();
  }
    if (num == 2)
  {
    melodie2();
  }
      if (num == 3)
  {
    melodie3();    
  }
}
trouve = false;
digitalWrite(led, LOW);
}

void melodie1() {
  int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);


    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

void melodie2() {
  int melody[] = {
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4,NO_SOUND
};

int noteDurations[] = {

  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,2,8,8,1,3
};
  for (int thisNote = 0; thisNote < 38; thisNote++) {

    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.50;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

void melodie3() {
  
  int melody[] = {

  NOTE_C4,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,
  NO_SOUND,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,
  NOTE_C4,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,
  NO_SOUND,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,
};

int noteDurations[] = {

  8,8,8,8,16,16,8,8,8,
  8,16,16,8,16,16,8,8,8,8,
  8,8,8,8,16,16,8,8,8,
  8,16,16,8,8,16,16,16,16,16,16,16,16,
};
  for (int thisNote = 0; thisNote < 41; thisNote++) {


    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.5;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
      if (pos == 0) {
    myservo.write(10);
    return; }
    if (pos == 10) {
    myservo.write(20);
    return;}
    if (pos == 20) {
    myservo.write(30);
    return;}
    if (pos == 30) {
    myservo.write(40);
    return;}
    if (pos == 40) {
    myservo.write(50);
    return;}
    if (pos == 50) {
    myservo.write(60);
    return;}
        if (pos == 60){
    myservo.write(0);
    return;}

Your coding style is horrid. NOTHING goes on the same line as the {. Fix that everywhere.

It would be easier to follow your code if every { was on a new line, too.

But, the biggest problem with this block of code is it’s tediousness. Regardless of the value of pos, you want to move the servo to another position 10 degrees away, except for the one special case where pos is 60. So:

if(pos < 60)
   myServo.write(pos + 10);
else
   myServo.write(0);

And, now it should be very clear that moving the servo and updating pos are NOT the same thing. You do one but not the other.

if(pos < 60)
  pos += 10;
else
  pos = 0;
myServo.write(pos);

fixes that problem.

Thank you ! It’s works like I want !
I post the final code :

#include "pitches.h"
#include <Servo.h> 
#define NO_SOUND 0
Servo myservo;
const int trigPin = 4;
const int echoPin = 3;
int pos = 0;
long num;
boolean trouve = false;
int led = 6;

void setup() {
myservo.attach(5);
pinMode(led, OUTPUT);  
}

void loop() {
  long duration, inches, cm;
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  delay(100);
         if (cm >= 10 && cm <= 30)
    {
      trouve = true;
      trouver();
    }
        else
    {
       if(pos < 60)
          pos += 5;
       else
          pos = 0;
        myservo.write(pos);
        delay(200);
        return;
  }
}



void trouver () {
    if (trouve)
 {
   num = random(4);
   digitalWrite(led, HIGH);
    if (num == 1)
  {
    melodie1();
  }
    if (num == 2)
  {
    melodie2();
  }
      if (num == 3)
  {
    melodie3();    
  }
}
trouve = false;
digitalWrite(led, LOW);
}

void melodie1() {
  int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);


    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

void melodie2() {
  int melody[] = {
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4,NO_SOUND
};

int noteDurations[] = {

  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,2,8,8,1,3
};
  for (int thisNote = 0; thisNote < 38; thisNote++) {

    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.50;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

void melodie3() {
  
  int melody[] = {

  NOTE_C4,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,
  NO_SOUND,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,NOTE_C2,
  NOTE_C4,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,
  NO_SOUND,NOTE_C2,NOTE_C2,NO_SOUND,NOTE_C2,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,NOTE_B3,
};

int noteDurations[] = {

  8,8,8,8,16,16,8,8,8,
  8,16,16,8,16,16,8,8,8,8,
  8,8,8,8,16,16,8,8,8,
  8,16,16,8,8,16,16,16,16,16,16,16,16,
};
  for (int thisNote = 0; thisNote < 41; thisNote++) {


    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.5;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}