Hi
here's the code. I think i've gone down the wrong rabbit hole, as it is very predicable and regimented, not very random.
It should run on anyones python and you'll see why I don't think it's very random.
what I
thanks
# Python Script
#import RPi.GPIO as GPIO
from datetime import datetime, time
from time import sleep
import random, sys
in1 = 24
in2 = 23
en = 25
temp1=1
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(in1,GPIO.OUT)
#GPIO.setup(in2,GPIO.OUT)
#GPIO.setup(en,GPIO.OUT)
#GPIO.output(in1,GPIO.LOW)
#GPIO.output(in2,GPIO.LOW)
#p=GPIO.PWM(en,1000)
#p.start(25)
def getTime(h):
return h[:2] + ':' + h[2:]
def CalculateStart_EndTime():
global starthour, startminute, endhour, endminute, totalminutesawake, number_of_swings, Target_for_today, starttime, endtime
Target_for_today = random.randint(15000,20000)
starthour = random.randint(6,8)
startminute = random.randint(0,10)
endhour = random.randint(20,23)
endminute = random.randint(0,59)
totalminutesawake = ((60-startminute)+(endminute)+((endhour-(starthour+1))*60))
if startminute < 10 : startminute="0"+str(startminute)
starttime = getTime("0"+str(starthour)+str(startminute))
if endminute < 10 : endminute="0"+str(endminute)
endtime = getTime(str(endhour)+str(endminute))
print "I'm going to run for a total of",totalminutesawake,"minutes, starting at",starttime,"ending at",endtime
number_of_swings = random.randint(10, totalminutesawake)
print "I'm going to run",number_of_swings,"times today to get to",Target_for_today,"swings"
def CalcuateActualStartTimes():
# For the total number of swing points (number_of_swings) calculate how long after waking up it should start
global startdelay
startdelay = [0]
tmpstartdelay=[0]
number_of_swings_still_todo = number_of_swings
Target_for_today_still_todo = Target_for_today
# for 1 > number of swings to make, get a random number. that random number will be the time delay since the start time to do (Target_for_today/number_of_swings swings each time)
print "number_of_swings",number_of_swings
for i in range(1, number_of_swings):
delay = random.randint(0,totalminutesawake) # get a random number of minutes to delay from waking up
startdelay.append(delay)
#I've now got a whole lot of time delays, I need to order them
startdelay.sort()
#remove duplcates
[tmpstartdelay.append(x) for x in startdelay if x not in tmpstartdelay]
startdelay = tmpstartdelay
#Now I now how many times I will swing, I need to calc the avg number of swings
Avg_number_of_swings_to_take_each_time = Target_for_today/len(startdelay)
# this is how many swings I'd take in the day
predicted_swings_done_using_avg_swings = len(startdelay) * Avg_number_of_swings_to_take_each_time
#Check if it'd be enough swings in the day to meet the 15000 requirement
while predicted_swings_done_using_avg_swings < 15000:
Avg_number_of_swings_to_take_each_time = Avg_number_of_swings_to_take_each_time + random.randint(1,5)
predicted_swings_done_using_avg_swings = len(startdelay) * Avg_number_of_swings_to_take_each_time
print "I'd take",predicted_swings_done_using_avg_swings,"in the day if I use the average swings each time"
for i in startdelay:
print(i)
print "I swung",len(startdelay), "times today with",Avg_number_of_swings_to_take_each_time,"swings each time"
print "that means i swing every", totalminutesawake/len(startdelay),"minutes"
def StartRunning():
#Set it to turn forwards
GPIO.output(in1,GPIO.HIGH)
GPIO.output(in2,GPIO.LOW)
#set the run speed to fast
p.ChangeDutyCycle(50)
def Stoprunning():
GPIO.cleanup()
#print("GPIO Clean up")
def CheckIfCurrentTimeIsWithinRange():
now = datetime.now()
if time(int(starthour),int(startminute)) <= now.time() <= time(int(endhour),int(endminute)):
print "I should be running now since it's",now.time().strftime("%H:%M")
CalculateStart_EndTime()
CalcuateActualStartTimes()
CheckIfCurrentTimeIsWithinRange()
#print("run")
#StartRunning()
#sleep(5)
#print("stop")
#Stoprunning()