rpinewsymphony
Posts: 3
Joined: Mon Aug 19, 2019 2:13 pm

How can I make one delay for all sensor?

Sun Oct 13, 2019 4:11 am

This code data logs IR, Ultrasonic and a raindrop sensor but the code prints for about 2 milliseconds each time and it overwhelms our sd card, we would like to make the delay for about 30 second each time to print but right now we don't know how to do that. Thank You

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
import time, math
import os
from datetime import datetime

file = open("/home/pi/dataloggers2.csv", "a")

if os.stat("/home/pi/dataloggers2.csv").st_size == 0:
        file.write("Time,RainSensor1,RPM1,RPM2,RPM3,RPMave,Distance(cm)\n")

rpm = 0
rpm1 = 0
rpm2 = 0
elapse = 0
elapse1 = 0
elapse2 = 0
sensor = 24
sensor1 = 27
sensor2 = 22
pulse = 0
pulse1 = 0
pulse2 = 0
distance1 = 0
start_timer = time.time()

def init_GPIO():                    
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(sensor,GPIO.IN,GPIO.PUD_UP)

def calculate_elapse(channel):              
    global pulse, start_timer, elapse
    pulse+=1                                
    elapse = time.time() - start_timer      
    start_timer = time.time()               

def calculate_speed(r_cm):
    global pulse,elapse,rpm
    if elapse !=0:                          
        rpm = 1/elapse * 60
        
def init_interrupt():
    GPIO.add_event_detect(sensor, GPIO.FALLING, callback = calculate_elapse, bouncetime = 20)


def init_GPIO1():                   
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(sensor1,GPIO.IN,GPIO.PUD_UP)

def calculate_elapse1(channel):             
    global pulse1, start_timer, elapse1
    pulse1+=1                               
    elapse1 = time.time() - start_timer     
    start_timer = time.time()               

def calculate_speed1(r1_cm):
    global pulse1,elapse1,rpm1
    if elapse1 !=0:                         
        rpm1 = 1/elapse1 * 60
        
def init_interrupt1():
    GPIO.add_event_detect(sensor1, GPIO.FALLING, callback = calculate_elapse1, bouncetime = 20)


def init_GPIO2():                   
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(sensor2,GPIO.IN,GPIO.PUD_UP)

def calculate_elapse2(channel):             
    global pulse2, start_timer, elapse2
    pulse2+=1                              
    elapse2 = time.time() - start_timer     
    start_timer = time.time()               

def calculate_speed2(r2_cm):
    global pulse2,elapse2,rpm2,dist_km2,dist_meas2,km_per_sec2,km_per_hour2
    if elapse2 !=0:                         
        rpm2 = 1/elapse2 * 60
        
def init_interrupt2():
    GPIO.add_event_detect(sensor2, GPIO.FALLING, callback = calculate_elapse2, bouncetime = 20)

def ping():
    GPIO.setmode(GPIO.BCM)
       
    TRIG = 23
    ECHO = 18
    STATUS = 0
    
    GPIO.setup(TRIG,GPIO.OUT)
    GPIO.setup(ECHO,GPIO.IN)
    
    GPIO.output(TRIG, False)
    time.sleep(1)
    
    GPIO.output(TRIG, True)
    time.sleep(0.00001)
    GPIO.output(TRIG, False)
    
    while GPIO.input(ECHO)==0:
        pulse_start = time.time()
        
    while GPIO.input(ECHO)==1:
        pulse_end = time.time()
    global distance  
    pulse_duration = pulse_end - pulse_start
    
    distance = pulse_duration * 17150
    
    distance = round(distance, 2)
    
def isitwet():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(4, GPIO.IN)
    state = GPIO.input(4)
    distance1 = 0
    
    global status, now

    if (state==1):
        now = datetime.now()
        #print ("DRY\tDistance:",distance,"cm")
        #print("DRY")
        status = "DRY"
        sleep (1)
    else:
        now = datetime.now()
        #print ("WET\tDistance:",distance,"cm")
        #print("WET")
        status = "WET"
        sleep (1)

if _name_ == '_main_':
    init_GPIO()
    init_interrupt()
    init_GPIO1()
    init_interrupt1()
    init_GPIO2()
    init_interrupt2()

    while True:
        ping()
        isitwet()
        calculate_speed(6)
        calculate_speed1(6)
        calculate_speed2(6)
        rpmf = (rpm + rpm1 + rpm2) / 3
        print('rpm:{0:.0f}-RPM rpm1:{1:.0f}-RPM rpm2:{2:.0f} AveRPM:{3:.0f}-RPM Distance:{4}-cm Weather:{5}'.format(rpm,rpm1,rpm2,rpmf,distance,status))
        file.write(str(now)+","+str(status)+","+str(rpm)+","+str(rpm1)+","+str(rpm2)+","+str(rpmf)+","+str(distance)+"\n")
sleep(0.1)

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

Re: How can I make one delay for all sensor?

Sun Oct 13, 2019 7:38 am

If you want 30 seconds between each read of the sensors and subsequent print of the results then just add a 30 sleep like this

Code: Select all


    while True:
        ping()
        isitwet()
        calculate_speed(6)
        calculate_speed1(6)
        calculate_speed2(6)
        rpmf = (rpm + rpm1 + rpm2) / 3
        print('rpm:{0:.0f}-RPM rpm1:{1:.0f}-RPM rpm2:{2:.0f} AveRPM:{3:.0f}-RPM Distance:{4}-cm Weather:{5}'.format(rpm,rpm1,rpm2,rpmf,distance,status))
        file.write(str(now)+","+str(status)+","+str(rpm)+","+str(rpm1)+","+str(rpm2)+","+str(rpmf)+","+str(distance)+"\n")
        sleep(30)
        
        
now you while true loop will run and wait 30 seconds before running again.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”