使用倾斜传感器

时间:2015-05-26 08:46:09

标签: android c tilt-sensor

在我的Arduino程序中使用倾斜传感器时出现以下问题。没有倾斜传感器,一切都正常,但在使用倾斜传感器时,功能的工作顺序不正确。

首先我的版本没有倾斜传感器。这个版本工作正常。我正在使用5行中的710个LED。我有4个功能,一个接一个地启动。我希望使用倾斜传感器的原理相同。如果我改变倾斜位置,程序必须从我的灯板的另一侧开始,就像沙漏一样。

#include <Adafruit_NeoPixel.h>
#define PIN 6  // led
Adafruit_NeoPixel strip = Adafruit_NeoPixel(710, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
 Serial.begin(9600);
 strip.begin();
 strip.show();
}

void loop() {
  colorWipe(strip.Color(255, 147, 41), 127);
  colorWipe1(strip.Color(255, 147, 41), 127); //warm white
  colorWipe2(strip.Color(1, 1, 1), 127); //black effect
  colorWipe3(strip.Color(255, 147, 41), 127); //candle effect
}


void colorWipe(uint32_t c, uint8_t wait) {  
 int a;
 int k, l, m, n, o;
 for (a=1 ; a<600; a++) {
  for (k=0,l=283,m=284,n=567,o=568 ;k<10,l<273,m<294, n<557, o<578; k++,l--,m++,n--,o++) {

      strip.setPixelColor(k, random(250,255), random(142,149), random(35,43), random(35,120)); 
      strip.setPixelColor(l, random(250,255), random(142,149), random(35,43), random(35,120));      
      strip.setPixelColor(m, random(250,255), random(142,149), random(35,43), random(35,120));
      strip.setPixelColor(n, random(250,255), random(142,149), random(35,43), random(35,120));
      strip.setPixelColor(o, random(250,255), random(142,149), random(35,43), random(35,120));          
    }        
      strip.show();
      delay(random(50-70));
  }
}

void colorWipe1(uint32_t c, uint8_t wait) { 
 int a, b;
 int k, l, m, n, o;
 int q, r, s, t, v; 
 int z=10, x;
 for (b=1; b<141;b=b+random(0,2)) {
   for (k=1+b,l=282-b,m=283+b,n=566-b,o=567+b ;k<11+b,l<273-b,m<292+b, n<557-b, o<576+b; k=k+random(0,2),l=l-random(0,2),m=m+random(0,2),n=n-random(0,2),o=o+random(0,2)) {
       strip.setPixelColor(k, random(250,255), random(142,149), random(35,43), random(35,120)); 
       strip.setPixelColor(l, random(250,255), random(142,149), random(35,43), random(35,120));      
       strip.setPixelColor(m, random(250,255), random(142,149), random(35,43), random(35,120));
       strip.setPixelColor(n, random(250,255), random(142,149), random(35,43), random(35,120));
       strip.setPixelColor(o, random(250,255), random(142,149), random(35,43), random(35,120));  
       delay(random(120-150));
      }        
        strip.show();
        delay(random(50-70));
  }
}  

void colorWipe2(uint32_t c, uint8_t wait) {  
 int a, b, d=0;
 int k, l, m, n, o;
 int q, r, s, t, v;
 for (b=0; b<131; b=b+random(0,2)) {

   for (k=0+b,l=283-b,m=284+b,n=567-b,o=568+b ;k<1+b,l<282-b,m<285+b, n<566-b, o<570+b; k=k+random(0,2),l=l-random(0,2),m=m+random(0,2),n=n-random(0,2),o=o+random(0,2)) {

      strip.setPixelColor(k, 255, 147, 41, d);       
      strip.setPixelColor(l, 255, 147, 41, d);
      strip.setPixelColor(m, 255, 147, 41, d);
      strip.setPixelColor(n, 255, 147, 41, d);
      strip.setPixelColor(o, 255, 147, 41, d);
      delay(random(450-500));


     } 
     strip.show();
       delay(random(220-270));
   }   

}

void colorWipe3(uint32_t c, uint8_t wait) {  
 int a;
 int k, l, m, n, o;
  for (a=1 ; a<600; a++) {
   for (k=132, l=151 , m=416 , n=435 , o=700 ;k<142, l>141, m<426, n>425, o<710; k++, l--, m++, n--, o++ ) {
      strip.setPixelColor(k, random(250,255), random(142,149), random(35,43), random(55,180)); 
      strip.setPixelColor(l, random(250,255), random(142,149), random(35,43), random(55,180));      
      strip.setPixelColor(m, random(250,255), random(142,149), random(35,43), random(55,180));
      strip.setPixelColor(n, random(250,255), random(142,149), random(35,43), random(55,180));
      strip.setPixelColor(o, random(250,255), random(142,149), random(35,43), random(55,180));  

    }        
      strip.show();
      delay(random(50-70));
  }
}

Hier是使用倾斜传感器的示例。

int tilt_switch = 10;
#define PIN 6
#include <Adafruit_NeoPixel.h>
int tilt_switch_state = 0;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(710, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
 Serial.begin(9600);
 strip.begin();
 strip.show();
}

void loop() {
  tilt_switch_state = digitalRead(tilt_switch);
  //check to see if the tilt switch is closed
  if (tilt_switch_state == HIGH) {

    colorWipe(strip.Color(255, 147, 41), 127);
    colorWipe1(strip.Color(255, 147, 41), 127);
  }

  if (tilt_switch_state == LOW) {
    colorWipe3(strip.Color(255, 147, 41), 127);

   }
  }

  void colorWipe(uint32_t c, uint8_t wait) {
  int a;
  int k, l, m, n, o;
  int b;
  int q, r, s, t, v;
  int z = 10, x;
  for (a = 1 ; a < 600; a++) {

for (k = 0, l = 283, m = 284, n = 567, o = 568 ; k < 10, l < 273, m < 294, n < 557, o < 578; k++, l--, m++, n--, o++) {

  strip.setPixelColor(k, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
  strip.setPixelColor(l, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
  strip.setPixelColor(m, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
  strip.setPixelColor(n, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
  strip.setPixelColor(o, random(250, 255), random(142, 149), random(35, 43), random(35, 120));

}
strip.show();
if (tilt_switch_state == HIGH) {
  for (k = 132, l = 151 , m = 416 , n = 435 , o = 700 ; k<142, l>141, m<426, n>425, o < 710; k++, l--, m++, n--, o++ ) {
    strip.setPixelColor(k,  1, 1, 1, 1);
    strip.setPixelColor(l,  1, 1, 1, 1);
    strip.setPixelColor(m,  1, 1, 1, 1);
    strip.setPixelColor(n,  1, 1, 1, 1);
    strip.setPixelColor(o,  1, 1, 1, 1);
  }

  break;

 }
  delay(random(50 - 70));
 }    
}

void colorWipe1(uint32_t c, uint8_t wait) {

int a, b;
int k, l, m, n, o;
int q, r, s, t, v;
int z = 10, x;
for (b = 1; b < 141; b = b + random(0, 2)) {

for (k = 1 + b, l = 282 - b, m = 283 + b, n = 566 - b, o = 567 + b ; k < 11 + b, l < 273 - b, m < 292 + b, n < 557 - b, o < 576 + b; k = k + random(0, 2), l = l - random(0, 2), m = m + random(0, 2), n = n - random(0, 2), o = o + random(0, 2)) {
  strip.setPixelColor(k, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
  strip.setPixelColor(l, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
  strip.setPixelColor(m, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
  strip.setPixelColor(n, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
  strip.setPixelColor(o, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
  delay(random(120 - 150));
}
strip.show();

if(tilt_switch_state == HIGH){
 for (k=132, l=151 , m=416 , n=435 , o=700 ;k<142, l>141, m<426, n>425, o<710; k++, l--, m++, n--, o++ ) {
 strip.setPixelColor(k,  1, 1, 1, 1);
 strip.setPixelColor(l,  1, 1, 1, 1);
 strip.setPixelColor(m,  1, 1, 1, 1);
 strip.setPixelColor(n,  1, 1, 1, 1);
 strip.setPixelColor(o,  1, 1, 1, 1);

Serial.println("tilt3");

}

delay(random(50 - 70));
 break;
 }
}

 void colorWipe3(uint32_t c, uint8_t wait) {
   int a;
   int k, l, m, n, o;
  for (a = 1 ; a < 600; a++) {
   for (k = 132, l = 151 , m = 416 , n = 435 , o = 700 ; k<142, l>141, m<426, n>425, o < 710; k++, l--, m++, n--, o++ ) {

  strip.setPixelColor(k, random(250, 255), random(142, 149), random(35, 43), random(55, 180));
  strip.setPixelColor(l, random(250, 255), random(142, 149), random(35, 43), random(55, 180));
  strip.setPixelColor(m, random(250, 255), random(142, 149), random(35, 43), random(55, 180));
  strip.setPixelColor(n, random(250, 255), random(142, 149), random(35, 43), random(55, 180));
  strip.setPixelColor(o, random(250, 255), random(142, 149), random(35, 43), random(55, 180));

}
strip.show();
if (tilt_switch_state == LOW) {
  for (k = 0, l = 283, m = 284, n = 567, o = 568 ; k < 10, l < 273, m < 294, n < 557, o < 578; k++, l--, m++, n--, o++) {
    strip.setPixelColor(k,  1, 1, 1, 1);
    strip.setPixelColor(l,  1, 1, 1, 1);
    strip.setPixelColor(m,  1, 1, 1, 1);
    strip.setPixelColor(n,  1, 1, 1, 1);
    strip.setPixelColor(o,  1, 1, 1, 1);
    Serial.println("tilt2");
  }
  break;
}

delay(random(50 - 70));
 }
}

如果我只使用2个功能colorWipe和colorWipe3(它们从光板的不同侧面开始)它工作正常,但如果我在colorWipe后添加colorWipe1,我有以下错误: colorWipe和colorWipe1一起启动,但是没有倾斜,每个功能都会在上一个功能之后启动。

由于 安迪

1 个答案:

答案 0 :(得分:1)

可能的答案:

int tilt_switch = 10;
#define PIN 6
#include <Adafruit_NeoPixel.h>
int tilt_switch_state = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(710, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(9600);
  strip.begin();
  strip.show();
 pinMode(tilt_switch, INPUT); 


}

void loop() {
strip.show();  
tilt_switch_state = digitalRead(tilt_switch);
  if (digitalRead(tilt_switch)==HIGH) {   
    Serial.println("1");
      colorWipe(strip.Color(255, 147, 41), 127);
      delay(10);
      colorWipe1(strip.Color(255, 147, 41), 127);
      delay(10);
      colorWipe2(strip.Color(255, 147, 41), 127);
      delay(10);
      colorWipe4(strip.Color(255, 147, 41), 127);
  } else {
    Serial.println("0");
    colorWipe0(strip.Color(255, 147, 41), 127);
  }

}

void colorWipe(uint32_t c, uint8_t wait) {
  int a,b, k, l, m, n, o, q, r, s, t, v,x;
  int z = 10;
  for (a = 1 ; a < 220; a++) {    
    for (k = 0, l = 283, m = 284, n = 567, o = 568 ; k < 10,l < 273,m < 294,n < 557,o < 578; k++, l--, m++, n--, o++) {

      strip.setPixelColor(k, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
      strip.setPixelColor(l, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
      strip.setPixelColor(m, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
      strip.setPixelColor(n, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
      strip.setPixelColor(o, random(250, 255), random(142, 149), random(35, 43), random(35, 120));
      Serial.println("wipe");
    }

    strip.show();      
      if (digitalRead(tilt_switch)==HIGH) { 
          /*for (k = 132, l = 151 , m = 416 , n = 435 , o = 700 ; k<142,l>141,m<426,n>425,o < 710; k++, l--, m++, n--, o++ ) {
            strip.setPixelColor(k,  1, 1, 1, 1);
            strip.setPixelColor(l,  1, 1, 1, 1);
            strip.setPixelColor(m,  1, 1, 1, 1);
            strip.setPixelColor(n,  1, 1, 1, 1);
            strip.setPixelColor(o,  1, 1, 1, 1);
            Serial.println("tilt");
      }*/
    }

    if (digitalRead(tilt_switch)==LOW) {
      Serial.println("----del");
      break;
    }

    delay(random(50 - 70));

  }    

如果有人有兴趣,我也会添加其他功能。