firtel

Untitled

Feb 16th, 2022 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <U8g2lib.h>
  3. #include <Wire.h>
  4.  
  5. #define joyStickY A1
  6. #define joyStickX A0
  7. #define motorDriver1 10//PWMピン
  8. #define motorDriver2 9//PWMピン
  9. #define sensorPin 7
  10.  
  11. //ポーリング用
  12. unsigned long lastupdate = 0;
  13. unsigned long lastupdata = 0;
  14.  
  15. //カウンタ用
  16. volatile int count = 0;
  17. int dir = 0;
  18.  
  19. U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
  20.  
  21.  
  22. void setup() {
  23.   Serial.begin(115200);//デバッグ用
  24.   u8g2.begin();
  25.   u8g2.clearBuffer();                   // clear the internal memory
  26.   u8g2.setFont(u8g2_font_logisoso20_tr);    // choose a suitable font
  27.   u8g2.drawStr(0,10,"Hello World!");    // write something to the internal memory
  28.   u8g2.sendBuffer();                    // transfer internal memory to the display
  29.   //割り込み開始
  30.   attachInterrupt(digitalPinToInterrupt(sensorPin), counter, RISING);
  31.   //pinMode(motorDriver1, OUTPUT);
  32.   //pinMode(motorDriver2, OUTPUT);
  33.   //TCCR1A = 0b00000001;
  34.   //TCCR1B = 0b00000001;
  35.  
  36. }
  37.  
  38. void loop() {
  39.   //前回実行時の時間を今の時間と比較
  40.   if (lastupdate + 200 <= millis()) {
  41.     lastupdate = millis();//次の実行のために時間更新
  42.     int sensor = analogRead(joyStickY);
  43.     int out = map(sensor, 0, 1023, -255, 255);
  44.     motor(out);//モーター用関数を読み出す
  45.     //Serial.println(out);
  46.   }
  47.   /*if (lastupdata + 200 <= millis()) {
  48.     lastupdata = millis();//次の実行のために時間更新
  49.     //lcd();
  50.   }*/
  51. }
  52.  
  53. //モーター用関数
  54. void motor(int output) {
  55.   if (output > 0) {
  56.     analogWrite(motorDriver1, output);
  57.     analogWrite(motorDriver2, 0);
  58.     dir = 1;
  59.   } else if (output <= 0) {
  60.     analogWrite(motorDriver1, 0);
  61.     analogWrite(motorDriver2, output * -1);
  62.     dir = -1;
  63.   }
  64. }
  65.  
  66. void counter() {
  67.   count = count + 1 * dir;
  68. }
  69.  
  70. void lcd() {
  71.   u8g2.clearBuffer();          // clear the internal memory
  72.   u8g2.setFont(u8g2_font_logisoso20_tr); // choose a suitable font
  73.   u8g2.setCursor(0,20);
  74.   u8g2.print(count);
  75.   u8g2.setCursor(0,40);
  76.   u8g2.print(dir);
  77.   u8g2.sendBuffer();          // transfer internal memory to the displa
  78. }
  79.  
Add Comment
Please, Sign In to add comment