#define BLYNK_PRINT Serial | |
#include <WiFi.h> | |
#include <WiFiClient.h> | |
#include <BlynkSimpleEsp32.h> | |
//#include <M5StickC.h> | |
#include <Wire.h> | |
// You should get Auth Token in the Blynk App. | |
char auth[] = "BlynkアプリのYourAuthTokenを入力"; | |
// Your WiFi credentials. | |
char ssid[] = "WiFiのSSID"; | |
char pass[] = "パスワード";; | |
const int motorL = 0x60; | |
const int motorR = 0x64; | |
long Speed; | |
long SpeedL, SpeedR; | |
//モータドライバ I2C制御 motor driver I2C | |
void writeMotorResister(int motor, byte vset, byte data1){ | |
int vdata = vset << 2 | data1; | |
Wire.beginTransmission(motor); | |
Wire.write(0x00); | |
Wire.write(vdata); | |
Wire.endTransmission(true); | |
} | |
//ジョイスティックのデータ受信 | |
BLYNK_WRITE(V0) { | |
long x = param[0].asInt(); | |
long y = param[1].asInt(); | |
Serial.print("x: "); | |
Serial.print(x); | |
Serial.print(" y: "); | |
Serial.print(y); | |
Speed = sqrt(x*x+y*y); | |
if(Speed > 20){ | |
Speed = 20; | |
} | |
Serial.print(" Speed: "); | |
Serial.println(Speed); | |
//モータ制御 | |
if(y >= 0){ | |
if(x >= 0){ | |
SpeedL = 0; | |
SpeedR = abs(x); | |
}else{ | |
SpeedL = abs(x); | |
SpeedR = 0; | |
} | |
writeMotorResister(motorL, byte(Speed - SpeedL), 0x01); | |
writeMotorResister(motorR, byte(Speed - SpeedR), 0x01); | |
}else{ | |
if(x >= 0){ | |
SpeedL = 0; | |
SpeedR = abs(x); | |
}else{ | |
SpeedL = abs(x); | |
SpeedR = 0; | |
} | |
writeMotorResister(motorL, byte(Speed - SpeedL), 0x02); | |
writeMotorResister(motorR, byte(Speed - SpeedR), 0x02); | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
Blynk.begin(auth, ssid, pass); | |
Wire.begin(0, 26, 10000); //SDA, SCL | |
delay(500); | |
writeMotorResister(motorL, 0x00, 0x00); | |
writeMotorResister(motorR, 0x00, 0x00); | |
} | |
void loop() | |
{ | |
Blynk.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment