MicroPython Codes of the PicoBricks
from machine import Pin, PWM, I2C
from utime import sleep
import utime
from picobricks import SSD1306_I2C
import _thread
# Define the libraries
buzzer = PWM(Pin(20, Pin.OUT))
trigger = Pin(15, Pin.OUT)
echo = Pin(14, Pin.IN)
# Define the input and Output pins
WIDTH = 128
HEIGHT = 64
# OLED screen settings
sda = machine.Pin(4)
scl = machine.Pin(5)
i2c = machine.I2C(0, sda=sda, scl=scl, freq=1000000)
# Initialize digital pin 4 and 5 as an OUTPUT for OLED communication
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
measure = 0
def getDistance():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
return distance
def airPiano():
global measure
while True:
if 5 < measure < 11:
buzzer.duty_u16(4000)
buzzer.freq(262)
sleep(0.4)
elif 10 < measure < 16:
buzzer.duty_u16(4000)
buzzer.freq(294)
sleep(0.4)
# ... [Kodun geri kalanı bu şekilde devam ediyor]
# Play the tone determined by the value of the distance sensor
while True:
measure = int(getDistance())
oled.text("Distance " + str(measure) + " cm", 5, 30)
oled.show()
sleep(0.01)
oled.fill(0)
oled.show()
Arduino C Codes of the PicoBricks
#include <Wire.h>
#include "ACROBOTIC_SSD1306.h"
#include <NewPing.h>
#define TRIGGER_PIN 15
#define ECHO_PIN 14
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
#define T_C 262
#define T_D 294
#define T_E 330
#define T_F 349
#define T_G 392
#define T_A 440
#define T_B 493
const int Buzzer = 20;
void setup() {
pinMode(Buzzer,OUTPUT);
Wire.begin();
oled.init();
oled.clearDisplay();
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
}
void loop() {
delay(50);
int distance = sonar.ping_cm();
if(distance > 5 & distance < 11) {
tone(Buzzer, T_C);
}
// ... [Kodun geri kalanı bu şekilde devam ediyor]
oled.clearDisplay();
oled.setTextXY(2, 4);
oled.putString("Distance: ");
oled.setTextXY(4, 6);
String string_distance = String(distance);
oled.putString(string_distance);
oled.setTextXY(4, 8);
oled.putString("cm");
}
GitHub Page
Tags: