Interfacing of Colour Sensor using an IR sensor

Dear all,

I am doing a project on interfacing a color sensor to the arduino and the output should only display once when the IR sensor is tripped.

I have written the whole code and its working perfectly fine except for a small hitch which i cannot figure out and that is the if else statement even after satisfying condition of one output it most of time gives random outputs and the last else statement just keep on repeating itself till a red,green or blue color is found.

I would be grateful if anyone could help me

Thank You
Arjun Warty

LCD_IR_COLOR_INTERFACING.ino (2.78 KB)

/*#include <Wire.h>  
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
*/

Why should we have to wade past commented out code to get to the real code?

void setup()
{
Serial.begin(115200);
pinMode(s0,OUTPUT);
pinMode(s1,OUTPUT);
pinMode(s2,OUTPUT);
pinMode(s3,OUTPUT);
pinMode(IR,INPUT);
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
attachInterrupt(0,IRInterrupt,RISING); 
/*lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("    ARRANGEM    ");
lcd.setCursor(0,1);
lcd.print("      SBMP      ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("  Ashka Ashani  ");
lcd.setCursor(0,1);
lcd.print(" Deesha Nirmal  ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("  Shaily Sheth  ");
lcd.setCursor(0,1);
lcd.print("                ");
delay(2000);
lcd.setCursor(0,0);
lcd.print(" R  G  B  O  T  ");
lcd.setCursor(0,1);
lcd.print(" XX XX XX XX XX ");
Serial.println(" R  G  B  O  T  ");
Serial.println(" XX XX XX XX XX ");
*/
}

If you can’t bother properly indenting your code, or using Tools + Auto Format before posting it, why should we bother reading it?

ISR(TIMER2_OVF_vect)//the timer 2, 10ms interrupt overflow again.
//Internal overflow interrupt executive function
{
TCNT2=0;
flag++;
if(flag==1)
{
countR=counter;
Serial.print("red=");
Serial.println(countR,DEC);
digitalWrite(s2,HIGH);
digitalWrite(s3,HIGH);
}

Serial output relies on interrupts. Interrupts don’t happen while on interrupt is being processed. You can NOT use Serial.print() or Serial.println() in an ISR.

for(int i=0;i<1000;i++)
{
  for(int i=0;i<1000;i++)
  TCNT2 = 0;
}

The compiler is smarter than you. TCNT2 will be set to 0 ONCE! Using the same name for inner and outer loop indexes is not indicative of being the sharpest crayon in the box.

Dear all,

I am extremely sorry for the inconvenience caused, hope you will understand. This is my first time on arduino and the first forum post ever so I did not have a clue of how to go about things. Next time I will try to be neat and to the point with my work.

Thanking You
Best Regard
Arjun Warty