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

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

Arduino 加速度センサ LED4つ明るさ変更

傾きによって明るさが変わります。

 

LEDの接続は↓参照

mrinvader007.hatenablog.jp

加速度センサの接続は↓参照

deviceplus.jp

 

 

内容↓

X軸Y軸それぞれの角度が変わるとLEDの明るさが変化する。(後半わかりにくい)

傾ける向きが変わると、点灯するLEDが変わる。

 

動画↓

youtu.be

 

スケッチ↓(青文字部分)

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

void setup()
{
// シリアルモニターの初期化をする
Serial.begin(9600) ;

}
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 - 280) / 2.56 - 90; //角度を求める式
int rotateY = (y - 264) / 2.56 - 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);
}
}


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);
}
}
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);
}
}
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);
}
}

}

広告を非表示にする