Go Down

Topic: Sharp IR sensor stops printing value (Read 334 times) previous topic - next topic

jehgar

I am having issues with the sharp IR sensor in which after the sensor gets a distance read of 550 or more and satisfies the last else if statement, the serial monitor will stop printing values and its as if the program is stuck in the else if statement until it resets itself after a couple of minutes. My goal is to have the sensor constantly reading for motion at all times.

Any suggestions or ideas?

Code: [Select]
#include <Adafruit_NeoPixel.h>
#include <DistanceGP2Y0A21YK.h>

#define PIN 6
#define PIXEL 16

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL, PIN, NEO_GRBW + NEO_KHZ800);


DistanceGP2Y0A21YK Dist;
int distance;

int R = 0;
int G = 0;
int B = 0;

int R2 = 255;
int G2 = 60;
int B2 = 80;

int sensorpin = 0;
int val = 0;

void setup() {
  strip.begin();
  strip.show();
  strip.setBrightness(127);

  Serial.begin(9600);
  // Dist.begin(A0);
}

void loop()
{
  /*
    distance = Dist.getDistanceCentimeter();
    Serial.print("\nDistance in centimers: ");
    Serial.print(distance);
    delay(2); //make it readable
  */
  val = analogRead(sensorpin);
  Serial.println(val);
  delay(100);

  // Breathe White
  //if (distance >= 16)
  if (val <= 400 )
  {
    Serial.println("WHITE");
    for ( R = G = B = 0; R < 150 && G < 150 && B < 150; R++ && G++ && B++) {
      for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(R, G, B));
        strip.show();
        delay(1);
      }
      delay(2);
    }

    // fade off
    for (R = G = B = 0; R > -1 && G > -1 && B > -1; R-- && G-- && B--) {
      for (int j = 0; j < strip.numPixels(); j++) {
        strip.setPixelColor(j, strip.Color(R, G, B));
        strip.show();
        delay(1);
      }
      delay(1);

    }
  }

  // BREATHE PINK
  //else if ( distance >= 8 )
  else if ( val >= 401 && val <= 550 )
  {
    Serial.println("PINK");
    for (int j = 64; j < 256; j++) {
      for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(j, 60, 80));
        strip.show();
      }
      delay (1);

      //blink off
      for (int j = 64; j > 200; j--) {
        for (int i = 0; i < strip.numPixels(); i++) {
          strip.setPixelColor(i, strip.Color(j, 0, 0));
          strip.show();
        }
        delay (2);

      }
    }
  }



  // ALERT RED
  //else if ( distance <= 7)
  else if ( val >= 551)
  {
    Serial.println("ALERT");
    for (int j = 0; j < 256; j++) {
      for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(255, 0, 0));
        strip.show();
      }
      delay (1);

      //blink off
      for (int j = 64; j > 0; j--) {
        for (int i = 0; i < strip.numPixels(); i++) {
          strip.setPixelColor(i, strip.Color(j, 0, 0));
          strip.show();
        }
        delay (2);
      }
    }
  }
}



JaBa

Hello,
my first Suggestion would be to clean up the code. Remove the unnecessary old (commented out) parts completely. There is no reason for R,G, and B in the for Loops as the values are always the same so you could just use one variable. Are you Aware that some of the for Loops make no sense (for (int j = 64; j > 200; j--)  for example).

And it would also help if you define what you mean by "its as if the program is stuck in the else if statement until it resets itself after a couple of minutes".  While "ist stuck", are there any changes at all with the LEDs?

What happens if you put a Serial.println("got this far") after the delay(2)? Does it print before the program hangs, or after it restarts? What happens if you put it after the delay(1)?


Go Up