Use LCD Keyboard Shieid to show color sensor's data which received [1.2]

来源:互联网 发布:joo yeon seo韩国 编辑:程序博客网 时间:2024/05/18 11:45

The code can be use ,but there's something wrong with it.

it only shows a few sets of data,and then it stops.

#include <LiquidCrystal.h>LiquidCrystal lcd(8,9,4,5,6,7);int s0=6,s1=5,s2=4,s3=3;//int s0=3,s1=4,s2=5,s3=6;int out=2;int flag=0;byte counter=0;int colorshow[3];double timebegin;byte countR=0,countG=0,countB=0;void setup() { Serial.begin(9600); pinMode(10,OUTPUT); lcd.begin(16,2); digitalWrite(10,HIGH); pinMode(s0,OUTPUT); pinMode(s1,OUTPUT);  pinMode(s2,OUTPUT); pinMode(s3,OUTPUT); }void TCS() { flag=0;   digitalWrite(s1,HIGH); digitalWrite(s0,HIGH); digitalWrite(s2,LOW); digitalWrite(s3,LOW); attachInterrupt(0, ISR_INTO, LOW); timer0_init(); }void ISR_INTO() { counter++; } void timer0_init(void) {  TCCR2A=0x00;  TCCR2B=0x07;   //the clock frequency source 1024 points  TCNT2= 100;    //10 ms overflow again  TIMSK2 = 0x01; //allow interrupt } int i=0; ISR(TIMER2_OVF_vect)//the timer 2, 10ms interrupt overflow again. Internal overflow interrupt executive function{    TCNT2=100;    flag++; if(flag==1)  {    countR=counter;    colorshow[0]=counter;    Serial.print("red=");    Serial.println(countR,DEC);    lcd.setCursor(0,0);    lcd.print("R=");    lcd.setCursor(2,0);    lcd.print("  ");    lcd.setCursor(2,0);    lcd.print(countR,DEC);    digitalWrite(s2,HIGH);    digitalWrite(s3,HIGH);  }  else if(flag==2)   {    countG=counter;    colorshow[1]=counter;    Serial.print("green=");    Serial.println(countG,DEC);    lcd.setCursor(5,0);    lcd.print("G=");    lcd.setCursor(7,0);    lcd.print("   ");    lcd.setCursor(7,0);    lcd.print(countG,DEC);    digitalWrite(s2,LOW);    digitalWrite(s3,HIGH);   }   else if(flag==3)    {    countB=counter;    colorshow[2]=counter;    Serial.print("blue=");    Serial.println(countB,DEC);    //Serial.println("\n");    lcd.setCursor(10,0);    lcd.print("B=");    lcd.setCursor(12,0);    lcd.print("   ");    lcd.setCursor(12,0);    lcd.print(countB,DEC);     digitalWrite(s2,LOW);    digitalWrite(s3,LOW);       }    else if(flag==4)    {      flag=0;      lcd.setCursor(0,1);      lcd.print("                ");      lcd.setCursor(0,2);      lcd.print("Color:");      lcd.setCursor(7,2);      if((colorshow[0]>220)&&(colorshow[1]>220)&&(colorshow[2]>220))        lcd.print("White");      else if((colorshow[0]<50)&&(colorshow[1]<50)&&(colorshow[2]<50))        lcd.print("Black");      else if((colorshow[1]>colorshow[0])&&(colorshow[1]>colorshow[2]))        lcd.print("Green");      else if((colorshow[2]>colorshow[0])&&(colorshow[2]>colorshow[1]))        lcd.print("Purple");      else if((colorshow[0]>colorshow[1])&&(colorshow[0]>colorshow[2]))        lcd.print("Red");      //lcd.print(colorshow[1]);            // a simple color decision      if((colorshow[0]>220)&&(colorshow[1]>220)&&(colorshow[2]>220))        Serial.println("White");      else if((colorshow[0]<50)&&(colorshow[1]<50)&&(colorshow[2]<50))        Serial.println("Black");      else if((colorshow[1]>colorshow[0])&&(colorshow[1]>colorshow[2]))        Serial.println("Green");      else if((colorshow[2]>colorshow[0])&&(colorshow[2]>colorshow[1]))        Serial.println("Purple");      else if((colorshow[0]>colorshow[1])&&(colorshow[0]>colorshow[2]))        Serial.println("Red");      Serial.println("\n");      }     counter=0;           //delay(10);}void loop() {  TCS();  //delay(1000);  while(1); }


阅读全文
1 0
原创粉丝点击