Right functions order in Arduino program using tilt sensor

139 views Asked by At

I have the following problem using tilt sensor in my Arduino program. Without tilt sensor everything works correct, but during using tilt sensor the functions works not in the right order.

Hier first my version without tilt sensor. This version works fine. I' am using 710 leds in 5 lines. I have 4 functions, which starts one after another. The same principle i want to have using the tilt sensor. If i change the tilt position the program must start from another side of my light panel, like in hourglass.

#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 is an example using tilt sensor.

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

If i use only 2 functions colorWipe and colorWipe3 (they starts from different sides of light panel) it works fine, but if i add colorWipe1 after colorWipe, i have the following mistake: colorWipe and colorWipe1 starts together, however without tilt each function starts after the previous function.

Thanks Andy

1

There are 1 answers

0
andy_bu On

Hier the possible answer:

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

  }    

If someone are interesting i'll add other functions too.