Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <U8g2lib.h>
- #include <Wire.h>
- #define joyStickY A1
- #define joyStickX A0
- #define motorDriver1 10//PWMピン
- #define motorDriver2 9//PWMピン
- #define sensorPin 7
- //ポーリング用
- unsigned long lastupdate = 0;
- unsigned long lastupdata = 0;
- //カウンタ用
- volatile int count = 0;
- int dir = 0;
- U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
- void setup() {
- Serial.begin(115200);//デバッグ用
- u8g2.begin();
- u8g2.clearBuffer(); // clear the internal memory
- u8g2.setFont(u8g2_font_logisoso20_tr); // choose a suitable font
- u8g2.drawStr(0,10,"Hello World!"); // write something to the internal memory
- u8g2.sendBuffer(); // transfer internal memory to the display
- //割り込み開始
- attachInterrupt(digitalPinToInterrupt(sensorPin), counter, RISING);
- //pinMode(motorDriver1, OUTPUT);
- //pinMode(motorDriver2, OUTPUT);
- //TCCR1A = 0b00000001;
- //TCCR1B = 0b00000001;
- }
- void loop() {
- //前回実行時の時間を今の時間と比較
- if (lastupdate + 200 <= millis()) {
- lastupdate = millis();//次の実行のために時間更新
- int sensor = analogRead(joyStickY);
- int out = map(sensor, 0, 1023, -255, 255);
- motor(out);//モーター用関数を読み出す
- //Serial.println(out);
- }
- /*if (lastupdata + 200 <= millis()) {
- lastupdata = millis();//次の実行のために時間更新
- //lcd();
- }*/
- }
- //モーター用関数
- void motor(int output) {
- if (output > 0) {
- analogWrite(motorDriver1, output);
- analogWrite(motorDriver2, 0);
- dir = 1;
- } else if (output <= 0) {
- analogWrite(motorDriver1, 0);
- analogWrite(motorDriver2, output * -1);
- dir = -1;
- }
- }
- void counter() {
- count = count + 1 * dir;
- }
- void lcd() {
- u8g2.clearBuffer(); // clear the internal memory
- u8g2.setFont(u8g2_font_logisoso20_tr); // choose a suitable font
- u8g2.setCursor(0,20);
- u8g2.print(count);
- u8g2.setCursor(0,40);
- u8g2.print(dir);
- u8g2.sendBuffer(); // transfer internal memory to the displa
- }
Add Comment
Please, Sign In to add comment