Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # imports
- import requests
- import json
- from nanpy import ArduinoApi
- from nanpy import SerialManager
- from time import sleep
- # from Assignments.print_temp_w_turtle import print_cool
- # from Assignments.print_temp_w_turtle import print_cold
- # from Assignments.print_temp_w_turtle import print_hot
- # from Assignments.print_temp_w_turtle import reset_turtle
- import temperature_from_arduino_support
- import turtle
- # setup the JSON
- def request_len():
- # request json file from webpage
- r = requests.get("https://formulae.brew.sh/api/formula.json")
- # save the https json request in json object
- packages_json = r.json()
- # retrieve packages
- package_name0 = packages_json[0]['name']
- package_name1 = packages_json[1]['name']
- print("requests")
- print(package_name0)
- print(package_name1)
- # compare lenghts
- res = len(package_name0) > len(package_name0)
- print("0 longer than 1:", res)
- return res
- ### SETUP THE TURTLE ###
- #tur = turtle.Turtle()
- #tur.speed(2)
- style = ('Bahnschrift', 70)
- #steps = 100
- # set mode:
- #__mode__ = 1
- ### setup connection to Arduino ###
- #usb_con = '' # path to usb-port where Arduino is connected
- try:
- connection = SerialManager()
- ard = ArduinoApi(connection = connection)
- except:
- print("Failed to connect to Arduino")
- #con = SerialManager(device=usb_con) # find connection
- #ard = ArduinoApi(connection=con) # create the arduino project
- ### Program the Arduino ###
- # setup pins
- # TODO - change these
- temp_pin = 14
- red_pin = 11
- green_pin = 10
- blue_pin = 9
- motor_pin = 12
- ard.pinMode(red_pin, ard.OUTPUT)
- ard.pinMode(green_pin, ard.OUTPUT)
- ard.pinMode(blue_pin, ard.OUTPUT)
- ard.pinMode(temp_pin, ard.INPUT)
- ard.pinMode(motor_pin, ard.OUTPUT)
- def red():
- ard.digitalWrite(red_pin, ard.HIGH)
- ard.digitalWrite(green_pin, ard.LOW)
- ard.digitalWrite(blue_pin, ard.HIGH)
- ard.digitalWrite(motor_pin, ard.HIGH)
- turtle.color('red')
- turtle.write('HOT', font = style)
- turtle.hideturtle()
- def yellow():
- ard.analogWrite(red_pin, 200)
- ard.analogWrite(green_pin, 50)
- ard.analogWrite(blue_pin, 0)
- ard.digitalWrite(motor_pin, ard.LOW)
- turtle.color('yellow')
- turtle.write('COOL', font = style)
- turtle.hideturtle()
- def green():
- ard.digitalWrite(red_pin, ard.LOW)
- ard.digitalWrite(green_pin, ard.HIGH)
- ard.digitalWrite(blue_pin, ard.LOW)
- ard.digitalWrite(motor_pin, ard.LOW)
- turtle.color('green')
- turtle.write('COLD', font = style)
- turtle.hideturtle()
- volt_const = 0.004882814
- # test the request
- if request_len():
- red()
- else:
- green()
- sleep(10)
- # main program
- while True:
- temp = ard.analogRead(temp_pin)
- temp = temp * volt_const
- temp = temp - 0.5
- temp = temp *100.0
- turtle.clear()
- if ( temp > 24 ):
- red()
- elif (temp > 21):
- yellow()
- else:
- green()
- print(temp)
- sleep(1)
- # end program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement