Hi all,
Looking for some help with my code. So, to give an overview, I've got 15 sharp IR sensors which stretch out along 10 meters. I'm looking to turn a bunch of LEDs turn on depending on where someone is. This works fine.
The issue is when more people come into the picture. I've programmed in a counter that updates a global variable and changes the color of the LEDs depending on how many people are using this (red for 1, green for 2, 3 blue etc). However, when the counter updates the color changes for everyone.
Any ideas on how to solve/best way to approach?
I appreciate this code is a little hacky.
void loop(){
averageRead();
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
LEDpos = 0;
pos = 0;
}
//Beam 1
if (average > threshold){ //average IR reading for sensor 1
peopleIn = true;
if (pos < 1 && counter == 1){
for(int i = 0; i <= 6; i++){
LEDpos = i;
scanner(0,0,127, 15);
}
}
if (pos < 1 && counter == 2){
for(int i = 0; i <= 6; i++){
LEDpos2 = i;
scanner(0,0,127, 15);
}
}
if (pos < 1 && counter == 3){
for(int i = 0; i <= 6; i++){
LEDpos3 = i;
scanner(0,0,127, 15);
}
}
////
if (pos > 1 && counter == 1){
for(int i = 11; i >= 6; i--){
LEDpos = i;
scanner(0,0,127, 15);
}
}
if (pos > 1 && counter == 2) {
for(int i = 11; i >= 6; i--){
LEDpos2 = i;
scanner(0,0,127, 15);
}
}
if (pos > 1 && counter == 3){
for(int i = 11; i >= 6; i--){
LEDpos3 = i;
scanner(0,0,127, 15);
}
}
clearL();
previousMillis = currentMillis; // Reset the counter to stop it defaulting to demo mode.
pos = 1;
}
//Beam 2
if (average1 > threshold){
if (peopleIn == true){
counter++;
peopleIn = false;
}
if (pos < 2 && counter == 1){
for(int i = 6; i <= 11; i++){
LEDpos = i;
scanner(0,0,127, 15);
}
}
if (pos < 2 && counter == 2){
for(int i = 6; i <= 11; i++){
LEDpos2 = i;
scanner(0,0,127, 15);
}
}
if (pos < 2 && counter == 3){
for(int i = 6; i <= 11; i++){
LEDpos3 = i;
scanner(0,0,127, 15);
}
}
if (pos > 2 && counter == 1){
for(int i = 22; i >= 11; i--){
LEDpos = i;
scanner(0,0,127, 15);
}
}
if (pos > 2 && counter == 2){
for(int i = 22; i >= 11; i--){
LEDpos2 = i;
scanner(0,0,127, 15);
}
}
if (pos > 2 && counter == 3){
for(int i = 22; i >= 11; i--){
LEDpos3 = i;
scanner(0,0,127, 15);
}
}
pos = 2;
previousMillis = currentMillis;
}
//Beam 3
if (average2 > threshold){
if (pos < 3 && counter == 1){
for(int i = 11; i <= 22; i++){
LEDpos = i;
scanner(0,0,127, 15);
}
}
if (pos < 3 && counter == 2){
for(int i = 11; i <= 22; i++){
LEDpos2 = i;
scanner(0,0,127, 15);
}
}
if (pos < 3 && counter == 3){
for(int i = 11; i <= 22; i++){
LEDpos3 = i;
scanner(0,0,127, 15);
}
}
if (pos > 3 && counter == 1){
for(int i = 33; i >= 22; i--){
LEDpos = i;
scanner(0,0,127, 15);
}
}
if (pos > 3 && counter == 2){
for(int i = 33; i >= 22; i--){
LEDpos2 = i;
scanner(0,0,127, 15);
}
}
if (pos > 3 && counter == 3){
for(int i = 33; i >= 22; i--){
LEDpos3 = i;
scanner(0,0,127, 15);
}
}
pos = 3;
previousMillis = currentMillis;
delay(t);
}
//Beam 4
if (average3 > threshold){
if (pos < 4 && counter == 1){
for(int i = 22; i <= 33; i++){
LEDpos = i;
scanner(0,0,127, 15);
}
}
if (pos < 4 && counter == 2){
for(int i = 22; i <= 33; i++){
LEDpos2 = i;
scanner(0,0,127, 15);
}
}
if (pos < 4 && counter == 3){
for(int i = 22; i <= 33; i++){
LEDpos3 = i;
scanner(0,0,127, 15);
}
}
if (pos > 4 && counter == 1){
for(int i = 44; i >= 33; i--){
LEDpos = i;
scanner(0,0,127, 15);
}
}
if (pos > 4 && counter == 2){
for(int i = 44; i >= 33; i--){
LEDpos2 = i;
scanner(0,0,127, 15);
}
}
if (pos > 4 && counter == 3){
for(int i = 44; i >= 33; i--){
LEDpos3 = i;
scanner(0,0,127, 15);
}
}
pos = 4; //update our global positioning
previousMillis = currentMillis;
}
if (average4 > threshold){
if (pos < 5 && counter == 1){
for(int i = 33; i <= 44; i++){
LEDpos = i;
scanner(0,0,127, 15);
}
}
if (pos < 5 && counter == 2){
for(int i = 33; i <= 44; i++){
LEDpos2 = i;
scanner(0,0,127, 15);
}
}
if (pos < 5 && counter == 3){
for(int i = 33; i <= 44; i++){
LEDpos3 = i;
scanner(0,0,127, 15);
}
}
if (pos > 5 && counter == 1){
for(int i = 55; i >= 44; i--){
LEDpos = i;
scanner(0,0,127, 15);
}
}
if (pos > 5 && counter == 2){
for(int i = 55; i >= 44; i--){
LEDpos2 = i;
scanner(0,0,127, 15);
}
}
if (pos > 5 && counter == 3){
for(int i = 55; i >= 44; i--){
LEDpos3 = i;
scanner(0,0,127, 15);
}
}
pos = 5; //update our global positioning
previousMillis = currentMillis;
}
////////////////// ETC ETC............ ////////////////////////
}
void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) {
for( int i = -160; i < 0; i++ ){
strip.setPixelColor(LEDpos + i , strip.Color(r, g, b));
strip.setPixelColor(LEDpos2 + i ,strip.Color(255,0, 0));
strip.setPixelColor(LEDpos3 + i ,strip.Color(0,255, 0));
strip.setPixelColor(LEDpos + 1, strip.Color(r, g, b));
strip.setPixelColor(LEDpos2 + 1 ,strip.Color(255,0, 0));
strip.setPixelColor(LEDpos3 + 1 ,strip.Color(0,255, 0));
strip.setPixelColor(LEDpos + 2, strip.Color(r/3, g/3, b/3));
strip.setPixelColor(LEDpos2 + 2 , strip.Color(255/3, 0/3, 0/3));
strip.setPixelColor(LEDpos3 + 2 , strip.Color(0, 255/3, 0/3));
strip.setPixelColor(LEDpos + 3, strip.Color(r/10, g/10, b/10));
strip.setPixelColor(LEDpos2 + 3 , strip.Color(255/10, 0/10, 0/10));
strip.setPixelColor(LEDpos3 + 3 , strip.Color(0, 255/10, 0/10));
strip.setPixelColor(LEDpos + 4, strip.Color(r/20, g/20, b/20));
strip.setPixelColor(LEDpos2 + 4 , strip.Color(255/20, 0/20, 0/20));
strip.setPixelColor(LEDpos3 + 4 , strip.Color(0, 255/20, 0/20));
strip.setPixelColor(LEDpos + 5, strip.Color(r/50, g/50, b/50));
strip.setPixelColor(LEDpos2 + 5 , strip.Color(255/50, 0/50, 0/50));
strip.setPixelColor(LEDpos3 + 5 , strip.Color(0, 255/50, 0/50));
}
strip.show();
}
void clearL(){
for (int i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0);
}
}