2022年9月28日水曜日

ChallengeRaspi zerowh keyboard control smartcar





 








Thanks to
https://python.plainenglish.io/pi-diary-making-my-own-rc-car-using-raspberry-pi-e767559d82

# zerowh_keyboard_smartcar.py

from gpiozero import Robot

from tkinter import *

from gpiozero import LED


bot=Robot(left=(5,6),right=(13,19))

red = LED(12)

green = LED(16)

blue = LED(20)


red.off()

green.off()

blue.on()


main=Tk()


def leftKey(event):

    bot.left()

    print("left")

    red.off()

    green.on()

    blue.off()

    

def rightKey(event):

    bot.right()

    print("right")

    red.off()

    green.on()

    blue.off()

         

def upKey(event):

    bot.forward()

    print("forward")

    red.off()

    green.on()

    blue.off()

       

def downKey(event):

    bot.backward()

    print("backward")

    red.off()

    green.on()

    blue.off()

    

def ctrlKey(event):

    bot.stop()

    print("stop")

    red.on()

    green.off()

    blue.off()

    

def escKey(event):

    red.off()

    green.off()

    blue.off()

    import sys;sys.exit()

    

frame = Frame(main,width=100,height=100)

frame.bind('<Left>',leftKey)

frame.bind('<Right>',rightKey)

frame.bind('<Up>',upKey)

frame.bind('<Down>',downKey)

frame.bind('<Control_L>',ctrlKey)

frame.bind('<Escape>',escKey)

frame.focus_set()

frame.pack()

main.mainloop()

    

2022年9月22日木曜日

ChallengeRaspi_zerowh_motors


 ##zerowh_twomotors_drv8833.py

from gpiozero import Robot

import time


robot = Robot(left=(4,14),right=(23,24))


while True:

    print("start")

    robot.forward(speed=0.2)

    time.sleep(1)

    

    robot.stop()

    time.sleep(1)


    robot.backward(speed=1)

    time.sleep(1)


    robot.stop()

    time.sleep(1)

    

    robot.left(speed=1)

    time.sleep(0.5)

    

    robot.right(speed=1)

    time.sleep(0.5)

    

    robot.stop()

    time.sleep(1) 

    


##zerowh_twomotors_drv8833_3buttons.py

from gpiozero import Robot,Button

from signal import pause


robot = Robot(left=(4,14),right=(23,24))

blue=Button(10) ##Forward

white=Button(9) ##stop

red=Button(11)  ##backward


robot.stop


while True:

    blue.when_pressed=robot.forward

    white.when_pressed=robot.stop

    red.when_pressed=robot.backward

    

    pause()



##zerowh_oneservo_sg90.py

import RPi.GPIO as GPIO

import time


GPIO.setmode(GPIO.BCM)


gp_out=15

GPIO.setup(gp_out,GPIO.OUT)


servo = GPIO.PWM(gp_out,50)


servo.start(0)


while True:

    servo.ChangeDutyCycle(2.5)

    time.sleep(0.5)

    

    

    servo.ChangeDutyCycle(7.25)

    time.sleep(0.5)

    

    servo.ChangeDutyCycle(12)

    time.sleep(0.5)

    

    servo.ChangeDutyCycle(7.25)

    time.sleep(0.5)

    


##zero_onestepper_28byj48.py

import time

import RPi.GPIO as GPIO

from RpiMotorLib import RpiMotorLib


GpioPins=[21,20,16,7]


f=0.5

Rev=5

s_angle=1.8

wait=(1/f)*(s_angle/360) /2


mymotortest=RpiMotorLib.BYJMotor('MymotorOne','28BYJ')


mymotortest.motor_run(GpioPins,wait,Rev*50,True,False,'half',0.05)


GPIO.cleanup()



##zerowh_ir08h_3led.py

import RPi.GPIO as GPIO

from gpiozero import LED

import time


sensor=26

red=LED(6)

green=LED(5)


##GPIO.setmode(GPIO.BOARD)

GPIO.setup(sensor,GPIO.IN)


green.off()

red.off()


while True:

    if GPIO.input(sensor):

        red.off()

        green.on()

        print ("Object is not Detected")

        while GPIO.input(sensor):

            time.sleep(0.2)

    else:

        red.on()

        green.off()

        print ("Object is Detected") 

2022年9月18日日曜日

LearningKit 2022 for M5Stack Core2 Sensors








 

//M5Stack_Core2 Sensors

#include <M5Core2.h>

#include <Wire.h>

#include <AXP192.h>

#include <DHT.h>


int PIR = 19;

int Trig = 13;

int Echo = 14;

int IRPin = 25;

int IRstate = HIGH;

int LaserPin = 27;

double speedSound = 331.5 + 0.61 * 20;

double distance = 0;

boolean goon=true;


AXP192 power;

DHT dht(26, DHT11);  //DHT11


void setup() 

{

  M5.begin();

  M5.Lcd.setTextColor(WHITE); 

  M5.Lcd.setTextSize(2);

  

  M5.Lcd.setCursor(0, 0);

  M5.Lcd.println("Welcom to Core2 sensors");  

  M5.Lcd.setCursor(0, 20);

  M5.Lcd.println("Coded by Kim Grossa");


  dht.begin();


  pinMode(Echo,INPUT);

  pinMode(Trig,OUTPUT);

  pinMode(PIR,INPUT_PULLUP);

  pinMode(IRPin, INPUT);

  pinMode(LaserPin, OUTPUT);

}


void loop() 

{

  sensorDHT11();

  sensorPIR();

  sensorHCSR04();

  sensorIR08H();

  sensorLaser();


  M5.Lcd.setCursor(0, 180);

  M5.Lcd.setTextColor(GREEN,BLACK);

  M5.Lcd.print("Battery Level   ");

  M5.Lcd.printf("%3.4f V",power.GetBatVoltage());

   

  M5.update();

}


void sensorDHT11()

{

  delay(1000);


  float h = dht.readHumidity();

  float t = dht.readTemperature();

  

  M5.Lcd.setTextColor(WHITE,BLACK); 

  M5.Lcd.setTextSize(2);

  

  M5.Lcd.setCursor(0, 40);

  M5.Lcd.println("Humid");


  M5.Lcd.setCursor(150, 40);

  M5.Lcd.printf("%.1f\n", h);


  M5.Lcd.setCursor(0, 60);

  M5.Lcd.println("Temp");


  M5.Lcd.setCursor(150, 60);

  M5.Lcd.printf("%.1f\n", t); 

}


void sensorPIR()

{

  if(digitalRead(PIR)==HIGH)

  {

    M5.Lcd.setTextColor(RED,BLACK);

    M5.Lcd.setTextSize(2);

    M5.Lcd.setCursor(0, 80);

    

    M5.Lcd.println("Motion detected      ");  

  }

  else

  {

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(2);

    M5.Lcd.setCursor(0, 80);

    M5.Lcd.setTextSize(2);

    M5.Lcd.println("Motion not detected  ");    

  }

}


void sensorHCSR04()

{

  M5.Lcd.setTextColor(WHITE,BLACK); 

  M5.Lcd.setTextSize(2);

  

  digitalWrite(Trig, LOW); 

  delayMicroseconds(2);

  digitalWrite(Trig, HIGH );

  delayMicroseconds( 10 );

  digitalWrite(Trig, LOW );


  double t = pulseIn(Echo, HIGH);

  if (t > 0) 

  {

    t = t / 2;

    distance = t * speedSound * 100 / 1000000; 

     

    M5.Lcd.setCursor(0, 100);

    M5.Lcd.print(distance);

    M5.Lcd.setCursor(150, 100);

    M5.Lcd.print("cm");

    delay(500);  

  }

}


void sensorIR08H()

{

  IRstate = digitalRead(IRPin);

    if (IRstate == LOW)

    {

      M5.Lcd.setTextColor(RED,BLACK); 

      M5.Lcd.setCursor(0, 120);  

      M5.Lcd.print("Deteced        ");

    }

    else

    {

      M5.Lcd.setTextColor(GREEN,BLACK); 

      M5.Lcd.setCursor(0, 120);  

      M5.Lcd.print("Not Detected    ");

    }

    delay(1000);   

}


void sensorLaser()

  {

    digitalWrite (LaserPin, HIGH); 

    M5.Lcd.setTextColor(RED,BLACK); 

    M5.Lcd.setCursor(0, 140);  

    M5.Lcd.print("Laser is ON ");

    delay (1000); 

    

    digitalWrite (LaserPin, LOW); 

    M5.Lcd.setTextColor(GREEN,BLACK); 

    M5.Lcd.setCursor(0, 140);  

    M5.Lcd.print("Laser is OFF");

    delay (1000);

  }

LearningKit 2022 for M5Stack Gray Sensors



 

2022年9月16日金曜日

LearningKit 2022 for M5Stack Core2


 https://github.com/bwbguard/M5Stack-Core2-MediaPlayer



https://github.com/ABC10946/tetris_m5stack_core2


https://gist.github.com/mongonta0716/18c28bff2dc533d8b51903c939a66a61

2022年9月10日土曜日

Daiso 500Yen Offroad Car Kai1,Kai2,Kai3


 











//Daiso_500yen_OffRoadCar_Kai3

//Thanks to mukujii.sakura.ne.jp/esp1.html

//Thanks to www.freenove.com

//DRV8833-M1,M2


#include <WiFi.h>

const char* ssid     = "";      

const char* password = ""; 

 

const char html[] =

"<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>\

<style>input {font-size:16pt;margin:16px;width:160px;}\

div {font-size:32pt;color:blue;text-align:center;width:600px;border:medium solid black;}</style>\

<title>ESP32wrover WiFiSimpleCar</title></head>\

<body><div><p>WiFi_Controller</p>\

<form method='get'>\

<input type='submit' name='le' value='Left' />\

<input type='submit' name='fo' value='Forward' />\

<input type='submit' name='ri' value='Right' /><br>\

<input type='submit' name='st' value='Stop' /><br>\

<input type='submit' name='ba' value='Back'/><br><br>\

</form></div></body></html>";


void Forward()

{

  digitalWrite(18,HIGH);

  digitalWrite(19,LOW);

}


void Back()

{

  digitalWrite(18,LOW);

  digitalWrite(19,HIGH);

}


void MotorStop()

{

  digitalWrite(18,LOW);

  digitalWrite(19,LOW);

}


void Right()

{

  digitalWrite(22,HIGH);

  digitalWrite(23,LOW);

  delay(50);

  digitalWrite(22,LOW);

  digitalWrite(23,LOW);

  //delay(50);

}


void Left()

{

  digitalWrite(22,LOW);

  digitalWrite(23,HIGH);

  delay(50);

  digitalWrite(22,LOW);

  digitalWrite(23,LOW);

  //delay(50);

}


WiFiServer server(80);


void setup()

{

  pinMode(18,OUTPUT);

  pinMode(19,OUTPUT);

  pinMode(22,OUTPUT);

  pinMode(23,OUTPUT);


  digitalWrite(18,LOW);

  digitalWrite(19,LOW);

  digitalWrite(22,LOW);

  digitalWrite(23,LOW);

  

  Serial.begin(115200);

  delay(10);


  Serial.println();

  Serial.println();

  Serial.print("Connecting to ");

  Serial.println(ssid);


  WiFi.begin(ssid, password);


  while (WiFi.status() != WL_CONNECTED) 

   {

    delay(500);

    Serial.print(".");

   }


  Serial.println("");

  Serial.println("WiFi connected.");

  Serial.println("IP address: ");

  Serial.println(WiFi.localIP());


  server.begin();

}


void loop(){

   WiFiClient client = server.available();


   if (client) {

      Serial.println("New Client.");

      String currentLine = "";

      while (client.connected()) {

         if (client.available()) {

            char c = client.read();

            Serial.write(c);

               if (c == '\n') {

                  if (currentLine.length() == 0) {

                     client.println("HTTP/1.1 200 OK");

                     client.println("Content-type:text/html");

                     client.println();

                     client.print(html);

                     client.println();

                     break;

                  } else {

                     currentLine = "";

                  }

               } else if (c != '\r') {

                  currentLine += c;

               }


            if (currentLine.endsWith("GET /?fo")) {

              Forward();              

            }

            if (currentLine.endsWith("GET /?le")) {

              Left();

            }

            if (currentLine.endsWith("GET /?ri")) {

              Right();

            }

            if (currentLine.endsWith("GET /?ba")) {

              Back();

            }

            if (currentLine.endsWith("GET /?st")) {

               MotorStop();

            }         

         }

      }

      client.stop();

      Serial.println("Client Disconnected.");

   }

}

2022年9月4日日曜日

ESP32 wrover WiFi TamiyaBuggyCar


 








//ESP32_wrover_wifi-TamiyaBuggy

//Thanks to mukujii.sakura.ne.jp/esp1.html

//Thanks to www.freenove.com

//Thanks to http://nonbirikousaku.blog.fc2.com/blog-entry-36.html


#include <Wire.h>

#define DRV8830_ADDR 0x64

#define CONTROL 0x00


#include <ESP32Servo.h>

Servo myservo;

int posVal=0;

int servoPin=15;


#include <WiFi.h>

const char* ssid     = "";      

const char* password = "";

const char html[] =

"<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>\

<style>input {font-size:16pt;margin:16px;width:160px;}\

div {font-size:32pt;color:blue;text-align:center;width:600px;border:medium solid black;}</style>\

<title>ESP32wrover WiFiTamiyaBuggy</title></head>\

<body><div><p>WiFi_Controller</p>\

<form method='get'>\

<input type='submit' name='fl' value='ForwardLow' />\

<input type='submit' name='st' value='Stop' />\

<input type='submit' name='fh' value='ForwardHigh' /><br>\

<input type='submit' name='bl' value='BackLow' />\

<input type='submit' name='st' value='Stop' />\

<input type='submit' name='bh' value='BackHigh' /><br>\

<input type='submit' name='lh' value='LeftHigh' />\

<input type='submit' name='ce' value='Center' />\

<input type='submit' name='rh' value='RightHigh' /><br>\

<input type='submit' name='ll' value='LeftLow' />\

<input type='submit' name='ce' value='Center' />\

<input type='submit' name='rl' value='RightLow' /><br><br>\

</form></div></body></html>";


void MotorStop()

{

  Wire.beginTransmission(DRV8830_ADDR);

  Wire.write(CONTROL);

  Wire.write(0x00<<2|0x03); 

  Wire.endTransmission();   

}


void ForwardHigh()

{

  Wire.beginTransmission(DRV8830_ADDR);

  Wire.write(CONTROL);

  Wire.write(0x26<<2|0x01); //OUTPUT 3.05V

  Wire.endTransmission(); 

}


void ForwardLow()

{

  Wire.beginTransmission(DRV8830_ADDR);

  Wire.write(CONTROL);

  Wire.write(0x13<<2|0x01);   //OUTPUT 1.53V

  Wire.endTransmission(); 

}


void Center()

{

  myservo.write(90);

  delay(100); 

}


void BackHigh()

{

  Wire.beginTransmission(DRV8830_ADDR);

  Wire.write(CONTROL);

  Wire.write(0x26<<2|0x02); //OUTPUT 3.05V

  Wire.endTransmission();  

}


void BackLow()

{

  Wire.beginTransmission(DRV8830_ADDR);

  Wire.write(CONTROL);

  Wire.write(0x13<<2|0x02);   //OUTPUT 1.53V

  Wire.endTransmission();  

}


void RightHigh()

{

  myservo.write(160);

  delay(100);     

}


void RightLow()

{

  myservo.write(130);

  delay(100);    

}


void LeftHigh()

{

  myservo.write(20);

  delay(100);    

}


void LeftLow()

{

  myservo.write(50);

  delay(100);    

}


WiFiServer server(80);


void setup()

{  

  Serial.begin(115200);

  delay(10);

  

  Serial.println();

  Serial.println();

  Serial.print("Connecting to ");

  Serial.println(ssid);


  WiFi.begin(ssid, password);


  while (WiFi.status() != WL_CONNECTED) 

   {

    delay(500);

    Serial.print(".");

   }


  Serial.println("");

  Serial.println("WiFi connected.");

  Serial.println("IP address: ");

  Serial.println(WiFi.localIP());


  server.begin();


  Wire.begin();

  

  myservo.setPeriodHertz(50);

  myservo.attach(servoPin,500,2500);


  Center();

}


void loop()

{  

  WiFiClient client = server.available();


   if (client) {

      Serial.println("New Client.");

      String currentLine = "";

      while (client.connected()) {

         if (client.available()) {

            char c = client.read();

            Serial.write(c);

               if (c == '\n') {

                  if (currentLine.length() == 0) {

                     client.println("HTTP/1.1 200 OK");

                     client.println("Content-type:text/html");

                     client.println();

                     client.print(html);

                     client.println();

                     break;

                  } else {

                     currentLine = "";

                  }

               } else if (c != '\r') {

                  currentLine += c;

               }


            if (currentLine.endsWith("GET /?fl")) {

              ForwardLow();              

            }

            if (currentLine.endsWith("GET /?fh")) {

              ForwardHigh();

            }

            if (currentLine.endsWith("GET /?bl")) {

              BackLow();

            }

            if (currentLine.endsWith("GET /?bh")) {

              BackHigh();

            }

            if (currentLine.endsWith("GET /?st")) {

               MotorStop();

            }

            if (currentLine.endsWith("GET /?lh")) {

               LeftHigh();

            }

            if (currentLine.endsWith("GET /?rh")) {

               RightHigh();

            }

            if (currentLine.endsWith("GET /?ll")) {

               LeftLow();

            }

            if (currentLine.endsWith("GET /?rl")) {

               RightLow();

            }                        

            if (currentLine.endsWith("GET /?ce")) {

               Center();

            }              

         }

      }

      

      client.stop();

      Serial.println("Client Disconnected.");

   }

}