0

我在 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 一起开始,但是没有倾斜每个函数在之后开始上一个功能。

谢谢安迪

4

1 回答 1

1

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

  }    

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

于 2015-06-01T13:53:17.480 回答