読者です 読者をやめる 読者になる 読者になる

レッドインベーダーの部屋

なんか色々なことをしています。

Arduino 加速度センサ モーターカー その2

前回からの続きです。

接続してからの角度を求めるところまでは↓参照

mrinvader007.hatenablog.jp

角度を求めるスケッチと角度を求めるのに必要な数値の計測のスケッチが掲載されています。

 

前回書いたようにいろんな実験段階としてスケッチを何種類か書きました。

どんな感じに動くかは一応動画を載せますが、

配線はその1をみて接続してください。

 

実験的なスケッチなので、

名前などはとくにありません。

 

rotateX,rotateYの部分は個体差があるので、直してください。(その1参照)

int rotateX = (x - 288) / 2.48 - 90; //角度を求める式
int rotateY = (y - 275) / 2.48 - 90;

↑この部分です。

パターン1(sensamotorcar)(青文字部分)

 

//A,Bは右タイヤ
//C,Dは左タイヤ
//A,Cは前進
//B,Dは後進
const int motorA = 1;
const int motorB = 2;
const int PWM_mot = 3;
const int motorC = 13;
const int motorD = 12;
const int PWM_mot2 = 11;

const int led2 = 5;
const int led3 = 6;
const int led6 = 9;
const int led7 = 10;

void setup() {
// シリアルモニターの初期化をする
Serial.begin(9600) ;
pinMode (motorA, OUTPUT);
pinMode (motorB, OUTPUT);
pinMode (motorC, OUTPUT);
pinMode (motorD, OUTPUT);
}

void loop() {

int i ;
long x, y, z;
//50回センサ値を読み込んで平均を算出
x = y = z = 0;
for (i = 0 ; i < 50 ; i++) {
x = x + analogRead(3) ; // X軸
y = y + analogRead(4) ; // Y軸
z = z + analogRead(5) ; // Z軸
}
x = x / 50 ;
y = y / 50 ;
z = z / 50 ;
int rotateX = (x - 288) / 2.48 - 90; //角度を求める式
int rotateY = (y - 275) / 2.48 - 90;
Serial.print("X:") ;
Serial.print(x) ;
Serial.print(" ") ;
Serial.print(rotateX) ;
Serial.print(" Y:") ;
Serial.print(y) ;
Serial.print(" ") ;
Serial.println(rotateY) ;

 

if ( rotateX < 9) {
for (int red = 0; red > -1; red -= 10) {
analogWrite(led2, red);
digitalWrite(motorA, LOW);
digitalWrite(motorB, LOW);
digitalWrite(motorC, LOW);
digitalWrite(motorD, LOW);
analogWrite(PWM_mot, 0);
analogWrite(PWM_mot2, 0);
}
}


if (rotateX >= 10 ) {
for (int red = 1; red < 11; red += 10) {

analogWrite(led2, red);
}
}


if (rotateX >= 20 ) {
for (int red = 10; red < 26; red += 10) {

analogWrite(led2, red);

}
}
if (rotateX >= 30) {
for (int red = 25; red < 51; red += 10) {
analogWrite(led2, red);

digitalWrite(motorA, HIGH);
digitalWrite(motorB, LOW);
analogWrite(PWM_mot, 255);
digitalWrite(motorC, LOW);
digitalWrite(motorD, LOW);
analogWrite(PWM_mot2, 0);
}
}
if (rotateX >= 40) {
for (int red = 50; red < 81; red += 10) {
analogWrite(led2, red);
}
}
if (rotateX >= 50) {
for (int red = 80; red < 111; red += 10) {
analogWrite(led2, red);
}
}
if (rotateX >= 60) {
for (int red = 110; red < 141; red += 10) {
analogWrite(led2, red);
}
}
if (rotateX >= 70) {
for (int red = 140; red < 171; red += 10) {
analogWrite(led2, red);
}
}
if (rotateX >= 80) {
for (int red = 170; red < 201; red += 10) {
analogWrite(led2, red);
}
}
if (rotateX >= 90) {
for (int red = 200; red < 256; red += 10) {
analogWrite(led2, red);
}
}

 


if ( rotateX > -9) {
for (int red = 0; red > -1; red -= 10) {
analogWrite(led3, red);


}
}

if (rotateX <= -10 ) {
for (int red = 1; red < 11; red += 10) {

analogWrite(led3, red);
}
}


if (rotateX <= -20 ) {
for (int red = 10; red < 26; red += 10) {

analogWrite(led3, red);

}
}
if (rotateX <= -30) {
for (int red = 25; red < 51; red += 10) {
analogWrite(led3, red);


digitalWrite(motorA, LOW);
digitalWrite(motorB, LOW);
digitalWrite(motorC, LOW);
digitalWrite(motorD, LOW);
analogWrite(PWM_mot, 0);
analogWrite(PWM_mot2, 0);
}
}
if (rotateX <= -40) {
for (int red = 50; red < 81; red += 10) {
analogWrite(led3, red);
}
}
if (rotateX <= -50) {
for (int red = 80; red < 111; red += 10) {
analogWrite(led3, red);
}
}
if (rotateX <= -60) {
for (int red = 110; red < 141; red += 10) {
analogWrite(led3, red);
}
}
if (rotateX <= -70) {
for (int red = 140; red < 171; red += 10) {
analogWrite(led3, red);
}
}
if (rotateX <= -80) {
for (int red = 170; red < 201; red += 10) {
analogWrite(led3, red);
}
}
if (rotateX <= -90) {
for (int red = 200; red < 256; red += 10) {
analogWrite(led3, red);
}
}

 

if ( rotateY < 9) {
for (int red = 0; red > -1; red -= 10) {
analogWrite(led6, red);
}
}


if (rotateY >= 10 ) {
for (int red = 1; red < 11; red += 10) {

analogWrite(led6, red);
}
}


if (rotateY >= 20 ) {
for (int red = 10; red < 26; red += 10) {

analogWrite(led6, red);

}
}
if (rotateY >= 30) {
for (int red = 25; red < 51; red += 10) {
analogWrite(led6, red);
}
}
if (rotateY >= 40) {
for (int red = 50; red < 81; red += 10) {
analogWrite(led6, red);
}
}
if (rotateY >= 50) {
for (int red = 80; red < 111; red += 10) {
analogWrite(led6, red);
}
}
if (rotateY >= 60) {
for (int red = 110; red < 141; red += 10) {
analogWrite(led6, red);
}
}
if (rotateY >= 70) {
for (int red = 140; red < 171; red += 10) {
analogWrite(led6, red);
}
}
if (rotateY >= 80) {
for (int red = 170; red < 201; red += 10) {
analogWrite(led6, red);
}
}
if (rotateY >= 90) {
for (int red = 200; red < 256; red += 10) {
analogWrite(led6, red);
}
}

 

if ( rotateY > -9) {
for (int red = 0; red > -1; red -= 10) {
analogWrite(led7, red);
}
}

if (rotateY <= -10 ) {
for (int red = 1; red < 11; red += 10) {

analogWrite(led7, red);
}
}


if (rotateY <= -20 ) {
for (int red = 10; red < 26; red += 10) {

analogWrite(led7, red);

}
}
if (rotateY <= -30) {
for (int red = 25; red < 51; red += 10) {
analogWrite(led7, red);
}
}
if (rotateY <= -40) {
for (int red = 50; red < 81; red += 10) {
analogWrite(led7, red);
}
}
if (rotateY <= -50) {
for (int red = 80; red < 111; red += 10) {
analogWrite(led7, red);
}
}
if (rotateY <= -60) {
for (int red = 110; red < 141; red += 10) {
analogWrite(led7, red);
}
}
if (rotateY <= -70) {
for (int red = 140; red < 171; red += 10) {
analogWrite(led7, red);
}
}
if (rotateY <= -80) {
for (int red = 170; red < 201; red += 10) {
analogWrite(led7, red);
}
}
if (rotateY <= -90) {
for (int red = 200; red < 256; red += 10) {
analogWrite(led7, red);
}
}

}

前提としてあまりにも初期段階のため走りません。

動画

 

youtu.be

パターン2(sensamotorcar2)(青文字部分)

//加速度センサを使用したモーターカー
//線が短いため走行不可
//ベータ版

//A,Bは右タイヤ
//C,Dは左タイヤ
//A,Cは前進
//B,Dは後進
const int motorA = 1;
const int motorB = 2;
const int PWM_mot = 3;
const int motorC = 13;
const int motorD = 12;
const int PWM_mot2 = 11;

const int led2 = 5;
const int led3 = 6;
const int led6 = 9;
const int led7 = 10;

 

void setup() {
// シリアルモニターの初期化をする
Serial.begin(9600) ;
pinMode (motorA, OUTPUT);
pinMode (motorB, OUTPUT);
pinMode (motorC, OUTPUT);
pinMode (motorD, OUTPUT);


}
void loop() {

int i ;
long x, y, z;
//50回センサ値を読み込んで平均を算出
x = y = z = 0;
for (i = 0 ; i < 50 ; i++) {
x = x + analogRead(3) ; // X軸
y = y + analogRead(4) ; // Y軸
z = z + analogRead(5) ; // Z軸
}
x = x / 50 ;
y = y / 50 ;
z = z / 50 ;
int rotateX = (x - 288) / 2.48 - 90; //角度を求める式
int rotateY = (y - 275) / 2.48 - 90;
Serial.print("X:") ;
Serial.print(x) ;
Serial.print(" ") ;
Serial.print(rotateX) ;
Serial.print(" Y:") ;
Serial.print(y) ;
Serial.print(" ") ;
Serial.print(rotateY) ;
Serial.print(" Z:") ;
Serial.println(z) ;


{
{ switch (rotateX) {
case 0:
{ for (int red = 0; red >= 0; red += -1)

analogWrite (led2, red);
}
{ for (int red = 0; red >= 0; red += -1)
analogWrite (led3, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 0);
break;
case 10:
{ for (int red = 1; red < 11; red += 10)
analogWrite (led2, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 50);
analogWrite ( PWM_mot2, 0);
break;
case 20:
{ for (int red = 10; red < 31; red += 10)
analogWrite (led2, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 70);
analogWrite ( PWM_mot2, 0);
break;
case 30:
{ for (int red = 30; red < 51; red += 10)
analogWrite (led2, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 90);
analogWrite ( PWM_mot2, 0);
break;
case 40:
{ for (int red = 50; red < 71; red += 10)
analogWrite (led2, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 110);
analogWrite ( PWM_mot2, 0);
break;
case 50:
{ for (int red = 70; red < 101; red += 10)
analogWrite (led2, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 150);
analogWrite ( PWM_mot2, 0);
break;
case 60:
{ for (int red = 100; red < 131; red += 10)
analogWrite (led2, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 170);
analogWrite ( PWM_mot2, 0);
break;
case 70:
{ for (int red = 130; red < 161; red += 10)
analogWrite (led2, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 190);
analogWrite ( PWM_mot2, 0);
break;
case 80:
{ for (int red = 160; red < 201; red += 10)
analogWrite (led2, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 220);
analogWrite ( PWM_mot2, 0);
break;
case 90:
{ for (int red = 200; red < 256; red += 10)
analogWrite (led2, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 255);
analogWrite ( PWM_mot2, 0);
break;
case -10:
{ for (int red = -1; red < 11; red += 10)
analogWrite (led3, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 50);
break;
case -20:
{ for (int red = 10; red < 31; red += 10)
analogWrite (led3, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 70);
break;
case -30:
{ for (int red = 30; red < 51; red += 10)
analogWrite (led3, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 90);
break;
case -40:
{ for (int red = 50; red < 71; red += 10)
analogWrite (led3, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 110);
break;
case -50:
{ for (int red = 70; red < 101; red += 10)
analogWrite (led3, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 150);
break;
case -60:
{ for (int red = 100; red < 131; red += 10)
analogWrite (led3, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 170);
break;
case -70:
{ for (int red = 130; red < 161; red += 10)
analogWrite (led3, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 190);
break;
case -80:
{ for (int red = 160; red < 201; red += 10)
analogWrite (led3, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 220);
break;
case -90:
{ for (int red = 200; red < 256; red += 10)
analogWrite (led3, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 255);
break;
}
}
{ switch (rotateY) {
case 0:
{ for (int red = 0; red >= 0; red += -1)

analogWrite (led6, red);
}
{ for (int red = 0; red >= 0; red += -1)
analogWrite (led7, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 0);
break;
case 10:
{ for (int red = 1; red < 11; red += 10)

analogWrite (led6, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 50);
analogWrite ( PWM_mot2, 50);
break;
case 20:
{ for (int red = 10; red < 31; red += 10)

analogWrite (led6, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 70);
analogWrite ( PWM_mot2, 70);
break;
case 30:
{ for (int red = 30; red < 51; red += 10)

analogWrite (led6, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 90);
analogWrite ( PWM_mot2, 90);
break;
case 40:
{ for (int red = 50; red < 71; red += 10)

analogWrite (led6, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 110);
analogWrite ( PWM_mot2, 110);
break;
case 50:
{ for (int red = 70; red < 101; red += 10)

analogWrite (led6, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 150);
analogWrite ( PWM_mot2, 150);
break;
case 60:
{ for (int red = 100; red < 131; red += 10)

analogWrite (led6, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 170);
analogWrite ( PWM_mot2, 170);
break;
case 70:
{ for (int red = 130; red < 161; red += 10)

analogWrite (led6, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 190);
analogWrite ( PWM_mot2, 190);
break;
case 80:
{ for (int red = 160; red < 201; red += 10)

analogWrite (led6, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 220);
analogWrite ( PWM_mot2, 220);
break;
case 90:
{ for (int red = 200; red < 256; red += 10)

analogWrite (led6, red);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 255);
analogWrite ( PWM_mot2, 255);
break;
case -10:
{ for (int red = 1; red < 11; red += 10)

analogWrite (led7, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, HIGH);
digitalWrite (motorC, LOW);
digitalWrite (motorD, HIGH);
analogWrite ( PWM_mot, 50);
analogWrite ( PWM_mot2, 50);
break;
case -20:
{ for (int red = 10; red < 31; red += 10)

analogWrite (led7, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, HIGH);
digitalWrite (motorC, LOW);
digitalWrite (motorD, HIGH);
analogWrite ( PWM_mot, 70);
analogWrite ( PWM_mot2, 70);
break;
case -30:
{ for (int red = 30; red < 51; red += 10)

analogWrite (led7, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, HIGH);
digitalWrite (motorC, LOW);
digitalWrite (motorD, HIGH);
analogWrite ( PWM_mot, 90);
analogWrite ( PWM_mot2, 90);
break;
case -40:
{ for (int red = 50; red < 71; red += 10)

analogWrite (led7, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, HIGH);
digitalWrite (motorC, LOW);
digitalWrite (motorD, HIGH);
analogWrite ( PWM_mot, 110);
analogWrite ( PWM_mot2, 110);
break;
case -50:
{ for (int red = 70; red < 101; red += 10)

analogWrite (led7, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, HIGH);
digitalWrite (motorC, LOW);
digitalWrite (motorD, HIGH);
analogWrite ( PWM_mot, 150);
analogWrite ( PWM_mot2, 150);
break;
case -60:
{ for (int red = 100; red < 131; red += 10)

analogWrite (led7, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, HIGH);
digitalWrite (motorC, LOW);
digitalWrite (motorD, HIGH);
analogWrite ( PWM_mot, 170);
analogWrite ( PWM_mot2, 170);
break;
case -70:
{ for (int red = 130; red < 161; red += 10)

analogWrite (led7, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, HIGH);
digitalWrite (motorC, LOW);
digitalWrite (motorD, HIGH);
analogWrite ( PWM_mot, 190);
analogWrite ( PWM_mot2, 190);
break;
case -80:
{ for (int red = 160; red < 201; red += 10)

analogWrite (led7, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, HIGH);
digitalWrite (motorC, LOW);
digitalWrite (motorD, HIGH);
analogWrite ( PWM_mot, 220);
analogWrite ( PWM_mot2, 220);
break;
case -90:
{ for (int red = 200; red < 256; red += 10)

analogWrite (led7, red);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, HIGH);
digitalWrite (motorC, LOW);
digitalWrite (motorD, HIGH);
analogWrite ( PWM_mot, 255);
analogWrite ( PWM_mot2, 255);
break;
}
}
}
}

 

動画

 

youtu.be

 

パターン3(sensamotorcar3)(青文字部分)

//A,Bは右タイヤ
//C,Dは左タイヤ
//A,Cは前進
//B,Dは後進
const int motorA = 1;
const int motorB = 2;
const int PWM_mot = 3;
const int motorC = 13;
const int motorD = 12;
const int PWM_mot2 = 11;

int led2 = 5;
int led3 = 6;
int led6 = 9;
int led7 = 10;

 

void setup() {
// シリアルモニターの初期化をする
Serial.begin(9600) ;
pinMode (motorA, OUTPUT);
pinMode (motorB, OUTPUT);
pinMode (motorC, OUTPUT);
pinMode (motorD, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led6, OUTPUT);
pinMode (led7, OUTPUT);


}
void loop() {

int i ;
long x, y, z;
//50回センサ値を読み込んで平均を算出
x = y = z = 0;
for (i = 0 ; i < 50 ; i++) {
x = x + analogRead(3) ; // X軸
y = y + analogRead(4) ; // Y軸
z = z + analogRead(5) ; // Z軸
}
x = x / 50 ;
y = y / 50 ;
z = z / 50 ;
int rotateX = (x - 288) / 2.48 - 90; //角度を求める式
int rotateY = (y - 275) / 2.48 - 90;
Serial.print("X:") ;
Serial.print(x) ;
Serial.print(" ") ;
Serial.print(rotateX) ;
Serial.print(" Y:") ;
Serial.print(y) ;
Serial.print(" ") ;
Serial.print(rotateY) ;
Serial.print(" Z:") ;
Serial.println(z) ;


{{{ if (rotateX <= 30, -30 <= rotateX) {

{ digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 0);
}
}
{ if (rotateX > 30) {

{ digitalWrite (led2, HIGH);
digitalWrite (led3, LOW);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 255);
analogWrite ( PWM_mot2, 100);
}
}
{ if (rotateX < -30) {

{ digitalWrite (led2, LOW);
digitalWrite (led3, HIGH);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 255);
analogWrite ( PWM_mot2, 100);
}
}
}

{ { if (rotateY <= 30, -30 <= rotateY) {

{ digitalWrite (led6, LOW);
digitalWrite (led7, LOW);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 0);
}
}
{ if (rotateY > 30) {

{ digitalWrite (led6, HIGH);
digitalWrite (led7, LOW);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 255);
analogWrite ( PWM_mot2, 100);
}
}
{ if (rotateY < -30) {

{ digitalWrite (led6, LOW);
digitalWrite (led7, HIGH);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, HIGH);
digitalWrite (motorC, LOW);
digitalWrite (motorD, HIGH);
analogWrite ( PWM_mot, 255);
analogWrite ( PWM_mot2, 100);
}
}
}
}


}

 

動画

youtu.be

 

パターン4 (sensamotorcar4)(青文字部分)

//A,Bは右タイヤ
//C,Dは左タイヤ
//A,Cは前進
//B,Dは後進

//led2は右
//led3は左
//led6は前
//led7は後ろ
const int motorA = 1;
const int motorB = 2;
const int PWM_mot = 3;
const int motorC = 13;
const int motorD = 12;
const int PWM_mot2 = 11;

int led2 = 5;
int led3 = 6;
int led6 = 9;
int led7 = 10;

 

void setup() {
// シリアルモニターの初期化をする
Serial.begin(9600) ;
pinMode (motorA, OUTPUT);
pinMode (motorB, OUTPUT);
pinMode (motorC, OUTPUT);
pinMode (motorD, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led6, OUTPUT);
pinMode (led7, OUTPUT);


}
void loop() {

int i ;
long x, y, z;
//50回センサ値を読み込んで平均を算出
x = y = z = 0;
for (i = 0 ; i < 50 ; i++) {
x = x + analogRead(3) ; // X軸
y = y + analogRead(4) ; // Y軸
z = z + analogRead(5) ; // Z軸
}
x = x / 50 ;
y = y / 50 ;
z = z / 50 ;
int rotateX = (x - 288) / 2.48 - 90; //角度を求める式
int rotateY = (y - 275) / 2.48 - 90;
Serial.print("X:") ;
Serial.print(x) ;
Serial.print(" ") ;
Serial.print(rotateX) ;
Serial.print(" Y:") ;
Serial.print(y) ;
Serial.print(" ") ;
Serial.print(rotateY) ;
Serial.print(" Z:") ;
Serial.println(z) ;


{

{ if (rotateX <= 30, -30 <= rotateX )
if ( rotateY <= 30, -30 <= rotateY) {

{ digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
digitalWrite (led6, LOW);
digitalWrite (led7, LOW);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 0);
}
}
{ if (rotateX > 30 )
if ( rotateY <= 30, -30 <= rotateY) {
{ digitalWrite (led2, HIGH);
digitalWrite (led3, LOW);
digitalWrite (led6, LOW);
digitalWrite (led7, LOW);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 0);
analogWrite ( PWM_mot2, 100);
}
}
{ if (rotateX < -30 )
if ( rotateY <= 30, -30 <= rotateY) {

{ digitalWrite (led2, LOW);
digitalWrite (led3, HIGH);
digitalWrite (led6, LOW);
digitalWrite (led7, LOW);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, LOW);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 255);
analogWrite ( PWM_mot2, 100);
}
}

 

{ if (rotateY > 30 )
if ( rotateX <= 30, -30 <= rotateX) {

{ digitalWrite (led6, HIGH);
digitalWrite (led7, LOW);
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
}
digitalWrite (motorA, HIGH);
digitalWrite (motorB, LOW);
digitalWrite (motorC, HIGH);
digitalWrite (motorD, LOW);
analogWrite ( PWM_mot, 255);
analogWrite ( PWM_mot2, 100);
}
}
{ if (rotateY < -30 )
if ( rotateX <= 30, -30 <= rotateX) {
{ digitalWrite (led6, LOW);
digitalWrite (led7, HIGH);
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
}
digitalWrite (motorA, LOW);
digitalWrite (motorB, HIGH);
digitalWrite (motorC, LOW);
digitalWrite (motorD, HIGH);
analogWrite ( PWM_mot, 255);
analogWrite ( PWM_mot2, 100);
}
}
}
}

動画

youtu.be

 

このような感じです。

回数が増えるにつれて完成度が高くなっているのがわかりますか?

書いていて完成系が見えてきたと個人的には思いましたが、

よく考えてみるとこれ「セグウェイ」ですね。

セグウェイもこんな感じだったと思います。

以上です。

 

今回は4種類書きましたけど、ほかのスケッチがまだあったので次回も書こうかと思います。

また次回お会いしましょう。

さようなら

 

おまけ動画

youtu.be

スケッチを撮影

広告を非表示にする