hi guys, this is my 2nd time coding and have no programming background. I am currently working with ir as a form of data communication, currently my code is working fine till on the 3rd sound input my ir seem to stop receiving (or maybe decoding). my irRecieve loop is running but it remains outside the " if (irrecv.decode(&results)) ", it only happens after the void fadeLed runs its full cycle twice.
please help me, your help is greatly appreciated.
THANK YOU in advance.
/*
Ripples
This is a MASTER origin, with sound input and light pulse
origin = 'A'
*/
char origin = 'A'; //Origin, master( 'A' || 'B' || 'C' || 'D' || 'E') || ripples '#'
// -------------------------(irSystem)-------------------//
#include <IRremote.h> //include IR remote library.
boolean irControl = false;
int countSend = 0;
unsigned long ir_currentTime;
unsigned long ir_lastTime;
//unsigned long ir_currentTime_receive;
//unsigned long ir_lastTime_receive;
IRsend irsend;
const int IR_ReceivePin = 11;
IRrecv irrecv(IR_ReceivePin);
decode_results results;
// -------------------------(sound)-------------------//
int soundSensor = A5;
int maxLoop_count;
unsigned long currentTime_sound;
unsigned long lastTime_sound;
int soundInterval = 200;
int soundValue;
int checkRun_sound;
//---------------------------(LED)-------------------//
int maxLed; // max LED value (to me changed by sound input)
#define minLed 10 // lowest LED value (no input, switch to '0' else to '10')
#define max_runTime 1000 // max runTime, code will find the next closest but smaller then max with out a dec
int upLedIncl = 0 ;
int increament = 1 ; // incremental/decrement value for up LED
int reduction_MaxVal = 30; // incremental/decrement of MAX VALUE led till minled
int upLed = 9;
int downLed = 10;
unsigned long currentTime;
unsigned long lastTime;
int loopCheck = LOW;
int upLedLvl = minLed;
int LedLvlMax = 0;
int downLedLvl = minLed;
int loopInterval;
int totalTimeRun;
int checkRun ;
int newSteps;
int steps = 1;
boolean storeLoop = false;
int countLoop = 0;
int reqLoop = 0; // required loop to reach LedLvlMax
boolean soundCheck = false;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // start receiving
}
void loop()
{
irData();
soundData();
if (loopCheck == LOW) startInterval_loopTime();
if (loopCheck == HIGH) newMaxInterval_loopTime();
if ((loopCheck == HIGH)&&(LedLvlMax <= minLed)) standby();
if ((loopCheck == HIGH)&&(maxLed > minLed)) fadeLed();
}
void startInterval_loopTime()
{
if (soundValue >= minLed)
{
maxLed = soundValue;
}
else maxLed = minLed;
LedLvlMax = maxLed;
downLedLvl = maxLed;
steps = LedLvlMax / increament;
reqLoop = LedLvlMax / reduction_MaxVal;
int i;
float f;
f = max_runTime / steps; // to remove 0.00
i = (int)f;
loopInterval = i;
totalTimeRun = loopInterval * steps;
loopCheck = HIGH;
}
void newMaxInterval_loopTime()
{
newSteps = LedLvlMax / increament;
float f = totalTimeRun / newSteps;
loopInterval = (int) f;
if (storeLoop)
reqLoop = LedLvlMax / reduction_MaxVal;
storeLoop = false;
}
void standby()
{
analogWrite(upLed,minLed);
analogWrite(downLed,minLed);
storeLoop = true;
countLoop = 1;
loopCheck = LOW;
// Serial.println("standby");
}
//int delayIR(int time){
// for(int i=0;i < time;i++){
// irrecv.resume();
//
// if(irrecv.decode(&results)){
// return results.value;
// }else{
// delay(1);
// }
// }
//
// return -1;
//}
void irData ()
{
// Serial.println ("irData");
if ((countLoop >= 2) && (countLoop <= reqLoop) && (upLedLvl < (minLed + 5)) && (upLedLvl > (minLed - 5)))irControl = true;
else irControl = false;
if (irControl) irSend();
if (!irControl)
{
irReceive();
// irrecv.resume();
}
}
byte val1;
byte val2;
int cmd;
void irSend()
{
// Serial.println ("irSend");
boolean sendCheck = false;
if ( countSend <= 1 ) sendCheck = true; // send count
else if (countSend >= 1);
sendCheck = false;
if (!sendCheck) countSend = 0;
ir_currentTime = millis();
if (ir_currentTime >= (ir_lastTime + 1000)) // time control per loop
{
val1 = (char)origin; // for Origin, master( 'A' || 'B' || 'C' || 'D' || 'E') || ripples '#'
val2 = LedLvlMax; // for LedLvlMax
Serial.println (countSend);
cmd = (val1 << 8) + val2;
if (countSend == 0)
{
irsend.sendRC5( cmd, 16 );
Serial.println ("SEND");
countSend += 1;
}
int ir_check = ir_currentTime - ir_lastTime;
ir_lastTime = ir_currentTime;
// irPrint();
}
delay(1);
}
void irPrint()
{
Serial.print ("val1 : ");
Serial.println ((char)val1);
Serial.print ("val2 : ");
Serial.println (val2);
Serial.println (cmd,BIN);
Serial.println (countSend);
Serial.println(" ");
}
//int valRead_print_1;
//int valRead_print_2;
//byte valRead_1; // Origin, master( 'A' || 'B' || 'C' || 'D' || 'E') || ripples '#'
//byte valRead_2; // LedLvlMax value
//
void irReceive()
{
// Serial.println ("irReceive");
if (irrecv.decode(&results))
{
irrecv.resume();
// Serial.println ("irReceive");
byte valRead_1 = results.value >> 8; // Origin, master( 'A' || 'B' || 'C' || 'D' || 'E') || ripples '#'
byte valRead_2 = results.value & 255; // LedLvlMax value
// if (soundValue >= LedLvlMax)
// {
// countLoop = 1;
// loopCheck = LOW;
// }
Serial.println (results.value,BIN);
Serial.print("val_reading 1 :");
char myChar = (char)valRead_1; // conversion of byte to char(charater)
Serial.println (myChar); // result of val2, origin is in DEC
Serial.print("va1_reading 2 :");
Serial.println (valRead_2,DEC);
}
// valRead_print_1 = valRead_1;
// valRead_print_2 = valRead_2;
}
void fadeLed()
{
currentTime = millis();
if(currentTime >= (lastTime + loopInterval))
{
Serial.println(upLedLvl);
analogWrite(upLed, upLedLvl );
analogWrite(downLed, downLedLvl);
upLedLvl += upLedIncl;
downLedLvl= ( LedLvlMax + minLed ) - upLedLvl;
if (upLedLvl >= LedLvlMax){
upLedIncl = -increament;
}
if (upLedLvl <= minLed){
upLedIncl = increament;
LedLvlMax -= reduction_MaxVal;
countLoop += 1;
if (LedLvlMax <= minLed) LedLvlMax = minLed;
}
checkRun = currentTime - lastTime;
lastTime = currentTime;
printStatus();
}
irrecv.resume();
delay(1);
}
void soundData()
{
currentTime_sound = millis();
if ( currentTime_sound >= (lastTime_sound + soundInterval))
{
soundValue = analogRead(soundSensor);
soundValue = map(soundValue,0,1023,0,1000);
if (soundValue >= 255) soundValue = 255; // Max cap at 255, digital out max val(255)
checkRun_sound = currentTime_sound - lastTime_sound;
lastTime_sound = millis();
if (soundValue >= LedLvlMax)
{
countLoop = 1;
loopCheck = LOW;
}
// printSound();
}
delay(1);
}
void printSound ()
{
Serial.print ("sound map : ");
Serial.println (soundValue);
Serial.print ("soundInterval vs runTime : ");
Serial.print ( soundInterval );
Serial.print (" vs ");
Serial.println ( checkRun_sound );
Serial.println ( " " );
}
void printStatus()
{
Serial.print ("max Val : ");
Serial.println (LedLvlMax);
Serial.print ("reqLoop / countLoop : ");
Serial.print ( reqLoop);
Serial.print ("/ ");
Serial.println (countLoop);
Serial.print ("up / down : ");
Serial.print (upLedLvl);
Serial.print ("/ ");
Serial.println (downLedLvl);
Serial.print ("loopInterval / runTime: ");
Serial.print (loopInterval);
Serial.print (" / ");
Serial.println (checkRun);
// Serial.print("va1_reading 2 (int) :");
// Serial.println (valRead_print_2,DEC);
//
Serial.println (" ");
}