2022年8月28日日曜日

LearningKit 2022 for M5Stack Gray Controll DCmotor wiz DRV8830


 


//Learning Kit 2022 for M5Stack

//M5Stack_Gray_drv8830_dcmotor 

//Thanks to https://spiceman.jp/arduino-drv8830-program/


#include <M5Stack.h>

#include <Wire.h>

#define DRV8830_ADDR 0x64

#define CONTROL 0x00


void OpenScreen()

{

  M5.Lcd.setCursor(0,0);

  M5.Lcd.setTextColor(WHITE);

  M5.Lcd.setTextSize(2);

  M5.Lcd.print("Welcome to M5Stack Gray");

  M5.Lcd.setCursor(0,20);

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

  M5.Lcd.setCursor(0,40);

  M5.Lcd.print("Run DCmotor wiz DRV8830");

  

  M5.Lcd.setCursor(0,70);

  M5.Lcd.setTextColor(BLUE);

  M5.Lcd.setTextSize(3);

  M5.Lcd.print("ButtonA Reverse");

  M5.Lcd.setCursor(0,100);

  M5.Lcd.print("ButtonB Stop");

  M5.Lcd.setCursor(0,130);

  M5.Lcd.print("ButtonC Forward");

}


void setup() 

{

  M5.begin();

  M5.Power.begin();

  M5.Lcd.setBrightness(200);


  Wire.begin();

}


void loop() 

{

  boolean aBtn = M5.BtnA.wasPressed();

  boolean bBtn = M5.BtnB.wasPressed();

  boolean cBtn = M5.BtnC.wasPressed();

   

  OpenScreen(); 

   

  if(aBtn)

  { 

    //M5.Lcd.setCursor(0,180);

    //M5.Lcd.setTextColor(GREEN,BLACK);

    //M5.Lcd.setTextSize(3);

    //M5.Lcd.print("Reverse........");

    

    Wire.beginTransmission(DRV8830_ADDR);

    Wire.write(CONTROL);

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

    Wire.endTransmission();


    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Press any key");

  }


  if(bBtn)

  {

    //M5.Lcd.setCursor(0,180);

    //M5.Lcd.setTextColor(GREEN,BLACK);

    //M5.Lcd.setTextSize(3);

    //M5.Lcd.print("Stop........"); 


    Wire.beginTransmission(DRV8830_ADDR);

    Wire.write(CONTROL);

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

    Wire.endTransmission();

  

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Press any key");           

  }


  if(cBtn)

  { 

    //M5.Lcd.setCursor(0,180);

    //M5.Lcd.setTextColor(GREEN,BLACK);

    //M5.Lcd.setTextSize(3);

    //M5.Lcd.print("Forward......."); 

    

    Wire.beginTransmission(DRV8830_ADDR);

    Wire.write(CONTROL);

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

    Wire.endTransmission();

    

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Press any key");   

  }


  M5.update();

}

LearningKit 2022 for M5Stack Gray Controll Stepper wiz ULN2003


 


//Learning Kit 2022 for M5Stack

//M5Stack_Gray_uln2003_stepper 


#include <M5Stack.h>

#include <Stepper.h>

int i=0;

Stepper myStepper(2048,16,5,17,26);


void OpenScreen()

{

  M5.Lcd.setCursor(0,0);

  M5.Lcd.setTextColor(WHITE);

  M5.Lcd.setTextSize(2);

  M5.Lcd.print("Welcome to M5Stack Gray");

  M5.Lcd.setCursor(0,20);

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

  M5.Lcd.setCursor(0,40);

  M5.Lcd.print("Run stepper wiz ULN2003");

  

  M5.Lcd.setCursor(0,70);

  M5.Lcd.setTextColor(BLUE);

  M5.Lcd.setTextSize(3);

  M5.Lcd.print("ButtonA Reverse");

  M5.Lcd.setCursor(0,100);

  M5.Lcd.print("ButtonB Wiper");

  M5.Lcd.setCursor(0,130);

  M5.Lcd.print("ButtonC Forward");

}


void setup() 

{

  M5.begin();

  M5.Power.begin();

  M5.Lcd.setBrightness(200);


  myStepper.setSpeed(15);

}


void loop() 

{

  boolean aBtn = M5.BtnA.wasPressed();

  boolean bBtn = M5.BtnB.wasPressed();

  boolean cBtn = M5.BtnC.wasPressed();

   

  OpenScreen(); 

  

  if(aBtn)

  { 

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Reverse......"); 

    for(i=1;i<=2;i++)

    { 

      myStepper.step(2048);

    }

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Press any key"); 

  }


  if(bBtn)

  {

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Wiper........"); 

    for(i=1;i<=3;i++)

    {

      myStepper.step(512);

      delay(50);

      myStepper.step(-512);

    } 

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Press any key");           

  }


  if(cBtn)

  { 

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Forward......."); 

    for(i=1;i<=2;i++)

    {

      myStepper.step(-2048); 

    } 

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Press any key");   

  }


  M5.update();


}

2022年8月27日土曜日

LearningKit 2022 for M5Stack Gray MiniSlot


 //Learning Kit 2022 for M5Stack

//M5Stack_Gray_minislot 


#include <M5Stack.h>

int num,i=0;

int yourmoney=100;

String numstr,leftstr,centerstr,rightstr;


const uint8_t buttonA_GPIO = 39;

const uint8_t buttonB_GPIO = 38;

const uint8_t buttonC_GPIO = 37;

 

bool flag_btA_is_pressed = false;

bool flag_btB_is_pressed = false;

bool flag_btC_is_pressed = false;


static void buttonA_isr() {

  Serial.println("A interrupt");

  flag_btA_is_pressed = true;

}

 

static void buttonB_isr() {

  Serial.println("B interrupt");

  flag_btB_is_pressed = true;

}

 

static void buttonC_isr() {

  Serial.println("C interrupt");

  flag_btC_is_pressed = true;

}


void OpenScreen()

{

  M5.Lcd.setCursor(0,0);

  M5.Lcd.setTextColor(WHITE);

  M5.Lcd.setTextSize(2);

  M5.Lcd.print("Welcome to M5Stack Gray");

  M5.Lcd.setCursor(0,20);

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

  M5.Lcd.setCursor(0,40);

  M5.Lcd.print("Mini Slot");

  

  M5.Lcd.setCursor(0,60);

  M5.Lcd.setTextColor(BLUE);

  M5.Lcd.setTextSize(2);

  M5.Lcd.print("ButtonA New Game   ");

  M5.Lcd.setCursor(0,80);

  M5.Lcd.print("ButtonB No Action  ");

  M5.Lcd.setCursor(0,100);

  M5.Lcd.print("ButtonC Stop slot  ");

  M5.Lcd.setCursor(0,180);

  M5.Lcd.setTextColor(BLUE,BLACK);

  M5.Lcd.setTextSize(3);

  M5.Lcd.print("You have $" + String(yourmoney)+ "   ");

}


void SlotRotate()

{

  num=random(1,4);

  numstr=String(num);

  leftstr=numstr;

  M5.Lcd.setCursor(50,140);

  M5.Lcd.setTextColor(GREEN,BLACK);

  M5.Lcd.setTextSize(4);

  M5.Lcd.print(leftstr); 

  delay(50);

  num=random(1,4);

  numstr=String(num);

  centerstr=numstr;

  M5.Lcd.setCursor(150,140);

  M5.Lcd.setTextColor(GREEN,BLACK);

  M5.Lcd.setTextSize(4);

  M5.Lcd.print(centerstr);

  delay(50);

  num=random(1,4);

  numstr=String(num);

  rightstr=numstr;

  M5.Lcd.setCursor(250,140);

  M5.Lcd.setTextColor(GREEN,BLACK);

  M5.Lcd.setTextSize(4);

  M5.Lcd.print(rightstr);

  delay(50);    

}


void setup() 

{

  M5.begin();

  M5.Power.begin();

  M5.Lcd.setBrightness(200);

  OpenScreen();


  pinMode(buttonA_GPIO, INPUT);

  pinMode(buttonB_GPIO, INPUT);

  pinMode(buttonC_GPIO, INPUT);

 

  attachInterrupt(digitalPinToInterrupt(buttonA_GPIO), buttonA_isr, FALLING);

  attachInterrupt(digitalPinToInterrupt(buttonB_GPIO), buttonB_isr, FALLING);

  attachInterrupt(digitalPinToInterrupt(buttonC_GPIO), buttonC_isr, FALLING);

 

  delay(1000); 

}


void loop() 

  SlotRotate();

  if (flag_btA_is_pressed) 

    {

      Serial.println("Button A is pressed!");

      flag_btA_is_pressed = false; 

      M5.Power.reset();    

    }

 

  if (flag_btB_is_pressed) {

    Serial.println("Button B is pressed!");

    flag_btB_is_pressed = false;

  }

 

  if (flag_btC_is_pressed) 

  {

    Serial.println("Button C is pressed!");

    flag_btC_is_pressed = false;

    if ((leftstr==centerstr)&& (centerstr==rightstr))

      {

        M5.Lcd.setCursor(50,140);

        M5.Lcd.setTextColor(RED,BLACK);

        M5.Lcd.setTextSize(4);

        M5.Lcd.print(leftstr); 


        M5.Lcd.setCursor(150,140);

        M5.Lcd.setTextColor(RED,BLACK);

        M5.Lcd.setTextSize(4);

        M5.Lcd.print(centerstr);

      

        M5.Lcd.setCursor(250,140);

        M5.Lcd.setTextColor(RED,BLACK);

        M5.Lcd.setTextSize(4);

        M5.Lcd.print(rightstr); 

             

        M5.Lcd.setCursor(0,180);

        M5.Lcd.setTextColor(BLUE,BLACK);

        M5.Lcd.setTextSize(3);

        yourmoney=yourmoney+10;

        M5.Lcd.print("You have $" + String(yourmoney)+ "   ");

      }

      else

      {

        M5.Lcd.setCursor(0,180);

        M5.Lcd.setTextColor(BLUE,BLACK);

        M5.Lcd.setTextSize(3);

        yourmoney=yourmoney-10;

        M5.Lcd.print("You have $" + String(yourmoney)+ "   "); 

       }  

       delay(1000);

  }

  if(yourmoney<=0)

  {

    M5.Lcd.fillScreen(BLACK);

    M5.Lcd.setCursor(0,140);

    M5.Lcd.setTextColor(RED,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Game over        ");

    delay(1000);

    M5.Power.reset();

  }

        

  M5.update();

}

2022年8月26日金曜日

LearningKit 2022 for M5Stack Gray Controll DCmotor wiz DRB8833


 


//Learning Kit 2022 for M5Stack

//M5Stack_Gray_drv8833_dcmotor 


#include <M5Stack.h>


void OpenScreen()

{

  M5.Lcd.setCursor(0,0);

  M5.Lcd.setTextColor(WHITE);

  M5.Lcd.setTextSize(2);

  M5.Lcd.print("Welcome to M5Stack Gray");

  M5.Lcd.setCursor(0,20);

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

  M5.Lcd.setCursor(0,40);

  M5.Lcd.print("Run dcmotor wiz DRV8833");

  

  M5.Lcd.setCursor(0,70);

  M5.Lcd.setTextColor(BLUE);

  M5.Lcd.setTextSize(3);

  M5.Lcd.print("ButtonA Reverse");

  M5.Lcd.setCursor(0,100);

  M5.Lcd.print("ButtonB Stop");

  M5.Lcd.setCursor(0,130);

  M5.Lcd.print("ButtonC Forward");

}


void setup() 

{

  M5.begin();

  M5.Power.begin();

  M5.Lcd.setBrightness(200);


  pinMode(16,OUTPUT);

  pinMode(17,OUTPUT);

}


void loop() 

{

  boolean aBtn = M5.BtnA.wasPressed();

  boolean bBtn = M5.BtnB.wasPressed();

  boolean cBtn = M5.BtnC.wasPressed();

   

  OpenScreen(); 

  

  if(aBtn)

  { 

    //M5.Lcd.setCursor(0,180);

    //M5.Lcd.setTextColor(GREEN,BLACK);

    //M5.Lcd.setTextSize(3);

    //M5.Lcd.print("Reverse........");

    

    digitalWrite(16,LOW);

    digitalWrite(17,HIGH);

    

    //M5.Lcd.setCursor(0,180);

    //M5.Lcd.setTextColor(GREEN,BLACK);

    //M5.Lcd.setTextSize(3);

    //M5.Lcd.print("Press any key.."); 

  }


  if(bBtn)

  {

    //M5.Lcd.setCursor(0,180);

    //M5.Lcd.setTextColor(GREEN,BLACK);

    //M5.Lcd.setTextSize(3);

    //M5.Lcd.print("Stop.........");

    

    digitalWrite(16,LOW);

    digitalWrite(17,LOW);

    

    //M5.Lcd.setCursor(0,180);

    //M5.Lcd.setTextColor(GREEN,BLACK);

    //M5.Lcd.setTextSize(3);

    //M5.Lcd.print("Press any key");     

  }


  if(cBtn)

  { 

    //M5.Lcd.setCursor(0,180);

    //M5.Lcd.setTextColor(GREEN,BLACK);

    //M5.Lcd.setTextSize(3);

    //M5.Lcd.print("Forward......"); 

    

    digitalWrite(16,HIGH);

    digitalWrite(17,LOW);

    

    //M5.Lcd.setCursor(0,180);

    //M5.Lcd.setTextColor(GREEN,BLACK);

    //M5.Lcd.setTextSize(3);

    //M5.Lcd.print("Press any key.."); 

  }


  M5.update();


}

2022年8月25日木曜日

LearningKit 2022 for M5Stack Gray Servo Sweep


 



//Learning Kit 2022 for M5Stack

//M5Stack_Gray_runservo 


#include <M5Stack.h>

#include <ESP32Servo.h>


Servo myservo;


int servoPin=16;

int posVal=0;

int cnt=0;


void OpenScreen()

{

  M5.Lcd.setCursor(0,0);

  M5.Lcd.setTextColor(WHITE);

  M5.Lcd.setTextSize(2);

  M5.Lcd.print("Welcome to M5Stack Gray");

  M5.Lcd.setCursor(0,20);

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

  M5.Lcd.setCursor(0,40);

  M5.Lcd.print("Servo Sweep");

  

  M5.Lcd.setCursor(0,70);

  M5.Lcd.setTextColor(BLUE);

  M5.Lcd.setTextSize(3);

  M5.Lcd.print("ButtonA Reverse");

  M5.Lcd.setCursor(0,100);

  M5.Lcd.print("ButtonB Wiper");

  M5.Lcd.setCursor(0,130);

  M5.Lcd.print("ButtonC Forward");

}


void setup() 

{

  M5.begin();

  M5.Power.begin();

  M5.Lcd.setBrightness(200);


  myservo.setPeriodHertz(50);

  myservo.attach(servoPin,500,2500);

}


void loop() 

{

  boolean aBtn = M5.BtnA.wasPressed();

  boolean bBtn = M5.BtnB.wasPressed();

  boolean cBtn = M5.BtnC.wasPressed();

   

  OpenScreen(); 

  

  if(aBtn)

  { 

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Reverse........");

    

    for (cnt=0;cnt<=3;cnt +=1)

    {

      for (posVal = 0; posVal <= 180; posVal += 1) 

      { 

        myservo.write(posVal);       

        delay(15);                   

      } 

    }

    

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Press any key.."); 

  }


  if(bBtn)

  {

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Wiper.........");

    

    for (cnt=0;cnt<=5;cnt +=1)

    {

      for (posVal = 45; posVal <= 135; posVal += 10) 

      { 

        myservo.write(posVal);       

        delay(15);                   

      } 


      for (posVal = 135; posVal >= 45; posVal -= 10)

      { 

        myservo.write(posVal);

        delay(15);

      } 

    }

    

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Press any key");     

  }


  if(cBtn)

  { 

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Forward......"); 

    

    for (cnt=0;cnt<=3;cnt +=1) 

    {

      for (posVal = 180; posVal >= 0; posVal -= 1)

      { 

        myservo.write(posVal);

        delay(15);                   

      }

    }

    

    M5.Lcd.setCursor(0,180);

    M5.Lcd.setTextColor(GREEN,BLACK);

    M5.Lcd.setTextSize(3);

    M5.Lcd.print("Press any key.."); 

  }


  M5.update();


}

2022年8月22日月曜日

ESP32 wrover WiFi simplecar

 Tamiya No.189 71.4:1


Tamiya No.168 344.2:1


//ESP32_wrover_wifi-simplecar

//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);

  digitalWrite(22,HIGH);

  digitalWrite(23,LOW); 

}


void Back()

{

  digitalWrite(18,LOW);

  digitalWrite(19,HIGH);

  digitalWrite(22,LOW);

  digitalWrite(23,HIGH);

}


void MotorStop()

{

  digitalWrite(18,LOW);

  digitalWrite(19,LOW);

  digitalWrite(22,LOW);

  digitalWrite(23,LOW);

}


void Right()

{

  digitalWrite(18,LOW);

  digitalWrite(19,LOW);

  digitalWrite(22,HIGH);

  digitalWrite(23,LOW);

  delay(200);

  Forward();

}


void Left()

{

  digitalWrite(18,HIGH);

  digitalWrite(19,LOW);

  digitalWrite(22,LOW);

  digitalWrite(23,LOW);

  delay(200);

  Forward();

}


WiFiServer server(80);


void setup()

{

  pinMode(18,OUTPUT);

  pinMode(19,OUTPUT);

  pinMode(22,OUTPUT);

  pinMode(23,OUTPUT);

  

  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年8月21日日曜日

ESP32 wrover WiFi crane


 










//ESP32_wrover_wifi-crane

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

//Thanks to www.freenove.com

//KeyStudio 4RelayB M1,M2

//Stepper1 28BYJ-48 5VDC, Stepper2 28BYJ-48 5VDC

//M1 Tamiya No.189, M2 Tamiya No.189

#include <Wire.h>

#include <SSD1306Wire.h>

SSD1306Wire display(0x3c,21,22);


int outPorts1[]={14,27,26,25};

int outPorts2[]={15,2,4,5};


#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 WiFiCrane</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='jd' value='JibDown' />\

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

<input type='submit' name='ju' value='JibUp' /><br> \

<input type='submit' name='lr' value='LeftRotate' />\

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

<input type='submit' name='rr' value='RighRotate' /><br><br>\

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


void Forward()

{

  digitalWrite(18,HIGH);

  digitalWrite(19,LOW);

  digitalWrite(13,HIGH);

  digitalWrite(23,LOW);

}


void Back()

{

  digitalWrite(18,LOW);

  digitalWrite(19,HIGH);

  digitalWrite(13,LOW);

  digitalWrite(23,HIGH); 

}


void MotorStop()

{

  digitalWrite(18,LOW);

  digitalWrite(19,LOW);

  digitalWrite(13,LOW);

  digitalWrite(23,LOW);

}


void Right()

{

  digitalWrite(18,LOW);

  digitalWrite(19,HIGH);

  digitalWrite(13,HIGH);

  digitalWrite(23,LOW);  

  delay(1000);

  Forward();     

}


void Left()

{

  digitalWrite(18,HIGH);

  digitalWrite(19,LOW);

  digitalWrite(13,LOW);

  digitalWrite(23,HIGH);   

  delay(1000);

  Forward();       

}


void JibUp()

{

  moveAngle1(false, 15, 3);

  delay(500);

  moveAngle1(false, 0, 3);

  delay(1000); 

}


void JibDown()

{

  moveAngle1(true, 15, 3);

  delay(500); 

  moveAngle1(true, 0, 3);

  delay(1000); 

}


void RightRotate()

{

  moveAngle2(true, 30, 3);

  delay(500);

  moveAngle2(true, 0, 3);

  delay(1000);    

}


void LeftRotate()

{

  moveAngle2(false, 30, 3);

  delay(500); 

  moveAngle2(false, 0, 3);

  delay(1000);   

}


void moveSteps1(bool dir, int steps, byte ms) {

  for (unsigned long i = 0; i < steps; i++) {

    moveOneStep1(dir); 

    delay(constrain(ms,3,20));      

  }

}


void moveOneStep1(bool dir) {

  static byte out = 0x01;

  if (dir) {  

    out != 0x08 ? out = out << 1 : out = 0x01;

  }

  else {      

    out != 0x01 ? out = out >> 1 : out = 0x08;

  }

  for (int i = 0; i < 4; i++) {

    digitalWrite(outPorts1[i], (out & (0x01 << i)) ? HIGH : LOW);

  }

}


void moveAround1(bool dir, int turns, byte ms){

  for(int i=0;i<turns;i++)

    moveSteps1(dir,32*64,ms);

}

void moveAngle1(bool dir, int angle, byte ms){

  moveSteps1(dir,(angle*32*64/360),ms);

}


void moveSteps2(bool dir, int steps, byte ms) {

  for (unsigned long i = 0; i < steps; i++) {

    moveOneStep2(dir); 

    delay(constrain(ms,3,20));       

  }

}


void moveOneStep2(bool dir) {

  static byte out = 0x01;

  if (dir) {  

    out != 0x08 ? out = out << 1 : out = 0x01;

  }

  else {      

    out != 0x01 ? out = out >> 1 : out = 0x08;

  }


  for (int i = 0; i < 4; i++) {

    digitalWrite(outPorts2[i], (out & (0x01 << i)) ? HIGH : LOW);

  }

}


void moveAround2(bool dir, int turns, byte ms){

  for(int i=0;i<turns;i++)

    moveSteps2(dir,32*64,ms);

}


void moveAngle2(bool dir, int angle, byte ms){

  moveSteps2(dir,(angle*32*64/360),ms);

}


WiFiServer server(80);


void setup()

{

  display.init();

  display.setFont(ArialMT_Plain_24);

  display.drawString(0,0,"Be carefull");

  display.setFont(ArialMT_Plain_24);

  display.drawString(0,20,"We are");

  display.setFont(ArialMT_Plain_24);

  display.drawString(0,40,"working");

  display.display();

  

  for(int i=0;i<4;i++) 

  {

    pinMode(outPorts1[i],OUTPUT);

  }


  for(int i=0;i<4;i++) 

  {

    pinMode(outPorts2[i],OUTPUT);

  } 


  pinMode(18,OUTPUT);

  pinMode(19,OUTPUT);

  pinMode(13,OUTPUT);

  pinMode(23,OUTPUT);

  

  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 /?jd")) {

              JibDown();

            }

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

              JibUp();

            }

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

               MotorStop();

            }

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

               LeftRotate();

            }

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

               RightRotate();

            }            

         }

      }

      client.stop();

      Serial.println("Client Disconnected.");

   }

}

2022年8月18日木曜日

ESP8266 WiFi Simplecar


 






//ESP8266_wrover_wifi-simplecar

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

//DRV8833 VCC-3.3V GND-GND OUT1,OUT2-M1 OUT3,OUT4-M2

//DRV8833 IN1-D2(GPIO4) IN2-D1(GPIO5) IN3-D6(GPIO12) IN4-D7(GPIO12)


#include <ESP8266WiFi.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>ESP8266 WiFiCar</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='nu' value='Not use' />\

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

<input type='submit' name='nu' value='Not use' /><br> \

<input type='submit' name='nu' value='Not use' />\

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

<input type='submit' name='nu' value='Not use' /><br><br>\

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


void Forward()

{

  digitalWrite(4,HIGH);

  digitalWrite(5,LOW);

  digitalWrite(12,HIGH);

  digitalWrite(13,LOW);

}


void Back()

{

  digitalWrite(4,LOW);

  digitalWrite(5,HIGH);

  digitalWrite(12,LOW);

  digitalWrite(13,HIGH);

}


void MotorStop()

{

  digitalWrite(4,LOW);

  digitalWrite(5,LOW);

  digitalWrite(12,LOW);

  digitalWrite(13,LOW);

}


void Right()

{

  digitalWrite(4,LOW);

  digitalWrite(5,LOW);

  digitalWrite(12,HIGH);

  digitalWrite(13,LOW);

  delay(300);

}

 

void Left()

{

  digitalWrite(4,HIGH);

  digitalWrite(5,LOW);

  digitalWrite(12,LOW);

  digitalWrite(13,LOW);

  delay(300); 

}


WiFiServer server(80);


void setup()

{

  pinMode(4,OUTPUT);

  pinMode(5,OUTPUT);

  pinMode(12,OUTPUT);

  pinMode(13,OUTPUT);

  

  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 /?nu")) {

              

            }

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

              

            }

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

               MotorStop();

            }

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

               

            }

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

               

            }            

         }

      }

      client.stop();

      Serial.println("Client Disconnected.");

   }

}

2022年8月10日水曜日

ChallengeRaspi_pico minislot




 #pico mini slot

#Pico Minislot

#Press Redkey  to start

#Press Greenkey to show slotleft

#Press Yellokey to show slotcenter

#Press Bluekey to show slotright

#Your first money is $1000


from machine import Pin, I2C

import ssd1306

import random

import utime


i2c = I2C(0, sda=Pin(16), scl=Pin(17) )

addr = i2c.scan()

print( "address is :" + str(addr) )

display = ssd1306.SSD1306_I2C(128, 64, i2c)


swflag1=False

swflag2=False

swflag3=False

swflag4=False


str1=''

str2=''

str3=''

str4=''


intval=0.5

yourmoney=900

randend=6  #9 slot  6 dice  2 tyouhan


bt1=Pin(18,Pin.IN,Pin.PULL_UP)

bt2=Pin(19,Pin.IN,Pin.PULL_UP)

bt3=Pin(20,Pin.IN,Pin.PULL_UP)

bt4=Pin(21,Pin.IN,Pin.PULL_UP)


def slotleft():

    global str1

    str1 = str(random.randint(1,randend))

    display.text(str1,15,10,1)    

    display.show()

    print(str1)


def slotcenter():

    global str2

    str2 = str(random.randint(1,randend))

    display.text(str2,45,10,1)

    display.show()

    print(str2)

    

def slotright():

    global str3

    str3 = str(random.randint(1,randend))

    display.text(str3,75,10,1)  

    display.show()

    print(str3)

    

def slotclear():

    display.fill(0)

    display.show()

       

def bt1_handler(pin):

    global swflag1

    swflag1=True

             

def bt2_handler(pin):

    global swflag2

    swflag2=True

           

def bt3_handler(pin):

    global swflag3

    swflag3=True

        

def bt4_handler(pin):

    global swflag4

    swflag4=True


bt1.irq(trigger = Pin.IRQ_FALLING,handler=bt1_handler) 

bt2.irq(trigger = Pin.IRQ_FALLING,handler=bt2_handler)    

bt3.irq(trigger = Pin.IRQ_FALLING,handler=bt3_handler)

bt4.irq(trigger = Pin.IRQ_FALLING,handler=bt4_handler)


display.text('Pico Minislot',0,0,1)

display.text('Redkey start',0,10,1)

display.text('Greenkey left',0,20,1)

display.text('Yellokey center',0,30,1)

display.text('Bluekey right',0,40,1)

display.show()


while True:   

    if swflag1==True:

        utime.sleep(intval)

        slotleft()

        swflag1=False

        

    if swflag2==True:

        utime.sleep(intval)

        slotcenter()

        swflag2=False


    if swflag3==True:

        utime.sleep(intval)

        slotright()

        swflag3=False

        

    if swflag4==True:

        utime.sleep(intval)

        slotclear()

        swflag4=False

        if str1==str2 and str2==str3:

            yourmoney=yourmoney +100

            display.text('$ ' + str(yourmoney),0,55,1)

            display.show()

        else:       

            yourmoney=yourmoney -100

            if yourmoney <= 0 :

                display.fill(0)

                display.text('Game over',0,0,1)

                display.show()

            else:

                display.text('$ ' +str(yourmoney),0,55,1)

                display.show()

            


2022年8月5日金曜日

EPS32 wrover WiFi BucketTruck






 














//EPS32_wrover_wifi-buckettruck
//Thanks to mukujii.sakura.ne.jp/esp1.html
//Thanks to www.freenove.com
//MX1508 MOTORA-M1 MX1508 MOTORB-M2
//RL1,RL2 - M3

#include <ESP32MX1508.h>
#define M1A 18  //MX1508 INT1
#define M1B 19 //MX1508 INT2
#define M2A 22 //MX1508 INT3
#define M2B 23 //MX1508 INT4
#define BU 4  //Bucket up
#define BD 5  //Bucket down
#define CH1 0  
#define CH2 1 
#define CH3 2 
#define CH4 3 
#define CH5 4
#define CH6 5

MX1508 motorA(M1A,M1B, CH1, CH2); 
MX1508 motorB(M2A,M2B, CH3, CH4);
MX1508 motorC(BU,BD,CH5,CH6); 

#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 WiFiCar</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='bd' value='BucketDown' />\
<input type='submit' name='st' value='Stop' />\
<input type='submit' name='bu' value='BucketUp' /><br> \
<input type='submit' name='lb' value='LeftBack' />\
<input type='submit' name='ba' value='Back' />\
<input type='submit' name='rb' value='RightBack' /><br><br>\
</form></div></body></html>";

void Forward()
{
  motorA.motorGo(200);
  motorB.motorGo(230);
  delay(100); 
}

void Back()
{
  motorA.motorRev(230);
  motorB.motorRev(200);
  delay(100);
}

void MotorStop()
{
  motorA.motorStop();
  motorB.motorStop();
  motorC.motorStop();
}

void Right()
{
  motorB.motorGo(200);
  motorA.motorRev(200);
  delay(50); 
  motorA.motorGo(200);
  motorB.motorGo(230);
  delay(100);  
}

void Left()
{
  motorA.motorGo(200);
  motorB.motorRev(200);
  delay(50); 
  motorA.motorGo(200);
  motorB.motorGo(230);
  delay(100);  
}

void RightBack()
{
  motorB.motorRev(200);
  motorA.motorGo(200);
  delay(50); 
  motorA.motorRev(230);
  motorB.motorRev(200);
  delay(100);     
}

void LeftBack()
{
  motorA.motorRev(200);
  motorB.motorGo(200);
  delay(50); 
  motorA.motorRev(230);
  motorB.motorRev(200);
  delay(100); 
}

void BucketUp()
{
  motorC.motorGo(200);
  delay(1000);          
}

void BucketDown()
{
  motorC.motorRev(200);
  delay(1000);                          
}

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();
}

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 /?lb")) {
              LeftBack();
            }
            if (currentLine.endsWith("GET /?rb")) {
              RightBack();
            }
            if (currentLine.endsWith("GET /?st")) {
               MotorStop();
            }
            if (currentLine.endsWith("GET /?bu")) {
               BucketUp();
            }
            if (currentLine.endsWith("GET /?bd")) {
               BucketDown();
            }            
         }
      }
      client.stop();
      Serial.println("Client Disconnected.");
   }
}