Control Multiple Color Pods with Sensor Inputs

I hope I have come to the right place on the forum to try to figure this out.

I currently have a code put together that uses the input from a sensor, in this case an IR sensor, and changes the LED pattern if an object gets close.

I have step one of my vision completed, being the working code that uses the sensor input and controls the LEDs. Step two is proving a little more difficult.

What I am trying to do is to connect more sensors and more pods, each being controlled by its own IR sensor.

Is there a way that I can connect a bunch of these together and have each once controlled on its own from a single Arduino?

Also, I have played with the code a little to get the desired outcome, but is there a way I can code this so that the IR sensing continues as the LEDs run through their functions? Right now, there is a delay between readings while the applicable pattern is run, so there is no IR sensor input until the LEDs complete their whole function.

Below is the code.

Any help is greatly appreciated!

#include <Adafruit_NeoPixel.h>



int ambientIR;                // variable to store the IR coming from the ambient
int obstacleIR;               // variable to store the IR coming from the object
int value[10];                // variable to store the IR values
int distance;                 // variable that will tell if there is an obstacle or not


#define RingPin 6
#define IRpin A0
#define IRemitter 2

Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, RingPin, NEO_GRB + NEO_KHZ800);



void setup(){
 Serial.begin(9600);         // initializing Serial monitor
 pinMode(IRemitter,OUTPUT);  // IR emitter LED on digital pin 2
 digitalWrite(IRemitter,LOW);// setup IR LED as off
 pinMode(RingPin,OUTPUT);         // lights in digital pin 6
 strip.begin();
 rainbowCycleStart(5);
 strip.show(); // Initialize all pixels to 'off'

 
}

void loop(){
 distance = readIR(5);       // calling the function that will read the distance and passing the "accuracy" to it
 Serial.println(distance);   // writing the read value on Serial monitor
  RingChange();                // uncomment to activate the buzzer function

}

int readIR(int times){
 for(int x=0;x<times;x++){     
   digitalWrite(IRemitter,LOW);           // turning the IR LEDs off to read the IR coming from the ambient
   delay(1);                                             // minimum delay necessary to read values
   ambientIR = analogRead(IRpin);  // storing IR coming from the ambient
   digitalWrite(IRemitter,HIGH);          // turning the IR LEDs on to read the IR coming from the obstacle
   delay(1);                                             // minimum delay necessary to read values
   obstacleIR = analogRead(IRpin);  // storing IR coming from the obstacle
   value[x] = ambientIR-obstacleIR;   // calculating changes in IR values and storing it for future average
 }

 for(int x=0;x<times;x++){        // calculating the average based on the "accuracy"
   distance+=value[x];
 }
 return(distance/times);            // return the final value
}


//-- Function to change ring color --//
void RingChange(){
 if (distance>10){
   if(distance>800){ // changed ring pattern if object is placed on it
// rainbowCycle(1);
//colorWipe(strip.Color(0, 255, 0), 50); // Green
 colorWipe(strip.Color(random(255), random(255), random(255)), 75); // Blue
// colorWipe(strip.Color(255, 0, 0), 50); // Blue
// theaterChase(strip.Color(0, 0, 255), 75); // Blue
// theaterChase(strip.Color(127, 127, 127), 50); // White
 //theaterChase(strip.Color(127, 0, 0), 50); // Red
 //theaterChase(strip.Color(0, 0, 127), 50); // Blue
 //theaterChaseRainbow(50);

   }
 }
 if(distance<800){  // ring pattern with no object on it
//rainbowCycle(1);
// colorWipe(strip.Color(255, 0, 0), 30); // Red
//colorWipe(strip.Color(0, 255, 0), 50); // Green
// colorWipe(strip.Color(0, 0, 255), 50); // Blue
//theaterChase(strip.Color(127, 127, 127), 50); // White
 //theaterChase(strip.Color(127, 0, 0), 50); // Red
 theaterChase(strip.Color(0, 0, 255), 75); // Blue
 //theaterChaseRainbow(50);
 }
}


// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
 for(uint16_t i=0; i<strip.numPixels(); i++) {
   strip.setPixelColor(i, c);
   strip.show();
   delay(wait);
 }
}

void rainbow(uint8_t wait) {
 uint16_t i, j;

 for(j=0; j<256; j++) {
   for(i=0; i<strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel((i+j) & 255));
   }
   strip.show();
   delay(wait);
 }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
 uint16_t i, j;

 for(j=0; j<256; j++) { // 5 cycles of all colors on wheel
   for(i=0; i< strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
   }
   strip.show();
   delay(wait);
 }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycleStart(uint8_t wait) {
 uint16_t i, j;

 for(j=0; j<256*3; j++) { // 5 cycles of all colors on wheel
   for(i=0; i< strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
   }
   strip.show();
   delay(wait);
 }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
 for (int j=0; j<1; j++) {  //do 10 cycles of chasing
   for (int q=0; q < 6; q++) {
     for (uint16_t i=0; i < strip.numPixels(); i=i+6) {
       strip.setPixelColor(i+q, c);    //turn every third pixel on
     }
     strip.show();

     delay(wait);

     for (uint16_t i=0; i < strip.numPixels(); i=i+6) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
     }
   }
 }
}



//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
 for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
   for (int q=0; q < 3; q++) {
     for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
     }
     strip.show();

     delay(wait);

     for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, 0);        //turn every third pixel off
     }
   }
 }
}



// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
 WheelPos = 255 - WheelPos;
 if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 }
 if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 }
 WheelPos -= 170;
 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

CupSensingPodsV2.ino (5.08 KB)

Hello. I'll add "welcome" once you have edited your post above and put in code tags using the </> icon. It tells you about doing that in the "read this first" message at the top of each forum section. Newbies never read that.

Paul

Revisions made in the posting. Much easier to view now.

Ok, welcome! And thankyou.

A question and a suggestion. Question: how many sensors and led groups did you have in mind?

Suggestion: each sensor and led group could be controlled by an attiny85.

I will be looking to control 20 pods. Each one individually with its own IR sensor.

Ok, yes i think that with some changes, the code can respond to changes in the ir sensor more quickly. You can use your Uno or whatever to perfect this code, then program 20 attiny85 to run each pod.

How will you power these pods? Each will need around 750mA i think. A USB phone charger for each pod may be an idea. These would provide 5V regulated/switch mode power which the leds and attiny85 can use directly.

Thanks for the input! I ended up going through and re-coding the entire script. I still have some bugs to work out (using the delay function in the IR sensing code), and some serious simplification to do (still trying to learn arrays), but I have it working with 10 pods per Arduino Mega. Ideally I would like to run all 20 pods on a single Mega, but with the current coding, I do not have enough analog inputs. (need 20 analog and 40 digital with current code).

Code is attached if you are interested.

CupSensingPods10Pod.ino (28.1 KB)

ATtiny45-85.png