LoginSignup
0
0

More than 5 years have passed since last update.

wemos d1で壁打ち

Posted at

概要

wemos d1で壁打ち、やってみた。

写真

MVC-007F.JPG

回路図

we.JPG

サンプルコード

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(16);
int x = 64;
int y = 0;
int z = (128 - 20) / 2;
int dx = 3;
int dy = -2;
int p = 0;

void setup()
{
    Serial.begin(115200);
    while (!Serial) delay(250);
    Serial.println("\nok");
    oled.begin(SSD1306_SWITCHCAPVCC, 0x3c);
    oled.clearDisplay();
    oled.setTextColor(WHITE);
    oled.setTextSize(2);
    oled.setCursor(0, 0);
    oled.print("oled 1");
    oled.display();
}
void loop()
{
    int p = 0;
    int val = analogRead(A0);
    int range = map(val, 0, 1024, 0, 6);
    switch (range)
    {
    case 1:
        Serial.println("1");
        p = -1;
    break;
    case 2:
        Serial.println("2");
        p = 1;
    break;
    case 3:
        Serial.println("3");
    break;
    case 4:
        Serial.println("4");
    break;
    case 5:
        Serial.println("5");
    break;
    case 6:
        Serial.println("6");
    break;
    }
    if (x + dx > 125 || x + dx < 2)
    {
        dx = -dx;
    }
    if (y + dy < 2)
    {
        dy = -dy;
    }
    else if (y + dy > 48)
    {
        if (x > z && x < z + 20)
        {
            dy = -dy;
        }
        else
        {
            x = 64;
            y = 2;
            z = 54;
            p = 0;
        }
    }
    if (p == 1)
    {
        z += 5;
    }
    else if (p == -1)
    {
        z -= 5;
    }
    x += dx;
    y += dy;
    oled.clearDisplay();
    oled.drawRect(0, 0, 127, 63, WHITE);
    oled.fillRect(z, 50, 20, 10, WHITE);
    oled.fillCircle(x, y, 4, WHITE);
    oled.display();
    p = 0;
    delay(100);
}



以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0