Monday, May 21, 2018

color sensor show LCD +Arduino mega 2560 r3

 ตัวอย่างโปรแกรม color sensor


//show RGB
int ledPin = 9;      // PWM connected to digital pin 9
int valum = 1;

float time1 = 0.0;
float time2 = 0.0;
float s=0.0;
float counter = 0.0;
float j = 0.0;
float k=0.0;
float rpm=0.0;
float rpm_set = 0.0;
unsigned long Cur_time=0;
unsigned long Prev_time=0;

int latter1 = 0;

void setup() {

  Serial.begin(9600);
  attachInterrupt(0,Millis,RISING);
  //LOW to trigger the interrupt whenever the pin is low,
  //CHANGE to trigger the interrupt whenever the pin changes value
  //RISING to trigger when the pin goes from low to high,
  //FALLING for when the pin goes from high to low.
  pinMode(ledPin,OUTPUT); //PWM
  digitalWrite(7,HIGH);
  digitalWrite(8,LOW);

}

void loop()
{
  String latter ="";
  if(Serial.available()>0)
  {
    while(Serial.available()>0)
    {
  latter += char(Serial.read());
    }
 
  Serial.println(latter);
  latter1=latter.toInt();
  }
  analogWrite(ledPin,latter1);
  delay(200);
  Cur_time = millis();
  int s = Cur_time-Prev_time;
  if(s >= 100)
      {
  j = (counter*10.0)/360.0;
  k = j/(s*0.001);
  rpm = k*60.0;    //rpm
  Serial.print("rpm:"); Serial.print(rpm); Serial.println("\t");

   rpm_set = analogWrite(latter1);
   err = rpm_set - rpm;
 
   counter=0;
   Prev_time = Cur_time;
   Serial.print("latter:"); Serial.print(latter); Serial.println("\t");
       }
}


void Millis()
{
  counter++;
}



No comments:

Post a Comment