AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Timed loop?

Wed Jul 17, 2019 4:53 am

Is there a way to have a timed loop?

Im using a Pi Zero to control a large scale diorama im making where when the program starts when a button is pressed i have the engine of the model aircraft start up with lights and sound.
One of the lights on the aircraft is a strobe and after some experimenting today i got the timing down perfect but now comes the annoying part. The strobe has to run as long as the engine run sequence which is about 6 minutes. Is there a way to loop that specific code for the certain amount of time it will be running or do i need to figure out how many times it needs to repeat itself during the engine run.

Thanks

User avatar
jbeale
Posts: 3469
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Timed loop?

Wed Jul 17, 2019 5:24 am

It is certainly possible to run a program for <x> seconds and then stop and/or take some other action. I could give an example if you want but I'd need to know what language you are using- Bash shell script, Python, C/C++ or something else?

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Timed loop?

Wed Jul 17, 2019 5:52 am

jbeale wrote:
Wed Jul 17, 2019 5:24 am
It is certainly possible to run a program for <x> seconds and then stop and/or take some other action. I could give an example if you want but I'd need to know what language you are using- Bash shell script, Python, C/C++ or something else?
Im using Python

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Wed Jul 17, 2019 7:46 am

This seems to be related to your previous question about starting a timed loop with a button for a model display, https://www.raspberrypi.org/forums/view ... 9#p1469089

May be you should show us what code you already have as you will probably need to integrate the motor and strobe parts of your code.

Edit.

In case you want to try adding the strobe to your existing code here is an example that flashes an LED for how ever many minutes you set.

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(20, GPIO.OUT)
p = GPIO.PWM(20, 3) # you can change the flash speed by changing the 3 lower = slower higher = faster
p.start(50)

delay = 0.6 # number of mins to flash

end = ((60*delay) + time.time())
now = time.time()
while end > now:
    time.sleep(0.5)
    now = time.time()
    #you could do other things here

p.ChangeDutyCycle(0)
GPIO.output(20, GPIO.LOW)
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Timed loop?

Wed Jul 17, 2019 8:49 am

My code is set up as follows:

Code: Select all

import time
import RPi.GPIO as GPIO
GPIO.setmode (GPIO.BCM)
GPIO.setup (4, GPIO.OUT)

for x in range (0,5)
	GPIO.output(4, True)
	time.sleep(0.06)
	GPIO.output(4, False)
	time.sleep(0.06)
	GPIO.output(4, True)
	time.sleep(0.06)
	GPIO.output(4, False)
	time.sleep(1)
	

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Wed Jul 17, 2019 8:55 am

Do you want this strobe to flash and have the motor start / sound play ( from original post ) all in the one program , because if you your motor will start very slowly.

Edit.

but to make your strobe program work for X mins

Code: Select all

import time
import RPi.GPIO as GPIO
GPIO.setmode (GPIO.BCM)
GPIO.setup (20, GPIO.OUT)

delay = 1 # number of mins to flash

end = ((60*delay) + time.time())
now = time.time()
while end > now:
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    time.sleep(0.06)
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    time.sleep(1)
    now = time.time()
    

Last edited by pcmanbob on Wed Jul 17, 2019 9:43 am, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Timed loop?

Wed Jul 17, 2019 9:36 am

pcmanbob wrote:
Wed Jul 17, 2019 8:55 am
Do you want this strobe to flash and have the motor start / sound play ( from original post ) all in the one program , because if you do you can't do it like that, while you are starting the motor.
Yes. I feel it would be a lot easier to program everything into one program rather than have multiple programs going off one button push.

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Wed Jul 17, 2019 9:46 am

AeroFixerB1 wrote:
Wed Jul 17, 2019 9:36 am

Yes. I feel it would be a lot easier to program everything into one program rather than have multiple programs going off one button push.
Then it would help to know what gpio is being use for what device and how you are driving your motor , simple transistor or motor driver board ?

I have been working on a simple example but I don't if it would work for you with out knowing more about you hard ware setup.

Also is the time this has to run critical to the second ? IE do you need to be able to adjust it by the number of seconds or will just number of minutes ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Timed loop?

Wed Jul 17, 2019 10:37 am

pcmanbob wrote:
Wed Jul 17, 2019 9:46 am
AeroFixerB1 wrote:
Wed Jul 17, 2019 9:36 am

Yes. I feel it would be a lot easier to program everything into one program rather than have multiple programs going off one button push.
Then it would help to know what gpio is being use for what device and how you are driving your motor , simple transistor or motor driver board ?

I have been working on a simple example but I don't if it would work for you with out knowing more about you hard ware setup.
So what i have so far as far as hardware is the motor is a small DC coreless motor, the motor starting slowly is just fine as i want it to start slowly and if it stays slow its fine because max rpm i want to replicate is 2600rpm. I plan on using Pulse width modulation to control my motor speed.

As far as lights i have 1mm micro LEDs with 1/4W resistors. The only thing that worries me with these is they say that 3V is the lowest power setting for these which make them dim and that 9V to 12V is the best for them and i can remember what voltage the GPIO pins put out.If anything, the dim lights will look more scale.

The only thing that i think would draw the most power would me the Stereo Speaker Bonnet i have to give me sound and im using a 3W 4Ohm speaker

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Wed Jul 17, 2019 11:14 am

Ok so I think a small DC coreless motor is just a standard DC brushed motor ( correct me if I am wrong ) so you can probably dive it using a 2N2222 transistor just the same as I have, what ever you do, do not power the motor from the 3.3v pin on the pi gpio header use a separate psu.

The pi gpio is 3.3v so your LEDs might not work , my example just uses a standard red LED with a suitable current limiting resistor.

So having said all that this is how I have it all connected.

Image

Now for the code which uses your strobe code , the code from your original thread and a timing loop.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)

delay = 60 # number of seconds to run 

# this is set up for pwm for motor and strobe
motor = GPIO.PWM(25, 50) # motor speed

#strobe 
def strobe():
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    time.sleep(0.06)
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    


# main loop
print (" Start program")
while True:
    # loop waiting for button press
    while GPIO.input(21) == 1:
        time.sleep(0.2)

    # set up timing 
    end = (delay + time.time())

    # here is were you will play your sound file
    print(" this is were you will play your sound file using which ever program you choose ")
    
    # now we start the motor
    print ("Start motor")
    motor.start(0)
    for dc in range(20, 100, 3):  # adjust start speed by adjusting 20 value adjust max speed by reducing the 100 value
        motor.ChangeDutyCycle(dc)
        strobe()
        time.sleep(0.9)
        
    # time delay with motor at full speed 
    now = time.time()
    while end > now:
        strobe()
        time.sleep(1)
        now = time.time()
        #you could do other things here
    
    # end of loop so stop motor and strobe
    print ("loop complete stop motor/strobe go back to waiting for button press")
    motor.ChangeDutyCycle(0)

    

    

Now its got some notes in the program but to explain bits that you might want to change,

The variable delay ( line 10 in my editor ) controls how long your motor / strobe runs as you did not answer my question about accuracy I have made it work with seconds so you can adjust it to within 1 second.

The motor start and speed control is handled by the for loop ( line 43 in my editor ) ,
the first value ( set at 20 ) is the start speed for the motor you will need to adjust this for your motor lower = slower ( minimum is 0 )
the second value ( set to 100 ) is the max run speed you will need to adjust this down to get the scale speed you want ( maximum is 100, minimum is the first value + 4 but I doubt you will ever get that low)

I have not included any sound file calls as I don't know what your file format is or how you want to call it so that's up to you.

I have tested both the hardware set-up and code so it should work ok with some adjustment to match you motor/led.

any questions please ask :)
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Timed loop?

Wed Jul 17, 2019 11:31 am

pcmanbob wrote:
Wed Jul 17, 2019 11:14 am
Ok so I think a small DC coreless motor is just a standard DC brushed motor ( correct me if I am wrong ) so you can probably dive it using a 2N2222 transistor just the same as I have, what ever you do, do not power the motor from the 3.3v pin on the pi gpio header use a separate psu.

The pi gpio is 3.3v so your LEDs might not work , my example just uses a standard red LED with a suitable current limiting resistor.

So having said all that this is how I have it all connected.

Image

Now for the code which uses your strobe code , the code from your original thread and a timing loop.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)

delay = 60 # number of seconds to run 

# this is set up for pwm for motor and strobe
motor = GPIO.PWM(25, 50) # motor speed

#strobe 
def strobe():
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    time.sleep(0.06)
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    


# main loop
print (" Start program")
while True:
    # loop waiting for button press
    while GPIO.input(21) == 1:
        time.sleep(0.2)

    # set up timing 
    end = (delay + time.time())

    # here is were you will play your sound file
    print(" this is were you will play your sound file using which ever program you choose ")
    
    # now we start the motor
    print ("Start motor")
    motor.start(0)
    for dc in range(20, 100, 3):  # adjust start speed by adjusting 20 value adjust max speed by reducing the 100 value
        motor.ChangeDutyCycle(dc)
        strobe()
        time.sleep(0.9)
        
    # time delay with motor at full speed 
    now = time.time()
    while end > now:
        strobe()
        time.sleep(1)
        now = time.time()
        #you could do other things here
    
    # end of loop so stop motor and strobe
    print ("loop complete stop motor/strobe go back to waiting for button press")
    motor.ChangeDutyCycle(0)

    

    

Now its got some notes in the program but to explain bits that you might want to change,

The variable delay ( line 10 in my editor ) controls how long your motor / strobe runs as you did not answer my question about accuracy I have made it work with seconds so you can adjust it to within 1 second.

The motor start and speed control is handled by the for loop ( line 43 in my editor ) ,
the first value ( set at 20 ) is the start speed for the motor you will need to adjust this for your motor lower = slower ( minimum is 0 )
the second value ( set to 100 ) is the max run speed you will need to adjust this down to get the scale speed you want ( maximum is 100, minimum is the first value + 4 but I doubt you will ever get that low)

I have not included any sound file calls as I don't know what your file format is or how you want to call it so that's up to you.

I have tested both the hardware set-up and code so it should work ok with some adjustment to match you motor/led.

any questions please ask :)
Thank you so much for your help with this. But what is the PSU you mention? Also what if i wanted to start the strobe lets say, 60 seconds after the motor has started and then shut off say 30 seconds before the motor stops is that possible? My goal is to simulate that the pilot is turning the lights on after the engine has started

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Wed Jul 17, 2019 11:50 am

AeroFixerB1 wrote:
Wed Jul 17, 2019 11:31 am


Thank you so much for your help with this. But what is the PSU you mention? Also what if i wanted to start the strobe lets say, 60 seconds after the motor has started and then shut off say 30 seconds before the motor stops is that possible? My goal is to simulate that the pilot is turning the lights on after the engine has started
That is the separate power supply to power your motor , as I said you do not want to connect this to the 3.3v or 5v pins on the pi , the 3.3v will not power a motor as the motor would over load it and possibly damage your pi, and connecting a motor to 5v is not recommended as voltage spike caused by a motor can cause your pi to reboot or possibly damage it. ( i assumed motor was 3.7v as that's what I found when I looked up you motor description )

So is that 60 seconds from when the motor is started or 60 seconds from when it running at full speed ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Timed loop?

Wed Jul 17, 2019 12:11 pm

pcmanbob wrote:
Wed Jul 17, 2019 11:50 am
AeroFixerB1 wrote:
Wed Jul 17, 2019 11:31 am


Thank you so much for your help with this. But what is the PSU you mention? Also what if i wanted to start the strobe lets say, 60 seconds after the motor has started and then shut off say 30 seconds before the motor stops is that possible? My goal is to simulate that the pilot is turning the lights on after the engine has started
That is the separate power supply to power your motor , as I said you do not want to connect this to the 3.3v or 5v pins on the pi , the 3.3v will not power a motor as the motor would over load it and possibly damage your pi, and connecting a motor to 5v is not recommended as voltage spike caused by a motor can cause your pi to reboot or possibly damage it. ( i assumed motor was 3.7v as that's what I found when I looked up you motor description )

So is that 60 seconds from when the motor is started or 60 seconds from when it running at full speed ?

It would be 60 seconds after starting.

The time frame of the engine start sequence is:
the prop is turning at 33rpm for about 10 seconds so only 5.5 rotations.

At engine start the prop is turning around 800 to 1000 rpm and then slowly increases to 1400 rpm once the engine is warmed up.

Max rpm is 2600 when at full throttle

then the engine goes back to 1400rpm until shutdown.

This whole sequence is only about 4 minutes

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Wed Jul 17, 2019 12:26 pm

AeroFixerB1 wrote:
Wed Jul 17, 2019 12:11 pm


It would be 60 seconds after starting.

The time frame of the engine start sequence is:
the prop is turning at 33rpm for about 10 seconds so only 5.5 rotations.

At engine start the prop is turning around 800 to 1000 rpm and then slowly increases to 1400 rpm once the engine is warmed up.

Max rpm is 2600 when at full throttle

then the engine goes back to 1400rpm until shutdown.

This whole sequence is only about 4 minutes
That sequence would require a lot more programming, and I have no way of knowing how fast your motor needs to run to simulate the required RPM.

may be its time for you to built the hardware on a test bench / breadboard and start doing your own testing and programming, then you will know what's required to meet you RPM requirements.

I will look at adding the strobe start after 60 seconds and stop 30 seconds before motor.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Wed Jul 17, 2019 12:51 pm

So I have added the strobe start / stop .

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)

delay = 60 # number of seconds to run 

# this is set up for pwm for motor and strobe
motor = GPIO.PWM(25, 50) # motor speed

#strobe 
def strobe():
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    time.sleep(0.06)
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    


# main loop
print (" Start program")
while True:
    # loop waiting for button press
    while GPIO.input(21) == 1:
        time.sleep(0.2)

    # set up timing 
    now = time.time()
    end = (delay + now)
    strobestart = (now + 60)
    strobestop = ((now + delay) - 30)
    
    # here is were you will play your sound file
    print(" this is were you will play your sound file using which ever program you choose ")
    
    # now we start the motor
    print ("Start motor")
    motor.start(0)
    for dc in range(20, 50, 3):  # adjust start speed by adjusting 20 value adjust max speed by reducing the 100 value
        motor.ChangeDutyCycle(dc)
        now = time.time()
        if now >= strobestart:
            strobe()
        time.sleep(0.9)
        
    # time delay with motor at full speed 
    now = time.time()
    while end > now:
        if now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()
        #you could do other things here
    
    # end of loop so stop motor and strobe
    print ("loop complete stop motor/strobe go back to waiting for button press")
    motor.ChangeDutyCycle(0)

    

    
 
But unless you can tell me your motors RPM at max speed / voltage and what RPM it needs to do, to simulate the model propeller RPM ,
and provide a detailed speed / time lay out, I cant really help with the that part of your program as I don't know what's required to make your motor meet the requirements.

So the speed/time layout would need to be detailed like this.

motor off

motor start at 33 RPM for 10 seconds

ramp motor speed up to ?? RPM & hold for Y seconds

and so on

even then it would only be an approximation as I can't test with your motor.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

hippy
Posts: 5595
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Timed loop?

Wed Jul 17, 2019 1:35 pm

I would probably use threads to control the motor and strobe. For example this will do nothing for 5 seconds, then turn the motors on, two seconds later turn the strobe on, have both on for 5 seconds, turn the strobe off, then two seconds later turn the motor off -

Code: Select all

#!/bin/usr/python

import threading
import time

running = True
motorOn = False
strobeOn = False

def ThreadMotor():
  while running:
    if motorOn:
      ...
    else:
      time.sleep(0.1)

def ThreadStrobe():
  while running:
    if strobeOn:
      ...
    else:
      time.sleep(0.1)
      
try:

  threading.Thread(target=ThreadMotor).start()
  threading.Thread(target=ThreadStrobe).start()
  
  time.sleep(5)
  motorOn = True
  time.sleep(2)
  strobeOn = True
  time.sleep(5)
  strobeOn = False
  time.sleep(2)
  motorOn = False
  time.sleep(1)

except KeyboardInterrupt:
  pass
finally:
  running = False
There are more elegant ways to do that but it demonstrates the basic principles of threading.

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Wed Jul 17, 2019 1:49 pm

Hippy I never considered threading as its not some thing I have experience of , but I think from my example code so far its possible to do in a simple while loop , its just that the goal posts keep changing.

I wish when people post a question they would just layout what they are wanting to do, especially in this case so much more detail could have been supplied at the start.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Wed Jul 17, 2019 4:25 pm

So kept playing with this and this is how far I have got with the information supplied so far.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)

delay = 240 # number of seconds to run 

# this is set up for pwm for motor 
motor = GPIO.PWM(25, 100) # motor speed

#strobe 
def strobe():
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    time.sleep(0.06)
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    


# main loop
print (" Start program")
while True:
    # loop waiting for button press
    while GPIO.input(21) == 1:
        time.sleep(0.2)

    # set up timing 
    start = now = time.time()
    end = (delay + now)
    strobestart = now + 60
    strobestop = (now + delay) - 30
    warmup = (now + 100)

    # here is were you will play your sound file
    print(" this is were you will play your sound file using which ever program you choose ")
    
    # now we start the motor
    print ("Start motor")
    dc = 25
    motor.start(dc)
    time.sleep(5)
    
    # warm up period
    while now <= warmup:
        dc = dc + 0.09
        motor.ChangeDutyCycle(dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()
    
    print ("end of warm up")
    motor.ChangeDutyCycle(32)
    # end of warm up 
   
       
    # time delay with motor at full speed 
    now = time.time()
    while end > now:
        if now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()
       
    
    # end of loop so stop motor and strobe
    print ("loop complete stop motor/strobe go back to waiting for button press")
    motor.ChangeDutyCycle(0)

and this is what it looks like at least on my hardware.

https://youtu.be/neK0A2Ug1zY

So strobe start 60 after motor and stops 30 seconds before motor .

motor starts and goes through warm up to 1400 RPM so does that look ok for 1400 RPM for you model ?

might also help to say how it should look for each speed you want based on this example , as you have quoted speeds of 800,1400,2600 RPM

if you can give me a time/speed chart as I described in my other post I will see what I can do to give you a starting point for you code, that you can adjust for your motor.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Timed loop?

Thu Jul 18, 2019 1:05 am

pcmanbob wrote:
Wed Jul 17, 2019 4:25 pm
So kept playing with this and this is how far I have got with the information supplied so far.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)

delay = 240 # number of seconds to run 

# this is set up for pwm for motor 
motor = GPIO.PWM(25, 100) # motor speed

#strobe 
def strobe():
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    time.sleep(0.06)
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    


# main loop
print (" Start program")
while True:
    # loop waiting for button press
    while GPIO.input(21) == 1:
        time.sleep(0.2)

    # set up timing 
    start = now = time.time()
    end = (delay + now)
    strobestart = now + 60
    strobestop = (now + delay) - 30
    warmup = (now + 100)

    # here is were you will play your sound file
    print(" this is were you will play your sound file using which ever program you choose ")
    
    # now we start the motor
    print ("Start motor")
    dc = 25
    motor.start(dc)
    time.sleep(5)
    
    # warm up period
    while now <= warmup:
        dc = dc + 0.09
        motor.ChangeDutyCycle(dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()
    
    print ("end of warm up")
    motor.ChangeDutyCycle(32)
    # end of warm up 
   
       
    # time delay with motor at full speed 
    now = time.time()
    while end > now:
        if now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()
       
    
    # end of loop so stop motor and strobe
    print ("loop complete stop motor/strobe go back to waiting for button press")
    motor.ChangeDutyCycle(0)

and this is what it looks like at least on my hardware.

https://youtu.be/neK0A2Ug1zY

So strobe start 60 after motor and stops 30 seconds before motor .

motor starts and goes through warm up to 1400 RPM so does that look ok for 1400 RPM for you model ?

might also help to say how it should look for each speed you want based on this example , as you have quoted speeds of 800,1400,2600 RPM

if you can give me a time/speed chart as I described in my other post I will see what I can do to give you a starting point for you code, that you can adjust for your motor.
Thank you so much for posting this this is a huge help towards the final goal! I will put a better description together of what it should look like i could probably actually find a video of what it looks like. Ill mess with my bread board more to get everything fine tuned. I know ive changed the goal post a lot since i first started posting so im sorry for the confusion of things at times. It will be a couple days until i can mess with my motors as im waiting on parts to come in. by the way does anyone have a good part number for the PSU? all the ones i find on ebay look huge and i dont imagine it has to be that big.

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Thu Jul 18, 2019 5:19 am

It might help if you told us what voltage you need for your motor, but assuming 5v and your motor being so small , if that's all its going to power then any 5V 1A power supply would do, depending how often its likely to run you could even use batteries, they might be a quick option for testing.

I have no problem helping but just be aware I will not be on the forum much or have access to my pi's for the next 2 weeks.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Timed loop?

Thu Jul 18, 2019 10:59 am

pcmanbob wrote:
Thu Jul 18, 2019 5:19 am
It might help if you told us what voltage you need for your motor, but assuming 5v and your motor being so small , if that's all its going to power then any 5V 1A power supply would do, depending how often its likely to run you could even use batteries, they might be a quick option for testing.

I have no problem helping but just be aware I will not be on the forum much or have access to my pi's for the next 2 weeks.
Showing it only needs 3.2v to run max rpm. Here are the motors i have atm:
https://www.amazon.com/gp/product/B07CQ ... UTF8&psc=1

as far as what the motor is going to simulate for the engine start and run this a video of the engine starting:
https://www.youtube.com/watch?v=VZSDsWk9rdc&t=77s

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Thu Jul 18, 2019 12:24 pm

Seeing as they are only 3v then some thing like this will do as a power supply.

examples only.
https://www.amazon.com/s?k=3v+dc+psu&ref=nb_sb_noss

for 1 motor 500ma would be fine, as the motors don't have a current rating but they are very small, you could even measure the current draw from a couple of batteries to get an idea.

if you are going to be running more motors from the same supply then some thing bigger 1-2A may be required.

Edit.

So I grabbed the audio from that engine start video and edited it down to the start to stop about 3'42" in all so I will use that to base the motor speed control on, if that's suitable for your display ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pcmanbob
Posts: 6420
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Timed loop?

Thu Jul 18, 2019 3:56 pm

So I could go on editing this program forever but seeing as it really needs to be run on the actual hardware that's going to be used for the display, to allow it to be fine tuned to that motor / propeller I am going to stop.

So this is the code I came up with , it may not be the right way to do it or even the pretty way to do it , but it works and can be adjust for both section run time and pwm ramp up / down for each part of the sound track.

Code: Select all

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)



# this is set up for pwm for motor 
motor = GPIO.PWM(25, 100) # motor speed

#strobe 
def strobe():
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    time.sleep(0.06)
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    


# main loop
print (" Start program")
while True:
    # loop waiting for button press
    while GPIO.input(21) == 1:
        time.sleep(0.2)

    # set up timing 
    start = now = time.time()
    end = now + 224
    strobestart = now + 60
    strobestop = now + 192
    warmup = now + 81.5
    idel = now + 103.5
    fastidel = now + 151
    full = now + 180
    shutdown = now + 221.5

    # here is were you will play your sound file
    
    os.system("aplay /home/pi/engine.wav &")
    
    # now we start the motor
    print ("Start motor")
    dc = 23
    motor.start(dc)
    time.sleep(10)
    print("end of start")
    
    print ("warm up")
    # warm up period
    dc = 25
    motor.ChangeDutyCycle(dc)
    while now <= warmup:
        dc = dc + 0.012
        motor.ChangeDutyCycle(dc)
        print (dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()
    
    print ("end of warm up")
    print("idel")
    # idel period
    dc = 26
    motor.ChangeDutyCycle(dc)
    while now <= idel:
        
        motor.ChangeDutyCycle(dc)
        print (dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()
    
    print ("end of idel")
    
    print ("up to 1400")
    # 1400
    dc = 27
    motor.ChangeDutyCycle(dc)
    while now <= fastidel:
        dc = dc + 0.084
        motor.ChangeDutyCycle(dc)
        print (dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()   
    
           
    dc = 28
    motor.ChangeDutyCycle(dc)   
    print ("end of 1400") 
    time.sleep(0.3)
    
    
    print ("full speed")
    #full speed
    
    while now <= full:
        dc = dc + 0.5
        motor.ChangeDutyCycle(dc)
        print (dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()   

    print ("end of full speed")
    
    print ("shutdown")
    # shutdown
    dc = 39.5
    motor.ChangeDutyCycle(dc)
    while now <= shutdown:
        dc = dc - 0.95
        if dc < 24.5:
            dc = 24.5
        motor.ChangeDutyCycle(dc)
        print (dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()   

    print ("end") 
    dc = 0
    motor.ChangeDutyCycle(dc)    
    
    while now <= end:
        now = time.time()
        time.sleep(0.5)
    
       
    
    # end of loop so stop motor and strobe
    print ("loop complete stop motor/strobe go back to waiting for button press")

so I left all the print statements in that I was using to watch the execution and pwm values while making adjustments as I found it helpful.

and so this it what it looks like on my hardware , will probably look much better with an actual model aircraft and propeller.

https://youtu.be/i0w124BPl_w
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Timed loop?

Fri Jul 19, 2019 12:51 am

pcmanbob wrote:
Thu Jul 18, 2019 3:56 pm
So I could go on editing this program forever but seeing as it really needs to be run on the actual hardware that's going to be used for the display, to allow it to be fine tuned to that motor / propeller I am going to stop.

So this is the code I came up with , it may not be the right way to do it or even the pretty way to do it , but it works and can be adjust for both section run time and pwm ramp up / down for each part of the sound track.

Code: Select all

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)



# this is set up for pwm for motor 
motor = GPIO.PWM(25, 100) # motor speed

#strobe 
def strobe():
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    time.sleep(0.06)
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    


# main loop
print (" Start program")
while True:
    # loop waiting for button press
    while GPIO.input(21) == 1:
        time.sleep(0.2)

    # set up timing 
    start = now = time.time()
    end = now + 224
    strobestart = now + 60
    strobestop = now + 192
    warmup = now + 81.5
    idel = now + 103.5
    fastidel = now + 151
    full = now + 180
    shutdown = now + 221.5

    # here is were you will play your sound file
    
    os.system("aplay /home/pi/engine.wav &")
    
    # now we start the motor
    print ("Start motor")
    dc = 23
    motor.start(dc)
    time.sleep(10)
    print("end of start")
    
    print ("warm up")
    # warm up period
    dc = 25
    motor.ChangeDutyCycle(dc)
    while now <= warmup:
        dc = dc + 0.012
        motor.ChangeDutyCycle(dc)
        print (dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()
    
    print ("end of warm up")
    print("idel")
    # idel period
    dc = 26
    motor.ChangeDutyCycle(dc)
    while now <= idel:
        
        motor.ChangeDutyCycle(dc)
        print (dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()
    
    print ("end of idel")
    
    print ("up to 1400")
    # 1400
    dc = 27
    motor.ChangeDutyCycle(dc)
    while now <= fastidel:
        dc = dc + 0.084
        motor.ChangeDutyCycle(dc)
        print (dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()   
    
           
    dc = 28
    motor.ChangeDutyCycle(dc)   
    print ("end of 1400") 
    time.sleep(0.3)
    
    
    print ("full speed")
    #full speed
    
    while now <= full:
        dc = dc + 0.5
        motor.ChangeDutyCycle(dc)
        print (dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()   

    print ("end of full speed")
    
    print ("shutdown")
    # shutdown
    dc = 39.5
    motor.ChangeDutyCycle(dc)
    while now <= shutdown:
        dc = dc - 0.95
        if dc < 24.5:
            dc = 24.5
        motor.ChangeDutyCycle(dc)
        print (dc)
        if now >= strobestart and now <= strobestop:
            strobe()
        time.sleep(1)
        now = time.time()   

    print ("end") 
    dc = 0
    motor.ChangeDutyCycle(dc)    
    
    while now <= end:
        now = time.time()
        time.sleep(0.5)
    
       
    
    # end of loop so stop motor and strobe
    print ("loop complete stop motor/strobe go back to waiting for button press")

so I left all the print statements in that I was using to watch the execution and pwm values while making adjustments as I found it helpful.

and so this it what it looks like on my hardware , will probably look much better with an actual model aircraft and propeller.

https://youtu.be/i0w124BPl_w
Wow this is just amazing. Im blown away. Thank you so much for the help so far from all of you. Once i get the parts im waiting for in im going to get everything set up on my breadboard and do some adjusting to get everything perfect.

Thank you so much to all of you again who have helped me.

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Timed loop?

Fri Jul 19, 2019 2:15 am

pcmanbob wrote:
Wed Jul 17, 2019 11:14 am
Ok so I think a small DC coreless motor is just a standard DC brushed motor ( correct me if I am wrong ) so you can probably dive it using a 2N2222 transistor just the same as I have, what ever you do, do not power the motor from the 3.3v pin on the pi gpio header use a separate psu.

The pi gpio is 3.3v so your LEDs might not work , my example just uses a standard red LED with a suitable current limiting resistor.

So having said all that this is how I have it all connected.

Image

Now for the code which uses your strobe code , the code from your original thread and a timing loop.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)

delay = 60 # number of seconds to run 

# this is set up for pwm for motor and strobe
motor = GPIO.PWM(25, 50) # motor speed

#strobe 
def strobe():
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    time.sleep(0.06)
    GPIO.output(20, True)
    time.sleep(0.06)
    GPIO.output(20, False)
    


# main loop
print (" Start program")
while True:
    # loop waiting for button press
    while GPIO.input(21) == 1:
        time.sleep(0.2)

    # set up timing 
    end = (delay + time.time())

    # here is were you will play your sound file
    print(" this is were you will play your sound file using which ever program you choose ")
    
    # now we start the motor
    print ("Start motor")
    motor.start(0)
    for dc in range(20, 100, 3):  # adjust start speed by adjusting 20 value adjust max speed by reducing the 100 value
        motor.ChangeDutyCycle(dc)
        strobe()
        time.sleep(0.9)
        
    # time delay with motor at full speed 
    now = time.time()
    while end > now:
        strobe()
        time.sleep(1)
        now = time.time()
        #you could do other things here
    
    # end of loop so stop motor and strobe
    print ("loop complete stop motor/strobe go back to waiting for button press")
    motor.ChangeDutyCycle(0)

    

    

Now its got some notes in the program but to explain bits that you might want to change,

The variable delay ( line 10 in my editor ) controls how long your motor / strobe runs as you did not answer my question about accuracy I have made it work with seconds so you can adjust it to within 1 second.

The motor start and speed control is handled by the for loop ( line 43 in my editor ) ,
the first value ( set at 20 ) is the start speed for the motor you will need to adjust this for your motor lower = slower ( minimum is 0 )
the second value ( set to 100 ) is the max run speed you will need to adjust this down to get the scale speed you want ( maximum is 100, minimum is the first value + 4 but I doubt you will ever get that low)

I have not included any sound file calls as I don't know what your file format is or how you want to call it so that's up to you.

I have tested both the hardware set-up and code so it should work ok with some adjustment to match you motor/led.

any questions please ask :)
A question i forgot to ask with this diagram, where is the positive and negative side of the motor they are not labeled in this diagram or does in not matter what side of the rectifier its connected to

Return to “Beginners”