Hello everyone!
I am using gsr sensor, a neopixel strip, and a ir sharp sensor.
There are three main steps:
- The led is off, when the user put his finger on the gsr sensor the led start fading and blinking. --> WORK
- When a certain value is detected by GSR the led stops blinking and start spinning. --> WORK
- While the led is spinning the ir sensor should detect if someone is passing by and updates a counter. --> NOT WORKING
I have put a delay() in the ir sensor because otherwise it detects more than one passage at a time. The delay() obviously stop all the program including the led strip. I have tried to use the Millis( ) function but without success.
Here the code
#include <Adafruit_NeoPixel.h>
#define PIN 2
#define NUM_PIXELS 146
Adafruit_NeoPixel strip(NUM_PIXELS, PIN, NEO_GRB + NEO_KHZ800);
const int GSR = A0;
int threshold = 0;
int sensorValue;
int emozione[6];
int delta;
int giri;
int ir_pin = 5;
int hitObject = false;
long sum = 0;
bool lockout = false; //to print the sensor value only when the fingers are placed
bool luce = false; // to change the status of the light
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
strip.begin();
strip.show();
Serial.begin(9600);
pinMode(ir_pin, INPUT); //counter inoput
delay(1000);
}
void loop() {
sensorValue = analogRead(GSR);
if (sensorValue > 400) { //when the fingers are not resting the treshold is reset
sum = 0;
}
if (luce == false && sensorValue < 400) { //if the light is off and the fingers are resting it starts to throb
GetTreshold(); //get the resting conductivity of the utent
FadeInOut(0xff, 0x77, 0x00); //the light pulses and the user's emotion is monitored
}
if (luce == true && giri > 0) { // when an emotion is detected (and therefore the number of giri he has to do) the light pulses
for (int i = 0; i < NUM_PIXELS; i++) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
CounterGiri(); // monitor the user's circumambulation around the object
}
strip.setPixelColor(i, 0xff, 0x77, 0x00);
strip.setPixelColor(i - 1, 0xff, 0x77, 0x00);
// strip.setPixelColor(i - 2, 0xff, 0x77, 0x00);
strip.show();
delay(20);
strip.clear();
}
}
}
void GetTreshold() { // determines the teshold, the base value on which to calculate the delta
if (sum == 0) {
delay (1000);
for (int i = 0; i < 700; i++) { // average between 500 values to have a treshold
sensorValue = analogRead(GSR);
sum += sensorValue;
delay(5);
}
threshold = sum / 700;
Serial.print("threshold =");
Serial.println(threshold);
}
}
void ReadValue() {
sensorValue = analogRead(GSR);
Serial.print("sensorValue=");
Serial.println(sensorValue);
delay(10);
delta = threshold - sensorValue;
if (delta > 20 && luce == false) { //if the difference between treshold and detected value is greater than a tot, an emotion is recorded
printemotion();
}
}
void printemotion() {
Serial.println("EMOZIONE RILEVATA!");
for (int i = 0; i < 6; i++) { //save the 6 values following the detection of the emotion to approach the minimum of the peak
sensorValue = analogRead(GSR);
emozione[i] = sensorValue - threshold;
delay(200);
}
Serial.println(abs(emozione[5]));
lockout = true;
giri = abs(emozione[5]) - 20; //return the number of turns (giri)
Serial.print("emozione =");
Serial.println(giri);
luce = true; //change the state of the light which stops pulsing and starts spinning
delay (3000); //time between when the light stops pulsing and starts spinning
}
void FadeInOut(byte red, byte green, byte blue) {
float r, g, b;
for (int k = 0; k < 256; k = k + 2) {
ReadValue();
r = (k / 256.0) * red;
g = (k / 256.0) * green;
b = (k / 256.0) * blue;
setAll(r, g, b);
strip.show();
}
for (int k = 255; k >= (-1); k = k - 4) {
ReadValue();
r = (k / 256.0) * red;
g = (k / 256.0) * green;
b = (k / 256.0) * blue;
setAll(r, g, b);
strip.show();
}
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
}
void setAll(byte red, byte green, byte blue) {
for (int i = 0; i < NUM_PIXELS; i++ ) {
setPixel(i, red, green, blue);
}
strip.show();
}
void CounterGiri() {
int val = digitalRead(ir_pin);
if ( (val == 0) && (hitObject == false) && giri > 0 ) {
giri--;
hitObject = true;
Serial.print("Giri = ");
Serial.println( giri);
//delay (500); //trying not to use it because it stops the spin o the led too
}
else if ( (val == 1) && (hitObject == true) ) {
hitObject = false;
}
if (giri == 0) {
strip.clear();
}
}
Thank you!