【模板】机器人六级模板

机器人六级模板

包含:

  • 1位4位数码管
  • 8 × 8 8\times8 8×8点阵
  • 按键(已消抖)
  • 电位器
  • 发光二极管
  • 交通灯
  • 单个直流电机(无码盘)

模板:

#include <AccelStepper.h>
#include <MPU6050_tockn.h>
#include <Wire.h>
#include <ssd1306.h>
#include <ssd1306_console.h>
typedef int ll;
class Motor {
  private:
    typedef int ll;
    ll ch1, ch2, is_rev = 1, _speed = 0;
    ll changeSpeed(ll speed) const {
      return speed == 0 ? 0 : map(speed, 1, 100, 750, 1000);
    }
  public:
    Motor(ll pin1, ll pin2, ll _ch1, ll _ch2, ll fre = 5000, ll bits = 10) {
      ch1 = _ch1, ch2 = _ch2;
      ledcSetup(ch1, fre, bits);
      ledcSetup(ch2, fre, bits);
      ledcAttachPin(pin1, ch1);
      ledcAttachPin(pin2, ch2);
      ledcWrite(ch1, 0);
      ledcWrite(ch2, 0);
    }
    static double getRoll(ll sum) {
      return sum / 146.0;
    }
    static ll getAngle(ll sum) {
      return sum / 146.0 * 360;
    }
    void reverse() {
      is_rev *= -1;
    }
    void run(ll speed) {
      speed *= is_rev;
      _speed = speed;
      if (speed > 0) {
        ledcWrite(ch1, 0);
        ledcWrite(ch2, changeSpeed(speed));
      } else {
        ledcWrite(ch1, changeSpeed(-speed));
        ledcWrite(ch2, 0);
      }
    }
    void runAnticlockwise(ll speed) {
      _speed = -speed * is_rev;
      if (is_rev == -1) {
        ledcWrite(ch1, 0);
        ledcWrite(ch2, changeSpeed(speed));
      } else {
        ledcWrite(ch1, changeSpeed(speed));
        ledcWrite(ch2, 0);
      }
    }
    void runClockwise(ll speed) {
      _speed = speed * is_rev;
      if (is_rev == -1) {
        ledcWrite(ch1, changeSpeed(speed));
        ledcWrite(ch2, 0);
      } else {
        ledcWrite(ch1, 0);
        ledcWrite(ch2, changeSpeed(speed));
      }
    }
    void stop(bool isfloat = false) {
      _speed = 0;
      if (!isfloat) {
        ledcWrite(ch1, 1000);
        ledcWrite(ch2, 1000);
      } else {
        ledcWrite(ch1, 0);
        ledcWrite(ch2, 0);
      }
    }
    ll getSpeed() const {
      return _speed;
    }
    ~Motor() {
      ledcWrite(ch1, 1000);
      ledcWrite(ch2, 1000);
    }
};
class DisplayDigit4 {
  private:
    typedef int ll;
    ll dpin;
    ll cpin;
    ll lpin;
    byte num[12] = {0xfc, 0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0, 0xfe, 0xf6, 0x01, 0x00};
  public:
    void Display(ll w, ll x, ll y, ll z) {
      ll d[] = {w, x, y, z};
      for (ll digit = 0; digit < 4; ++digit) {
        byte val = 0;
        bitSet(val, digit);
        if (d[digit] == -1) {
          val = 0;
          d[digit] = 0;
        }
        shiftOut(dpin, cpin, MSBFIRST, val);
        shiftOut(dpin, cpin, LSBFIRST, ~d[digit]);
        digitalWrite(lpin, HIGH);
        digitalWrite(lpin, LOW);
        delay(2);
      }
    }
    void clear() {
      Display(-1, -1, -1, -1);
    }
    DisplayDigit4(ll Dpin, ll Cpin, ll Lpin) {
      dpin = Dpin;
      cpin = Cpin;
      lpin = Lpin;
      pinMode(dpin, OUTPUT);
      pinMode(cpin, OUTPUT);
      pinMode(lpin, OUTPUT);
      clear();
    }
    void DisplayNum(ll n, bool has0 = false) {
      ll top = 0, s[10] = {};
      if (n == -1) {
        for (ll i = 0; i < 4; ++i) s[top++] = -1;
      } else if (n == 0) {
        s[top++] = 0;
        s[top++] = -1;
        s[top++] = -1;
        s[top++] = -1;
      } else if (has0) {
        for (ll i = 1; i <= 4; ++i) {
          s[top++] = n % 10;
          n /= 10;
        }
      } else {
        for (ll i = 1; i <= 4; ++i) {
          s[top++] = n % 10;
          n /= 10;
        }
        for (ll i = 3; i >= 1; --i) {
          if (s[top - 1] != 0) break;
          s[--top] = -1;
        }
        top = 4;
      }
      for (ll digit = 0; digit < 4 && top; ++digit) {
        byte val = 0;
        bitSet(val, digit);
        if (s[top - 1] == -1) {
          val = 0;
          s[top - 1] = 11;
        }
        shiftOut(dpin, cpin, MSBFIRST, val);
        shiftOut(dpin, cpin, LSBFIRST, ~num[s[--top]]);
        digitalWrite(lpin, HIGH);
        digitalWrite(lpin, LOW);
        delay(2);
      }
    }
};
class DisplayDigit1 {
  private:
    typedef int ll;
    ll dpin;
    ll cpin;
    ll lpin;
    byte num[12] = {0xfc, 0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0, 0xfe, 0xf6, 0x01, 0x00};
  public:
    void Display(ll x) {
      if (x == -1) {
        shiftOut(dpin, cpin, LSBFIRST, 0xff);
      } else {
        shiftOut(dpin, cpin, LSBFIRST, ~x);
      }
      digitalWrite(lpin, HIGH);
      digitalWrite(lpin, LOW);
    }
    void clear() {
      Display(-1);
    }
    DisplayDigit1(ll Dpin, ll Cpin, ll Lpin) {
      dpin = Dpin;
      cpin = Cpin;
      lpin = Lpin;
      pinMode(dpin, OUTPUT);
      pinMode(cpin, OUTPUT);
      pinMode(lpin, OUTPUT);
      clear();
    }
    void DisplayNum(ll n) {
      if (n == -1) {
        shiftOut(dpin, cpin, LSBFIRST, 0xff);
      } else {
        shiftOut(dpin, cpin, LSBFIRST, ~num[n]);
      }
      digitalWrite(lpin, HIGH);
      digitalWrite(lpin, LOW);
    }
};
class DotMatrix {
  private:
    typedef int ll;
    typedef bool(*func_bool_void)(void);
    ll dpin;
    ll cpin;
    ll lpin;
  public:
    void Display(ll pic[]) {
      byte dataRow;
      byte dataCol;
      for (ll i = 0; i < 8; ++i) {
        dataRow = 1 << i;
        dataCol = ~pic[i];
        shiftOut(dpin, cpin, LSBFIRST, dataCol);
        shiftOut(dpin, cpin, MSBFIRST, dataRow);
        digitalWrite(lpin, HIGH);
        digitalWrite(lpin, LOW);
      }
    }
    void clear() {
      ll n[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
      Display(n);
    }
    DotMatrix(ll Dpin, ll Cpin, ll Lpin) {
      dpin = Dpin;
      cpin = Cpin;
      lpin = Lpin;
      pinMode(dpin, OUTPUT);
      pinMode(cpin, OUTPUT);
      pinMode(lpin, OUTPUT);
    }
    void DotScanDisplay(func_bool_void wait) {
      byte dataRow;
      byte dataCol;
      for (ll i = 0; i < 8; ++i) {
        dataRow = 1 << i;
        for (ll j = 0; j < 8; ++j) {
          dataCol = 1 << j;
          shiftOut(dpin, cpin, LSBFIRST, ~dataCol);
          shiftOut(dpin, cpin, MSBFIRST, dataRow);
          digitalWrite(lpin, HIGH);
          digitalWrite(lpin, LOW);
          if (!wait()) {
            return;
          }
        }
      }
    }
    void RowScanDisplay(func_bool_void wait) {
      byte dataRow;
      byte dataCol;
      for (ll i = 0; i < 8; ++i) {
        dataRow = 1 << i;
        dataCol = 0xff;
        shiftOut(dpin, cpin, LSBFIRST, ~dataCol);
        shiftOut(dpin, cpin, MSBFIRST, dataRow);
        digitalWrite(lpin, HIGH);
        digitalWrite(lpin, LOW);
        if (!wait()) {
          return;
        }
      }
    }
    void ColScanDisplay(func_bool_void wait) {
      byte dataRow;
      byte dataCol;
      for (ll i = 0; i < 8; ++i) {
        dataCol = 1 << i;
        dataRow = 0xff;
        shiftOut(dpin, cpin, LSBFIRST, ~dataCol);
        shiftOut(dpin, cpin, MSBFIRST, dataRow);
        digitalWrite(lpin, HIGH);
        digitalWrite(lpin, LOW);
        if (!wait()) {
          return;
        }
      }
    }
};
class Button {
  private:
    typedef int ll;
    ll pin;
    ll LBtn = 0;
    ll Btn = 0;
    unsigned long long BtnTime = 0;
    const ll BtnDelayTime = 10;
    typedef void (*func_void_bool)(bool);
  public:
    Button(ll p) {
      pin = p;
      pinMode(pin, INPUT);
    }
    void GetState(func_void_bool calc) {
      ll val = !digitalRead(pin);
      if (val != LBtn) {
        BtnTime = millis();
        LBtn = val;
      }
      if (millis() - BtnTime >= BtnDelayTime) {
        if (val != Btn) {
          Btn = val;
          calc(val == LOW);
        }
      }
      LBtn = val;
    }
    ll GetState() {
      return !digitalRead(pin);
    }
};
class pot {
  private:
    typedef int ll;
    ll pin;
  public:
    pot(ll pin): pin(pin) {};
    ll GetAngle() {
      return analogRead(pin);
    }
};
class led {
  private:
    typedef int ll;
    ll pin;
  public:
    led(ll Pin) {
      pin = Pin;
      pinMode(pin, OUTPUT);
    }
    void TurnOn() {
      digitalWrite(pin, HIGH);
    }
    void TurnOff() {
      digitalWrite(pin, LOW);
    }
    void TurnTo(ll state) {
      digitalWrite(pin, state);
    }
};
class traffic {
  private:
    typedef int ll;
    ll redpin;
    ll yellowpin;
    ll greenpin;
  public:
    traffic(ll r, ll y, ll g) {
      redpin = r;
      yellowpin = y;
      greenpin = g;
      pinMode(redpin, OUTPUT);
      pinMode(yellowpin, OUTPUT);
      pinMode(greenpin, OUTPUT);
    }
    void Turn(ll rstate, ll ystate, ll gstate) {
      digitalWrite(redpin, rstate);
      digitalWrite(yellowpin, ystate);
      digitalWrite(greenpin, gstate);
    }
};
namespace pin {
}
using namespace pin;
ssd1306Console con;
AccelStepper stepper(4, 15, 13, 14, 12); // 黄12, 白13, 橙14, 绿15
MPU6050 mpu(Wire);
void setup() {
  stepper.setMaxSpeed(700);
  stepper.setAcceleration(300);
  stepper.setCurrentPosition(0);
  mpu.begin();
  mpu.calcGyroOffsets(true);
  ssd1306_128x64_i2c_init();
  ssd1306_clearScreen();
  ssd1306_setFixedFont(ssd1306xled_font6x8);
  Serial.begin(115200);
}
void loop() {

}

看到这里,就给一个赞吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值