From 6a59478f401019daf557fb62566223eeeeff050e Mon Sep 17 00:00:00 2001 From: jimmy Date: Thu, 27 Jun 2024 20:30:02 -0500 Subject: [PATCH 1/1] different projects to learn new things --- RGB1602.py | 149 ++++++++++++++++++++++++++++++++++++++++++++ buttonLED-func.py | 12 ++++ dcmotorSetUp.py | 7 +++ favicon.ico | Bin 0 -> 318 bytes index.html | 37 +++++++++++ ledButton.py | 10 +++ ledScanner.py | 28 +++++++++ ledStranRAND.py | 42 +++++++++++++ ledTWINKLE.py | 48 ++++++++++++++ letITglow.py | 61 ++++++++++++++++++ letITglowthread.py | 72 +++++++++++++++++++++ luxTest.py | 18 ++++++ photocellCounter.py | 26 ++++++++ playSong.py | 114 +++++++++++++++++++++++++++++++++ tempSense.py | 4 ++ uk_trafficLights.py | 20 ++++++ warHammer.py | 145 ++++++++++++++++++++++++++++++++++++++++++ 17 files changed, 793 insertions(+) create mode 100644 RGB1602.py create mode 100644 buttonLED-func.py create mode 100644 dcmotorSetUp.py create mode 100644 favicon.ico create mode 100644 index.html create mode 100644 ledButton.py create mode 100644 ledScanner.py create mode 100644 ledStranRAND.py create mode 100644 ledTWINKLE.py create mode 100644 letITglow.py create mode 100644 letITglowthread.py create mode 100644 luxTest.py create mode 100644 photocellCounter.py create mode 100644 playSong.py create mode 100644 tempSense.py create mode 100644 uk_trafficLights.py create mode 100644 warHammer.py diff --git a/RGB1602.py b/RGB1602.py new file mode 100644 index 0000000..407ef1d --- /dev/null +++ b/RGB1602.py @@ -0,0 +1,149 @@ +# -*- coding: utf-8 -*- +import time +from machine import Pin,I2C + +RGB1602_SDA = Pin(4) +RGB1602_SCL = Pin(5) + +RGB1602_I2C = I2C(0,sda = RGB1602_SDA,scl = RGB1602_SCL ,freq = 400000) + +#Device I2C Arress +LCD_ADDRESS = (0x7c>>1) +RGB_ADDRESS = (0xc0>>1) + +#color define + +REG_RED = 0x04 +REG_GREEN = 0x03 +REG_BLUE = 0x02 +REG_MODE1 = 0x00 +REG_MODE2 = 0x01 +REG_OUTPUT = 0x08 +LCD_CLEARDISPLAY = 0x01 +LCD_RETURNHOME = 0x02 +LCD_ENTRYMODESET = 0x04 +LCD_DISPLAYCONTROL = 0x08 +LCD_CURSORSHIFT = 0x10 +LCD_FUNCTIONSET = 0x20 +LCD_SETCGRAMADDR = 0x40 +LCD_SETDDRAMADDR = 0x80 + +#flags for display entry mode +LCD_ENTRYRIGHT = 0x00 +LCD_ENTRYLEFT = 0x02 +LCD_ENTRYSHIFTINCREMENT = 0x01 +LCD_ENTRYSHIFTDECREMENT = 0x00 + +#flags for display on/off control +LCD_DISPLAYON = 0x04 +LCD_DISPLAYOFF = 0x00 +LCD_CURSORON = 0x02 +LCD_CURSOROFF = 0x00 +LCD_BLINKON = 0x01 +LCD_BLINKOFF = 0x00 + +#flags for display/cursor shift +LCD_DISPLAYMOVE = 0x08 +LCD_CURSORMOVE = 0x00 +LCD_MOVERIGHT = 0x04 +LCD_MOVELEFT = 0x00 + +#flags for function set +LCD_8BITMODE = 0x10 +LCD_4BITMODE = 0x00 +LCD_2LINE = 0x08 +LCD_1LINE = 0x00 +LCD_5x8DOTS = 0x00 + + +class RGB1602: + def __init__(self, col, row): + self._row = row + self._col = col + + self._showfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; + self.begin(self._row,self._col) + + + def command(self,cmd): + RGB1602_I2C.writeto_mem(LCD_ADDRESS, 0x80, chr(cmd)) + + def write(self,data): + RGB1602_I2C.writeto_mem(LCD_ADDRESS, 0x40, chr(data)) + + def setReg(self,reg,data): + RGB1602_I2C.writeto_mem(RGB_ADDRESS, reg, chr(data)) + + + def setRGB(self,r,g,b): + self.setReg(REG_RED,r) + self.setReg(REG_GREEN,g) + self.setReg(REG_BLUE,b) + + def setCursor(self,col,row): + if(row == 0): + col|=0x80 + else: + col|=0xc0; + RGB1602_I2C.writeto(LCD_ADDRESS, bytearray([0x80,col])) + + def clear(self): + self.command(LCD_CLEARDISPLAY) + time.sleep(0.002) + def printout(self,arg): + if(isinstance(arg,int)): + arg=str(arg) + + for x in bytearray(arg,'utf-8'): + self.write(x) + + + def display(self): + self._showcontrol |= LCD_DISPLAYON + self.command(LCD_DISPLAYCONTROL | self._showcontrol) + + + def begin(self,cols,lines): + if (lines > 1): + self._showfunction |= LCD_2LINE + + self._numlines = lines + self._currline = 0 + + + + time.sleep(0.05) + + + # Send function set command sequence + self.command(LCD_FUNCTIONSET | self._showfunction) + #delayMicroseconds(4500); # wait more than 4.1ms + time.sleep(0.005) + # second try + self.command(LCD_FUNCTIONSET | self._showfunction); + #delayMicroseconds(150); + time.sleep(0.005) + # third go + self.command(LCD_FUNCTIONSET | self._showfunction) + # finally, set # lines, font size, etc. + self.command(LCD_FUNCTIONSET | self._showfunction) + # turn the display on with no cursor or blinking default + self._showcontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF + self.display() + # clear it off + self.clear() + # Initialize to default text direction (for romance languages) + self._showmode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT + # set the entry mode + self.command(LCD_ENTRYMODESET | self._showmode); + # backlight init + self.setReg(REG_MODE1, 0) + # set LEDs controllable by both PWM and GRPPWM registers + self.setReg(REG_OUTPUT, 0xFF) + # set MODE2 values + # 0010 0000 -> 0x20 (DMBLNK to 1, ie blinky mode) + self.setReg(REG_MODE2, 0x20) + self.setColorWhite() + + def setColorWhite(self): + self.setRGB(255, 255, 255) diff --git a/buttonLED-func.py b/buttonLED-func.py new file mode 100644 index 0000000..d980dce --- /dev/null +++ b/buttonLED-func.py @@ -0,0 +1,12 @@ +from machine import Pin +import utime +led=Pin(15,Pin.OUT) +button=Pin(13,Pin.IN) +led1=Pin(16,Pin.OUT) +button1=Pin(14,Pin.IN) +def button_press(pin): + led.toggle() +def button1_press(pin): + led1.toggle() +button.irq(trigger=Pin.IRQ_RISING,handler=button_press) +button1.irq(trigger=Pin.IRQ_RISING,handler=button1_press) \ No newline at end of file diff --git a/dcmotorSetUp.py b/dcmotorSetUp.py new file mode 100644 index 0000000..f7f3eeb --- /dev/null +++ b/dcmotorSetUp.py @@ -0,0 +1,7 @@ +from machine import Pin, PWM +import time + +pwma=Pin(7,Pin.OUT) +ain1=PWM(Pin(12)) +ain2=PWM(Pin(11)) + diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..a5cee0bc25d117fa7bb57e683807c2a74ca11f09 GIT binary patch literal 318 zcmbtPF%E<<409wNP!?v!?)N|#*!mcD7jZ(mvAp~-RXg~)b z5`pLm`HIoGka}0->8f5SH&B>yg{szSJ3l|v%}Tn}GO7NZs(kN$83N6_N6x^10KQv6 UqW(^(C#)Y}*$WcunO-`32j*KHkpKVy literal 0 HcmV?d00001 diff --git a/index.html b/index.html new file mode 100644 index 0000000..164d328 --- /dev/null +++ b/index.html @@ -0,0 +1,37 @@ + + + + + + + + +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+

LEDs

+

Cat is state

+

Red is RState

+

Green is GSTate

+

Environmental Stats

+

Temperature is temp

+

Relative Humidity is relHum %

+ + \ No newline at end of file diff --git a/ledButton.py b/ledButton.py new file mode 100644 index 0000000..bd01fcf --- /dev/null +++ b/ledButton.py @@ -0,0 +1,10 @@ +from machine import Pin +import time + +led=Pin(15,Pin.OUT) +button=Pin(14,Pin.IN,Pin.PULL_DOWN) + +while True: + if button.value(): + led.toggle() + time.sleep(.0) diff --git a/ledScanner.py b/ledScanner.py new file mode 100644 index 0000000..6251abd --- /dev/null +++ b/ledScanner.py @@ -0,0 +1,28 @@ +from machine import Pin +import time + +# Set up LED pins +seg1 = Pin(13, Pin.OUT) +seg2 = Pin(12, Pin.OUT) +seg3 = Pin(11, Pin.OUT) +seg4 = Pin(10, Pin.OUT) +seg5 = Pin(9, Pin.OUT) + +# Create a list of our LEDs +segments = [seg1, seg2, seg3, seg4, seg5] + +while True: + + # For loop to turn each LED on then off in order of the list + for led in segments: + + led.value(1) + time.sleep(0.08) + led.value(0) + + # For loop in reverse, running backwards through the list + for led in reversed (segments): + + led.value(1) + time.sleep(0.08) + led.value(0) diff --git a/ledStranRAND.py b/ledStranRAND.py new file mode 100644 index 0000000..7817008 --- /dev/null +++ b/ledStranRAND.py @@ -0,0 +1,42 @@ +# Imports +from machine import Pin +from neopixel import NeoPixel +import time, random + +# LED details +GPIOnumber = 28 +LEDcount = 15 + +# Define the strand pin number and number of LEDs from variables +strand = NeoPixel(Pin(GPIOnumber), LEDcount) + +# Turn off all LEDs before program start +strand.fill((0,0,0)) +strand.write() +time.sleep(1) + +while True: + mycolour = random.randint(0,256),random.randint(0,256),random.randint(0,256) + + for i in range(LEDcount): + + strand[i] = (mycolour) + strand.write() + + # Show the light for this long + time.sleep(.09) + + #Clear the strand at the end of each loop + strand.fill((0,0,0)) + strand.write() + + for i in reversed (range(LEDcount)): + strand[i] =(mycolour) + strand.write() + + # Show the light for this long + time.sleep(.09) + + #Clear the strand at the end of each loop + strand.fill((0,0,0)) + strand.write() diff --git a/ledTWINKLE.py b/ledTWINKLE.py new file mode 100644 index 0000000..367edaa --- /dev/null +++ b/ledTWINKLE.py @@ -0,0 +1,48 @@ +# Imports +from machine import Pin +from neopixel import NeoPixel +import time, random + +# LED details +GPIOnumber = 28 +LEDcount = 15 + +# set up buttons +cBtn=Pin(16,Pin.IN,Pin.PULL_UP) +sBtn=Pin(17,Pin.IN,Pin.PULL_UP) + +# Define the strand pin number and number of LEDs from variables +strand = NeoPixel(Pin(GPIOnumber), LEDcount) + +global mycolour +mycolour=(78,0,45) +# Turn off all LEDs before program start +strand.fill((mycolour)) +strand.write() +time.sleep(1) + +def ledStrand(): + i=random.randint(0,LEDcount-1) + strand[i]=(0,0,0) + strand.write() + time.sleep(0.09) + strand[i]=(mycolour) + strand.write() + time.sleep(0.09) + +def nColor(): + global mycolour + mycolour=(random.randint(0,255),random.randint(0,255),random.randint(0,255)) + time.sleep(0.09) + +def clearStrand(): + strand.fill((0,0,0)) + strand.write() + +while True: + ledStrand() + if sBtn.value()==0: + clearStrand() + break + if cBtn.value()==0: + nColor() \ No newline at end of file diff --git a/letITglow.py b/letITglow.py new file mode 100644 index 0000000..61fc6e7 --- /dev/null +++ b/letITglow.py @@ -0,0 +1,61 @@ +from machine import Pin +from neopixel import NeoPixel +import time, random + +# Set up LED pins +seg1 = Pin(0, Pin.OUT) +seg2 = Pin(1, Pin.OUT) +seg3 = Pin(2, Pin.OUT) +seg4 = Pin(3, Pin.OUT) +seg5 = Pin(4, Pin.OUT) +seg6 = Pin(6, Pin.OUT) + +# Create a list of our LEDs +segments = [seg1, seg2, seg3, seg4, seg5, seg6] + +# Set up buttons +greenbtn = Pin(14, Pin.IN, Pin.PULL_DOWN) +redbtn = Pin(15, Pin.IN, Pin.PULL_DOWN) + +# Define the strip pin number (2) and number of LEDs (12) +ring = NeoPixel(Pin(16), 12) + +# Initialize ring +ring.fill((0,0,0)) +ring.write() +time.sleep(1) + +while True: + + # For loop to turn each LED on then off in order of the list + for led in segments: + + led.value(1) + time.sleep(0.08) + led.value(0) + + # For loop in reverse, running backwards through the list + for led in reversed (segments): + + led.value(1) + time.sleep(0.08) + led.value(0) + + # Create random RGB values + r = random.randint(0,100) + g = random.randint(0,100) + b = random.randint(0,100) + + for i in range(12): + + # Light each LED a random colour + ring[i] = (r,g,b) + ring.write() + + # Show the LED for this long + time.sleep(0.05) + + #Clear the ring at the end of each loop + ring.fill((0,0,0)) + ring.write() + diff --git a/letITglowthread.py b/letITglowthread.py new file mode 100644 index 0000000..ada0371 --- /dev/null +++ b/letITglowthread.py @@ -0,0 +1,72 @@ +from machine import Pin +from neopixel import NeoPixel +import time, _thread, random + +# Set up LED pins +seg1 = Pin(0, Pin.OUT) +seg2 = Pin(1, Pin.OUT) +seg3 = Pin(2, Pin.OUT) +seg4 = Pin(3, Pin.OUT) +seg5 = Pin(4, Pin.OUT) +seg6 = Pin(6, Pin.OUT) + +# Create a list of our LEDs +segments = [seg1, seg2, seg3, seg4, seg5, seg6] + +# Set up buttons +greenbtn = Pin(14, Pin.IN, Pin.PULL_DOWN) +redbtn = Pin(15, Pin.IN, Pin.PULL_DOWN) + +# Define the strip pin number (2) and number of LEDs (12) +ring = NeoPixel(Pin(16), 12) + +# Initialize ring +ring.fill((0,0,0)) +ring.write() +time.sleep(1) + +#r = random.randint(0,100) +#g = random.randint(0,100) +#b = random.randint(0,100) + +#while True: +def ledScan(): + # For loop to turn each LED on then off in order of the list + for led in segments: + + led.value(1) + time.sleep(0.08) + led.value(0) + # For loop in reverse, running backwards through the list + for led in reversed (segments): + + led.value(1) + time.sleep(0.08) + led.value(0) + +def colorRing(): + # Create random RGB values + r = random.randint(0,100) + g = random.randint(0,100) + b = random.randint(0,100) + + for i in range(12): + + # Light each LED a random colour + ring[i] = (r,g,b) + ring.write() + + # Show the LED for this long + time.sleep(0.05) + + #Clear the ring at the end of each loop + #ring.fill((0,0,0)) + ring.write() +while True: + #ledScan() + if greenbtn.value()==1: + _thread.start_new_thread(colorRing(),()) + if redbtn.value()==1: + ring.fill((0,0,0)) + ring.write() + ledScan() \ No newline at end of file diff --git a/luxTest.py b/luxTest.py new file mode 100644 index 0000000..0669a24 --- /dev/null +++ b/luxTest.py @@ -0,0 +1,18 @@ +from machine import Pin, I2C +import veml7700 +import time + +i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=10000) # connected on i2c0 + +veml = veml7700.VEML7700(address=0x10, i2c=i2c, it=100, gain=1/8) + +def measure(): + lux_val = veml.read_lux() + return(lux_val) + +while True: + lux_val=measure() + print(int(lux_val)) + time.sleep(1) +if KeyboardInterrupt: + pass \ No newline at end of file diff --git a/photocellCounter.py b/photocellCounter.py new file mode 100644 index 0000000..cd5786e --- /dev/null +++ b/photocellCounter.py @@ -0,0 +1,26 @@ +#!/usr/bin/env micropython +from machine import ADC, Pin +from time import sleep +from machine import RTC + + + +photoPIN=26 +led=Pin(13,Pin.OUT) +rtc=RTC() + + +def readLight(photoGP): + photoRes=ADC(Pin(26)) + light=photoRes.read_u16() + light=round(light/65535*100,2) + return light + +file=open('photoLog.txt','w') +while True: + file.write("light: "+str(readLight(photoPIN))+"%"+ str(rtc.datetime())+"\n") + file.flush() + print("light: " + str(readLight(photoPIN)) +"%") + print(rtc.datetime()) + sleep(900) + \ No newline at end of file diff --git a/playSong.py b/playSong.py new file mode 100644 index 0000000..5edcbea --- /dev/null +++ b/playSong.py @@ -0,0 +1,114 @@ +from machine import Pin, PWM +from utime import sleep +buzzer = PWM(Pin(16)) + +tones = { +"B0": 31, +"C1": 33, +"CS1": 35, +"D1": 37, +"DS1": 39, +"E1": 41, +"F1": 44, +"FS1": 46, +"G1": 49, +"GS1": 52, +"A1": 55, +"AS1": 58, +"B1": 62, +"C2": 65, +"CS2": 69, +"D2": 73, +"DS2": 78, +"E2": 82, +"F2": 87, +"FS2": 93, +"G2": 98, +"GS2": 104, +"A2": 110, +"AS2": 117, +"B2": 123, +"C3": 131, +"CS3": 139, +"D3": 147, +"DS3": 156, +"E3": 165, +"F3": 175, +"FS3": 185, +"G3": 196, +"GS3": 208, +"A3": 220, +"AS3": 233, +"B3": 247, +"C4": 262, +"CS4": 277, +"D4": 294, +"DS4": 311, +"E4": 330, +"F4": 349, +"FS4": 370, +"G4": 392, +"GS4": 415, +"A4": 440, +"AS4": 466, +"B4": 494, +"C5": 523, +"CS5": 554, +"D5": 587, +"DS5": 622, +"E5": 659, +"F5": 698, +"FS5": 740, +"G5": 784, +"GS5": 831, +"A5": 880, +"AS5": 932, +"B5": 988, +"C6": 1047, +"CS6": 1109, +"D6": 1175, +"DS6": 1245, +"E6": 1319, +"F6": 1397, +"FS6": 1480, +"G6": 1568, +"GS6": 1661, +"A6": 1760, +"AS6": 1865, +"B6": 1976, +"C7": 2093, +"CS7": 2217, +"D7": 2349, +"DS7": 2489, +"E7": 2637, +"F7": 2794, +"FS7": 2960, +"G7": 3136, +"GS7": 3322, +"A7": 3520, +"AS7": 3729, +"B7": 3951, +"C8": 4186, +"CS8": 4435, +"D8": 4699, +"DS8": 4978 +} + +song = ["E5","G5","A5","P","E5","G5","B5","A5","P","E5","G5","A5","P","G5","E5"] + +def playtone(frequency): + buzzer.duty_u16(2000) + buzzer.freq(frequency) + +def bequiet(): + buzzer.duty_u16(0) + +def playsong(mysong): + for i in range(len(mysong)): + if (mysong[i] == "P"): + bequiet() + else: + playtone(tones[mysong[i]]) + sleep(0.3) + bequiet() +playsong(song) \ No newline at end of file diff --git a/tempSense.py b/tempSense.py new file mode 100644 index 0000000..7aa6155 --- /dev/null +++ b/tempSense.py @@ -0,0 +1,4 @@ +import machine, si7021, time + +temp_sensor=si7021.Si7021(machine.I2C(0,sda=machine.Pin(0),scl=machine.Pin(1),freq=400000)) + diff --git a/uk_trafficLights.py b/uk_trafficLights.py new file mode 100644 index 0000000..a065bf4 --- /dev/null +++ b/uk_trafficLights.py @@ -0,0 +1,20 @@ +import machine +import utime + +led_red=machine.Pin(15,machine.Pin.OUT) +led_yellow=machine.Pin(14,machine.Pin.OUT) +led_green=machine.Pin(13,machine.Pin.OUT) + +while True: + led_red.value(1) + utime.sleep(5) + led_yellow.value(1) + utime.sleep(2) + led_red.value(0) + led_yellow.value(0) + led_green.value(1) + utime.sleep(5) + led_green.value(0) + led_yellow.value(1) + utime.sleep(5) + led_yellow.value(0) \ No newline at end of file diff --git a/warHammer.py b/warHammer.py new file mode 100644 index 0000000..29e0da5 --- /dev/null +++ b/warHammer.py @@ -0,0 +1,145 @@ +from machine import Pin, PWM +import time +import _thread + +red=PWM(Pin(15)) +red.freq(5000) +red.duty_u16(65535) +blue=PWM(Pin(14)) +blue.freq(5000) +blue.duty_u16(0) +green=PWM(Pin(13)) +green.freq(5000) +green.duty_u16(0) + +led1=Pin(16,Pin.OUT) #BR1 +led2=Pin(17,Pin.OUT) #BR2 +led3=Pin(18,Pin.OUT) #BR3 +led4=Pin(19,Pin.OUT) #BR4 +led5=Pin(20,Pin.OUT) #BR5 +br=[led1,led2,led3,led4,led5] #br[i].toggle() + +ledD=Pin(22,Pin.OUT) #blue LED +ledD.value(0) +ledA=Pin(21,Pin.OUT) #red LED +ledA.value(1) + +global state + + +button0=Pin(12,Pin.IN,Pin.PULL_DOWN) #control ledD&ledA +global button0_pressed +button0_pressed=False +def button0_reader_thread(): + global button0_pressed + while True: + if button0.value()==1: + button0_pressed=True + time.sleep(0.01) +_thread.start_new_thread(button0_reader_thread,()) +while True: + if button0_pressed==True: + ledA.toggle() + ledD.toggle() + time.sleep(1) + button0_pressed=False + +while True: + if state==0: + stateZero() + elif state==1: + stateOne() + elif state==2: + stateTwo() + elif state==3: + stateThree() + elif state==4: + stateFour() + elif state==5: + stateFive() + elif state==6: + stateSix() + elif state==7: + stateSeven() + +button1_presses=0 +last_time=0 + + +button1=Pin(11,Pin.IN,Pin.PULL_DOWN) #control RGB and phases + +def button1_pressed_handler(): + global button1_presses, last_time + new_time=time.ticks_ms() + if (new_time-last_time)>200: + button1_presses+=1 + last_time=new_time + +button1.irq(trigger=machine.Pin.IRQ_FALLING,handler=button1_pressed_handler) + +old_presses=0 +while True: + if button1_presses!=old_presses: + state=button1_presses + print(state) + old_presses=button1_presses + + +def stateZero(): + red.duty_u16(65535) + green.duty_u16(0) + blue.duty_u16(0) + #state=1 + return + +def stateOne(): + red.duty_u16(15000) + green.duty_u16(0) + blue.duty_u16(65535) + #state=2 + i+=1 + led[i].on() + return + +def stateTwo(): + red.duty_u16(65535) + green.duty_u16(0) + blue.duty_u16(65535) + #state=3 + return + +def stateThree(): + red.duty_u16(35000) + green.duty_u16(65535) + blue.duty_u16(0) + #state=4 + return + +def stateFour(): + red.duty_u16(15000) + green.duty_u16(15000) + blue.duty_u16(65535) + #state=5 + return + +def stateFive(): + red.duty_u16(65535) + green.duty_u16(15000) + blue.duty_u16(0) + #state=6 + return + +def stateSix(): + red.duty_u16(0) + green.duty_u16(65535) + blue.duty_u16(65535) + #state=7 + return + +def stateSeven(): + red.duty_u16(0) + green.duty_u16(0) + blue.duty_u16(0) + state=0 + button1_presses=0 + return -- 2.47.3