Go Down

Topic: Ir Sensor (Read 250 times) previous topic - next topic

ribhav99

Hey,
So I have connected my arduino to LED lights and a mic and the light brightness, color, patterns, etc are determined by the ambient sound.
I am trying to add an ir sensor into the mix so I can use a remote to cycle bw different modes.
My code for the IR sensor works perfectly when I test it as a standalone program but when I integrate it with my code for the LEDs, I get different values for the same button on the remote.

slipstick

Hey, so you have a remote, a sensor and some code and they don't work together. Are you sure wouldn't like to give us a few more details like what these components are, what values you expect and what you get? You could maybe even show us the code that isn't working?

Steve


ribhav99

#2
Nov 10, 2018, 06:20 pm Last Edit: Nov 10, 2018, 06:22 pm by ribhav99
Yes sir.
Heres my code that works perfectly fine:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

#include <IRremote.h>

const int IR_INPUT = 7;
IRrecv irrecv(IR_INPUT);
decode_results irValue;

void setup(){
  Serial.begin(9600);
  irrecv.enableIRIn();
  irrecv.blink13(true);
}

void loop(){
  if (irrecv.decode(&irValue)){
        Serial.println(irValue.value);
        irrecv.resume();
  }
}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


This is my code for the LEDs. For simplicity, I'm removing some functions that decide the color, etc.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

#include <FastLED.h>
#include <math.h>
#include <IRremote.h>

#define LED_PIN  12
#define NUM_LEDS  300

const int IR_INPUT = 7;
IRrecv irrecv(IR_INPUT);
decode_results irValue;

const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
int ledPin = 12;
int red = 0;
int green = 0;
int blue = 0;
int brightness = 5;

function1
function2
function3
function4

void setup()
{
   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);
   FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
   irrecv.enableIRIn();
   irrecv.blink13(true);
}

void loop()
{

  if (irrecv.decode(&irValue)){
        Serial.println(irValue.value);
        irrecv.resume();
  }
   unsigned long startMillis= millis();  // Start of sample window
   unsigned int peakToPeak = 0;   // peak-to-peak level
 
   unsigned int signalMax = 0;
   unsigned int signalMin = 1024;
 
   // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(0);
      if (sample < 1024)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // save just the min levels
         }
      }
   }
   peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
   double volts = (peakToPeak * 5.0) / 1024;  // convert to volts
 



   //EVERYTHING HERE ON IF FOR THE LEDs

   Serial.println(volts);

   red = redColour(volts);
   green = greenColour(volts);
   blue = blueColour(volts);

   if (red > 255) {
    red = red % 101;
   }
   if (green > 255) {
    green = int(green * 2/3);
   }
   if (blue > 255) {
    blue = 255;
   }

   for(int i = 0; i < NUM_LEDS; i++) {
    leds = CRGB(red, green, blue);
   
    if (i > 20) {
      leds[i - 20] = CRGB(green, blue, red);
    }
    if (i > 100) {
      leds[i - 100] = CRGB(blue, red, green);
    }
    if (i > 200) {
      leds[i - 200] = CRGB(red, blue, green);
    }
    if (i > 250) {
      leds[i - 250] = CRGB(green, red, blue);
    }
    FastLED.setBrightness(brightness);
    FastLED.show();
   }
}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

I hope that helps

slipstick

Unfortunately you've removed so much that the code doesn't compile. But it doesn't seem to do anything with the values from the IR anyway. Also you haven't said what errors or differences you are seeing between the first version and the combined version.

My guess is that there's some incompatibility between the different libraries, maybe because FastLED has it's own math library. But it's only a guess, I've never used that particular combination.

Steve

ribhav99

I'm sorry i'll just put all of my code at the end of this post.
The differences bw the versions is that in the first version, each button gave a specific integer output whereas in the second one, the output for a given button is not consistent and multiple buttons give the same values.

Heres the code(simiplied a few piecewise functions to just return an integer for simplicity although the functions did use math functions like sin, tan and pow.)




#include <FastLED.h>
#include <math.h>
#include <IRremote.h>

#define LED_PIN  12
#define NUM_LEDS  300

const int IR_INPUT = 7;
IRrecv irrecv(IR_INPUT);
decode_results irValue;
CRGB leds[NUM_LEDS];

const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
int ledPin = 12;
int red = 0;
int green = 0;
int blue = 0;
int brightness = 5;

int randNum(int minimum, int maximum) {
  int x = rand() % minimum + maximum;
  return x;
}

int redColour(double n) {
  return 244;

int greenColour(double n) {
  return 200;


int blueColour(double n) {
  return 125;


void setup()
{
   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);
   FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
   irrecv.enableIRIn();
   irrecv.blink13(true);
}
 
 
void loop()
{

  if (irrecv.decode(&irValue)){
        Serial.println(irValue.value);
        irrecv.resume();
  }
   unsigned long startMillis= millis();  // Start of sample window
   unsigned int peakToPeak = 0;   // peak-to-peak level
 
   unsigned int signalMax = 0;
   unsigned int signalMin = 1024;
 
   // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(0);
      if (sample < 1024)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // save just the min levels
         }
      }
   }
   peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
   double volts = (peakToPeak * 5.0) / 1024;  // convert to volts
 
   Serial.println(volts);

   red = redColour(volts);
   green = greenColour(volts);
   blue = blueColour(volts);

   if (red > 255) {
    red = red % 101;
   }
   if (green > 255) {
    green = int(green * 2/3);
   }
   if (blue > 255) {
    blue = 255;
   }

   for(int i = 0; i < NUM_LEDS; i++) {
    leds = CRGB(red, green, blue);
   
    if (i > 20) {
      leds[i - 20] = CRGB(green, blue, red);
    }
    if (i > 100) {
      leds[i - 100] = CRGB(blue, red, green);
    }
    if (i > 200) {
      leds[i - 200] = CRGB(red, blue, green);
    }
    if (i > 250) {
      leds[i - 250] = CRGB(green, red, blue);
    }
    FastLED.setBrightness(brightness);
    FastLED.show();
   }
}

slipstick

Looks like a known incompatibility between IRRemote and FastLED with WS2811/12 type LEDs. See https://github.com/FastLED/FastLED/issues/198

I couldn't see any fix for it but it might be worth asking on the Google + FastLED comunity

Steve




Idahowalker

Could you please please read https://forum.arduino.cc/index.php?topic=149021.0?

You might learn about code tags and most importantly, 'we' can determine you can follow the instructions of our help.

ribhav99

Hey steve thanks a lot for that!
Do you know if the apa102 can be programmed in exactly the same way? Like could I use the same code?

Go Up