-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpico_car.py
737 lines (661 loc) · 23.6 KB
/
pico_car.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
import array, time, utime
import machine,onewire
from machine import Pin, PWM, I2C
from onewire import OneWire
from math import sqrt
import rp2
import framebuf
S1 = PWM(Pin(18))
S2 = PWM(Pin(19))
S3 = PWM(Pin(20))
S4 = PWM(Pin(21))
R_B = PWM(Pin(11))
R_A = PWM(Pin(10))
L_B = PWM(Pin(13))
L_A = PWM(Pin(12))
# register definitions
SET_CONTRAST = 0x81
SET_ENTIRE_ON = 0xa4
SET_NORM_INV = 0xa6
SET_DISP = 0xae
SET_MEM_ADDR = 0x20
SET_COL_ADDR = 0x21
SET_PAGE_ADDR = 0x22
SET_DISP_START_LINE = 0x40
SET_SEG_REMAP = 0xa0
SET_MUX_RATIO = 0xa8
SET_COM_OUT_DIR = 0xc0
SET_DISP_OFFSET = 0xd3
SET_COM_PIN_CFG = 0xda
SET_DISP_CLK_DIV = 0xd5
SET_PRECHARGE = 0xd9
SET_VCOM_DESEL = 0xdb
SET_CHARGE_PUMP = 0x8d
@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24)
def ws2812():
T1 = 2
T2 = 5
T3 = 3
wrap_target()
label("bitloop")
out(x, 1) .side(0) [T3 - 1]
jmp(not_x, "do_zero") .side(1) [T1 - 1]
jmp("bitloop") .side(1) [T2 - 1]
label("do_zero")
nop() .side(0) [T2 - 1]
wrap()
'''
library:
pciocar
ws2812b
ultrasonic
SSD1306
SSD1306_I2C
ir
ds18b20
'''
class pico_car:
def __init__(self):
S1.freq(100)
S2.freq(100)
S3.freq(100)
S4.freq(100)
R_B.freq(1000)
R_A.freq(1000)
L_B.freq(1000)
L_A.freq(1000)
self.current_speed = 0;
def Get_Properties(self):
results_dict = {
self.current_speed
}
return results_dict
def Car_Run(self, speed1, speed2):
speed1 = speed1*257
speed2 = speed2*257
R_B.duty_u16(0)
R_A.duty_u16(speed2)
L_B.duty_u16(speed1)
L_A.duty_u16(0)
def Car_Run_Forward(self, speed):
speed1 = speed*257
speed2 = speed*257
R_B.duty_u16(0)
R_A.duty_u16(speed2)
L_B.duty_u16(speed1)
L_A.duty_u16(0)
self.current_speed = speed;
def Car_Stop(self):
R_B.duty_u16(0)
R_A.duty_u16(0)
L_B.duty_u16(0)
L_A.duty_u16(0)
def Car_Back(self, speed1, speed2):
speed1 = speed1*257
speed2 = speed2*257
R_B.duty_u16(speed2)
R_A.duty_u16(0)
L_B.duty_u16(0)
L_A.duty_u16(speed1)
def Car_Run_Backward(self, speed):
speed1 = speed*257
speed2 = speed*257
R_B.duty_u16(speed2)
R_A.duty_u16(0)
L_B.duty_u16(0)
L_A.duty_u16(speed1)
def Car_Tank_Left(self, speed):
speed1 = speed*257
speed2 = speed*257
R_B.duty_u16(0)
R_A.duty_u16(speed2)
L_B.duty_u16(0)
L_A.duty_u16(speed1)
def Car_Tank_Right(self, speed):
speed1 = speed*257
speed2 = speed*257
R_B.duty_u16(speed2)
R_A.duty_u16(0)
L_B.duty_u16(speed1)
L_A.duty_u16(0)
def Car_Left(self, speed1, speed2):
speed1 = speed1*257
speed2 = speed2*257
R_B.duty_u16(0)
R_A.duty_u16(speed2)
L_B.duty_u16(0)
L_A.duty_u16(speed1)
def Car_Right(self, speed1, speed2):
speed1 = speed1*257
speed2 = speed2*257
R_B.duty_u16(speed2)
R_A.duty_u16(0)
L_B.duty_u16(speed1)
L_A.duty_u16(0)
def servo180(self, num, angle):
angle = angle*72.2222+3535
if num == 1:
S1.duty_u16(int(angle))
elif num == 2:
S2.duty_u16(int(angle))
elif num == 3:
S3.duty_u16(int(angle))
elif num == 4:
S4.duty_u16(int(angle))
def servo270(self, num, angle):
angle = angle*48.1481+3535
if num == 1:
S1.duty_u16(int(angle))
elif num == 2:
S2.duty_u16(int(angle))
elif num == 3:
S3.duty_u16(int(angle))
elif num == 4:
S4.duty_u16(int(angle))
def servo360(self, num, angle):
angle = angle*36.1111+3535
if num == 1:
S1.duty_u16(int(angle))
elif num == 2:
S2.duty_u16(int(angle))
elif num == 3:
S3.duty_u16(int(angle))
elif num == 4:
S4.duty_u16(int(angle))
#delay here is the reset time. You need a pause to reset the LED strip back to the initial LED
#however, if you have quite a bit of processing to do before the next time you update the strip
#you could put in delay=0 (or a lower delay)
class ir():
def __init__(self):
#initialization ir
self.ird = Pin(7,Pin.IN) #IR receiver pin interface is GPIO7
#IR decoder dictionary
self.act = {
"Power": "LLLLLLLLHHHHHHHHLLLLLLLLHHHHHHHH",
"Lights": "LLLLLLLLHHHHHHHHLHLLLLLLHLHHHHHH",
"Sounds": "LLLLLLLLHHHHHHHHHLHLLLLLLHLHHHHH",
"TurnLeft": "LLLLLLLLHHHHHHHHLLLHLLLLHHHLHHHH",
"TurnRight": "LLLLLLLLHHHHHHHHLHLHLLLLHLHLHHHH",
"+": "LLLLLLLLHHHHHHHHLLHHLLLLHHLLHHHH",
"-": "LLLLLLLLHHHHHHHHLHHHLLLLHLLLHHHH",
"1": "LLLLLLLLHHHHHHHHLLLLHLLLHHHHLHHH",
"2": "LLLLLLLLHHHHHHHHHLLLHLLLLHHHLHHH",
"3": "LLLLLLLLHHHHHHHHLHLLHLLLHLHHLHHH",
"4": "LLLLLLLLHHHHHHHHLLHLHLLLHHLHLHHH",
"5": "LLLLLLLLHHHHHHHHHLHLHLLLLHLHLHHH",
"6": "LLLLLLLLHHHHHHHHLHHLHLLLHLLHLHHH",
"7": "LLLLLLLLHHHHHHHHLLLHHLLLHHHLLHHH",
"8": "LLLLLLLLHHHHHHHHHLLHHLLLLHHLLHHH",
"9": "LLLLLLLLHHHHHHHHLHLHHLLLHLHLLHHH",
"0": "LLLLLLLLHHHHHHHHHLHHLLLLLHLLHHHH",
"Up": "LLLLLLLLHHHHHHHHHLLLLLLLLHHHHHHH",
"Down": "LLLLLLLLHHHHHHHHHLLHLLLLLHHLHHHH",
"Left": "LLLLLLLLHHHHHHHHLLHLLLLLHHLHHHHH",
"Right": "LLLLLLLLHHHHHHHHLHHLLLLLHLLHHHHH"
}
self.command_values = {
"Power": 0,
"Lights": 2,
"Sounds": 5,
"TurnLeft": 8,
"TurnRight": 10,
"+": 12,
"-": 14,
"1": 16,
"2": 17,
"3": 18,
"4": 20,
"5": 21,
"6": 22,
"7": 24,
"8": 25,
"9": 26,
"0": 13,
"Up": 1,
"Down": 9,
"Left": 4,
"Right": 6
}
def read_ircode(self):
wait = 1
complete = 0
seq0 = []
seq1 = []
while wait == 1:
if self.ird.value() == 0:
wait = 0
while wait == 0 and complete == 0:
start = utime.ticks_us()
while self.ird.value() == 0:
ms1 = utime.ticks_us()
diff = utime.ticks_diff(ms1,start)
seq0.append(diff)
while self.ird.value() == 1 and complete == 0:
ms2 = utime.ticks_us()
diff = utime.ticks_diff(ms2,ms1)
if diff > 10000:
complete = 1
seq1.append(diff)
code = ""
for val in seq1:
if val < 2000:
if val < 700:
code += "L"
else:
code += "H"
if code!="":
#print(code)
pass
command = ""
for k,v in self.act.items():
if code == v:
command = k
if command == "":
command = code
return command
def Getir(self):
ir_command = self.read_ircode()
value = self.command_values.get(ir_command)
return value
class ds:
def __init__(self, unit='c', resolution=12):
self.pin=7
self.no_addr=0
self.addr=self.getaddr()
self.unit=unit
self.res=resolution
def getaddr(self):
ow=OneWire(Pin(self.pin))
a=ow.scan()
for i in a:
self.no_addr+=1
return a
def read(self):
if self.no_addr==0:
print ("no sensors detected")
if self.no_addr>=1:
temp_array=[]
#print ('number of sensors: ',self.no_addr)
for i in range(1,self.no_addr+1):
temp_array.append(self._request(self.addr[i-1]))
return temp_array
def _request(self, addr):
self._res(addr)
ow=OneWire(Pin(self.pin))
ow.reset()
ow.select_rom(addr)
ow.writebyte(0x44) #command to take reading
if self.res==12: #the resolution determines the amount of time needed
time.sleep_ms(1000)
if self.res==11:
time.sleep_ms(400)
if self.res==10:
time.sleep_ms(200)
if self.res==9:
time.sleep_ms(100)
ow.reset() #reset required for data
ow.select_rom(addr)
ow.writebyte(0xBE) #command to send temperature data
#all nine bytes must be read
LSB=ow.readbyte() #least significant byte
MSB=ow.readbyte() #most significant byte
ow.readbyte()
ow.readbyte()
ow.readbyte() #this is the configuration byte for resolution
ow.readbyte()
ow.readbyte()
ow.readbyte()
ow.readbyte()
ow.reset() #reset at end of data transmission
#convert response to binary, format the binary string, and perform math
d_LSB=float(0)
d_MSB=float(0)
count=0
b=bin(LSB)
b2=bin(MSB)
b3=""
l=10-len(b2)
for i in range(l):
if len(b2)<10:
b3+="0"
b2=b3+b2
b4=""
l=10-len(b)
for i in range(l):
if len(b)<10:
b4+="0"
b5=b4+b
for i in b5:
if count == 2:
if i=='1':
d_LSB+=2**3
if count == 3:
if i=='1':
d_LSB+=2**2
if count == 4:
if i=='1':
d_LSB+=2**1
if count == 5:
if i=='1':
d_LSB+=2**0
if count == 6:
if i=='1':
d_LSB+=2**-1
if count == 7:
if i=='1':
d_LSB+=2**-2
if count == 8:
if i=='1':
d_LSB+=2**-3
if count == 9:
if i=='1':
d_LSB+=2**-4
count+=1
count=0
sign=1
for i in b2:
if count == 6:
if i=='1':
sign=-1
if count == 7:
if i=='1':
d_MSB+=2**6
if count == 8:
if i=='1':
d_MSB+=2**5
if count == 9:
if i=='1':
d_MSB+=2**4
count+=1
temp=(d_LSB+d_MSB)*sign
'''
if self.unit=='c'or self.unit=='C':
print("TEMP is: "+str(temp)+" degrees C")
if self.unit=='f'or self.unit=='F':
temp=(temp*9/5)+32
print("TEMP F is: "+str(temp))
'''
return temp
def _res(self,addr):
ow=OneWire(Pin(self.pin))
ow.reset()
ow.select_rom(addr)
ow.writebyte(0x4E)
if self.res==12:
ow.writebyte(0x7F)
ow.writebyte(0x7F)
ow.writebyte(0x7F)
#print ("12 bit mode")
if self.res==11:
ow.writebyte(0x5F)
ow.writebyte(0x5F)
ow.writebyte(0x5F)
#print ("11 bit mode")
if self.res==10:
ow.writebyte(0x3F)
ow.writebyte(0x3F)
ow.writebyte(0x3F)
#print ("10 bit mode")
if self.res==9:
ow.writebyte(0x1F)
ow.writebyte(0x1F)
ow.writebyte(0x1F)
#print ("9 bit mode")
ow.reset()
class ws2812b:
def __init__(self, num_leds, state_machine, delay=0.001):
self.pixels = array.array("I", [0 for _ in range(num_leds)])
self.sm = rp2.StateMachine(state_machine, ws2812, freq=8000000, sideset_base=Pin(6))
self.sm.active(1)
self.num_leds = num_leds
self.delay = delay
self.brightnessvalue = 255
# Set the overal value to adjust brightness when updating leds
def brightness(self, brightness = None):
if brightness == None:
return self.brightnessvalue
else:
if (brightness < 1):
brightness = 1
if (brightness > 255):
brightness = 255
self.brightnessvalue = brightness
# Create a gradient with two RGB colors between "pixel1" and "pixel2" (inclusive)
def set_pixel_line_gradient(self, pixel1, pixel2, left_red, left_green, left_blue, right_red, right_green, right_blue):
if pixel2 - pixel1 == 0: return
right_pixel = max(pixel1, pixel2)
left_pixel = min(pixel1, pixel2)
for i in range(right_pixel - left_pixel + 1):
fraction = i / (right_pixel - left_pixel)
red = round((right_red - left_red) * fraction + left_red)
green = round((right_green - left_green) * fraction + left_green)
blue = round((right_blue - left_blue) * fraction + left_blue)
self.set_pixel(left_pixel + i, red, green, blue)
# Set an array of pixels starting from "pixel1" to "pixel2" to the desired color.
def set_pixel_line(self, pixel1, pixel2, red, green, blue):
for i in range(pixel1, pixel2+1):
self.set_pixel(i, red, green, blue)
def set_pixel(self, pixel_num, red, green, blue):
# Adjust color values with brightnesslevel
blue = round(blue * (self.brightness() / 255))
red = round(red * (self.brightness() / 255))
green = round(green * (self.brightness() / 255))
self.pixels[pixel_num] = blue | red << 8 | green << 16
# rotate x pixels to the left
def rotate_left(self, num_of_pixels):
if num_of_pixels == None:
num_of_pixels = 1
self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels]
# rotate x pixels to the right
def rotate_right(self, num_of_pixels):
if num_of_pixels == None:
num_of_pixels = 1
num_of_pixels = -1 * num_of_pixels
self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels]
def show(self):
for i in range(self.num_leds):
self.sm.put(self.pixels[i],8)
time.sleep(self.delay)
def fill(self, red, green, blue):
for i in range(self.num_leds):
self.set_pixel(i, red, green, blue)
time.sleep(self.delay)
class SSD1306:
def __init__(self, width, height, external_vcc):
self.width = width
self.height = height
self.external_vcc = external_vcc
self.pages = self.height
# Note the subclass must initialize self.framebuf to a framebuffer.
# This is necessary because the underlying data buffer is different
# between I2C and SPI implementations (I2C needs an extra byte).
self.poweron()
self.init_display()
def init_display(self):
for cmd in (
SET_DISP | 0x00, # off
# address setting
SET_MEM_ADDR, 0x00, # horizontal
# resolution and layout
SET_DISP_START_LINE | 0x00,
SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
SET_MUX_RATIO, self.height - 1,
SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
SET_DISP_OFFSET, 0x00,
SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12,
# timing and driving scheme
SET_DISP_CLK_DIV, 0x80,
SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1,
SET_VCOM_DESEL, 0x30, # 0.83*Vcc
# display
SET_CONTRAST, 0xff, # maximum
SET_ENTIRE_ON, # output follows RAM contents
SET_NORM_INV, # not inverted
# charge pump
SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14,
SET_DISP | 0x01): # on
self.write_cmd(cmd)
self.fill(0)
self.show()
def poweroff(self):
self.write_cmd(SET_DISP | 0x00)
def contrast(self, contrast):
self.write_cmd(SET_CONTRAST)
self.write_cmd(contrast)
def invert(self, invert):
self.write_cmd(SET_NORM_INV | (invert & 1))
def show(self):
x0 = 0
x1 = self.width - 1
if self.width == 64:
# displays with width of 64 pixels are shifted by 32
x0 += 32
x1 += 32
self.write_cmd(SET_COL_ADDR)
self.write_cmd(x0)
self.write_cmd(x1)
self.write_cmd(SET_PAGE_ADDR)
self.write_cmd(0)
self.write_cmd(self.pages - 1)
self.write_framebuf()
def fill(self, col):
self.framebuf.fill(col)
def pixel(self, x, y, col):
self.framebuf.pixel(x, y, col)
def scroll(self, dx, dy):
self.framebuf.scroll(dx, dy)
def text(self, string, x, y, col=1):
self.framebuf.text(string, x, y, col)
def solid_rectangle(self, x, y, width, height):
for i in range(width):
for j in range(height):
self.framebuf.pixel((x+i), (y+j), 1)
def rectangle(self, x, y, width, height):
for i in range(width):
self.framebuf.pixel((x+i), y, 1)
self.framebuf.pixel((x+i), (y+height-1), 1)
for j in range(height):
self.framebuf.pixel(x, (y+j), 1)
self.framebuf.pixel((x+width-1),(y+j), 1)
class SSD1306_I2C(SSD1306):
def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False):
self.i2c = i2c
self.addr = addr
self.temp = bytearray(2)
# Add an extra byte to the data buffer to hold an I2C data/command byte
# to use hardware-compatible I2C transactions. A memoryview of the
# buffer is used to mask this byte from the framebuffer operations
# (without a major memory hit as memoryview doesn't copy to a separate
# buffer).
self.buffer = bytearray(((height // 8) * width) + 1)
self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height)
super().__init__(width, height, external_vcc)
def write_cmd(self, cmd):
self.temp[0] = 0x80 # Co=1, D/C#=0
self.temp[1] = cmd
self.i2c.writeto(self.addr, self.temp)
def write_framebuf(self):
# Blast out the frame buffer using a single I2C transaction to support
# hardware I2C interfaces.
self.i2c.writeto(self.addr, self.buffer)
def poweron(self):
pass
class ultrasonic():
def __init__(self):
self.Trig = Pin(0, Pin.OUT)
self.Echo = Pin(1, Pin.IN)
def Distance(self):
self.Trig.value(0)
time.sleep(0.000002)
self.Trig.value(1)
time.sleep(0.000015)
self.Trig.value(0)
t2 = 0
while not self.Echo.value():
t1 = 0
t1 = 0
while self.Echo.value():
t2 += 1
time.sleep(0.001)
#print ("distance_1 is %d " % ((t2 - t1)* 2.0192))
return ((t2 - t1)* 2.0192/10)
def Distance_accurate(self):
num = 0
ultrasonic = []
while num < 5:
distance = self.Distance()
#print("distance is %f"%(distance))
while int(distance) == -1 :
distance = self.Distance()
return int(999)
#print("Tdistance is %f"%(distance) )
while (int(distance) >= 500 or int(distance) == 0) :
distance = self.Distance()
return int(999)
#print("Edistance is %f"%(distance) )
ultrasonic.append(distance)
num = num + 1
time.sleep(0.01)
distance = (ultrasonic[1] + ultrasonic[2] + ultrasonic[3])/3
#print("distance is %f cm"%(distance) )
return int(distance)
class sensors:
def __init__(self):
self.sensor_temp = machine.ADC(4)
self.conversion_factor = 3.3 / (65535)
self.sensor_battery = machine.ADC(28)
def read_temperature(self):
reading = self.sensor_temp.read_u16() * self.conversion_factor
temperature_celcius = 27 - (reading - 0.706)/0.001721
temperature_fahrenheit = (temperature_celcius)*(9/5)+32
return (temperature_celcius, temperature_fahrenheit)
def check_battery_level(self):
battery_level_reading = self.sensor_battery.read_u16()
if (battery_level_reading < 400):
return "Battery: none"
elif (battery_level_reading < 20000):
return "Battery: low"
elif (battery_level_reading < 25000):
return "Battery: medium"
else:
return "Battery: full"
class buzzer:
def __init__(self):
# set buzzer pin
self.BZ = PWM(Pin(22))
self.BZ.freq(1000)
# Initialize music
#A4 frequency is 440Hz
#D is sqrt of C and D#
#F is sqrt of E and F#
#B is sqrt of G and upper D#
self.note_freq_dict = {
"C8":2*2093,"C7":2093,"C6":int(2093/2),"C5":int(2093/4),"C4":int(2093/8),
"D6#":int(sqrt(2)*2*2*220),"D5#":int(sqrt(2)*2*220),"D4#":int(sqrt(2)*220),
"D5":int(sqrt((2093/4)*(sqrt(2)*2*220))),"D4":int(sqrt((2093/8)*(sqrt(2)*220))),
"E8":2*2637,"E7":2637,"E6":int(2637/2),"E5":int(2637/4),"E4":int(2637/8),
"F6#":4*370,"F5#":2*370,"F4#":370,"F3#":int(370/2),
"F6":int(sqrt((4*370)*(2637/2))),"F5":int(sqrt((2*370)*(2637/4))),"F4":int(sqrt(370*(2637/8))),
"G6":2*784,"G5":784,"G4":int(784/2),"G3":int(784/4),"G2":int(784/8),"G1":int(784/16),
"A8":16*440,"A5":440*2,"A5":2*440,"A4":440,"A3":int(440/2),
"B5":int(sqrt(784*(sqrt(2)*2*2*220))),"B4":int(sqrt((784/2)*(sqrt(2)*2*220)))
}
def c4_major_scale(self):
print('Playing C Major Scale ...')
C4_Major = [
self.note_freq_dict["C4"],
self.note_freq_dict["D4"],
self.note_freq_dict["E4"],
self.note_freq_dict["F4"],
self.note_freq_dict["G4"],
self.note_freq_dict["A4"],
self.note_freq_dict["B4"],
self.note_freq_dict["C5"]
]
for i in range(len(C4_Major)):
self.BZ.duty_u16(500)
self.BZ.freq(C4_Major[i])
time.sleep(0.5)
self.BZ.duty_u16(0)
time.sleep(0.01)