Go Down

Topic: Pizzo Buzzer Random Loop Help! (Read 772 times) previous topic - next topic

Steven33

Apr 11, 2018, 04:27 am Last Edit: Apr 11, 2018, 04:27 am by Steven33
I don't think the code is worth sharing since its just the buzzer attached to a pin and given a tone. Anywho, my problem is that I want to be able to have the program play the tone at random times. So if I have a servo moving constantly but in 1 sec it plays a tone but then in 10 secs, it happens to play the tone while the servo is uninterrupted. Any tips? I am new to programming :D  Thanks.

pert

Please post your full sketch. If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it's ok to add it as an attachment. Don't put your code in some external file service like dropbox, etc. We shouldn't need to go to an external website just to help you. I do feel it's reasonable to post a link to code hosted on GitHub or similar code hosting sites since that's an platform specifically designed for this sort of thing

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor then you will not  have access to this useful tool. I recommend using the standard Arduino IDE instead.

Grumpy_Mike

Quote
So if I have a servo moving constantly but in 1 sec it plays a tone but then in 10 secs, it happens to play the tone while the servo is uninterrupted.
This is not at all clear.
How do you interrupt a servo? Do you mean mechanically restrain it?
That would cause damage to the servo. You normally have no indication that a servo is blocked that the code can access, unless you fit some sort of feedback sensor like a rotary encoder.

Most tone generating libraries are blocking code so that will prevent a servo from working anyway.

So what do you actually want to do and why?

Steven33

#3
Apr 11, 2018, 03:32 pm Last Edit: Apr 12, 2018, 12:02 am by Steven33
Code: [Select]
Code: [Select]

/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.

  modified 8 Nov 2013
  by Scott Fitzgerald
  http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo servo1;// create servo object to control a servo
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
Servo servo8;

int pos = 0;// variable to store the servo position

void setup() {
  servo1.write(130);
  servo1.attach(0);
  servo2.write(40);
  servo2.attach(2);
  servo3.write(130);
  servo3.attach(3);
  servo4.write(40);
  servo4.attach(4);
  servo5.write(110);
  servo5.attach(5);
  servo6.write(40);
  servo6.attach(6);
  servo7.write(130);
  servo7.attach(7);
  servo8.write(30);
  servo8.attach(8);
  pinMode(10, OUTPUT);
  delay(2000);
}

void loop() {

  servo1.write(135);
  servo8.write(45);
  servo4.write(60);
  servo5.write(115);
  delay(15);

  tone(10, 243);

  for (pos = 0; pos <= 90; pos += 3.5) {
    servo2.write(pos);
    servo7.write(150 - pos);
    servo3.write(60 + pos);
    servo6.write(80 - pos);
    delay(10);
  }

  servo1.write(110);
  servo8.write(25);
  servo4.write(35);
  servo5.write(93);
  delay(15);

  tone(10, 4300);


  for (pos = 90; pos >= 0; pos -= 3.5) {
    servo2.write(pos);
    servo7.write(150 - pos);
    servo3.write(60 + pos);
    servo6.write(80 - pos);
    delay(10);

    tone(10, 5500);

  }
}


I want my quadruped to keep walking and playing random buzzer tones. In this version of my code, it just beeps at a constant rate. I want it to beep randomly and at random times, makes it sound less 'robotic' and more 'alive'

Grumpy_Mike

Quote
want it to beep randomly and random time
So use the random number function to generate the numbers in the tone call and delay so it is not the same every time.

Steven33

I'm new to programming (like 1 week new) so could you show how you would delay the random function? Thanks a ton  :D

Grumpy_Mike

Ok this is how programming works. Some one mentions a function so you RTFM here
https://www.arduino.cc/reference/en/language/functions/random-numbers/random/
Then you put that function inside the brackets of the delay function call. Thus telling the call to delay for a time given by the number of milliseconds returned by the random function.

Go Up