ArduinoGeneral

test motor speed with Arduino and IR switch sensor

userHead Leff 2015-07-16 02:37:48 1529 Views0 Replies


Code: Select allunsigned int Speed = 0;
int IRsensor = 2;
void setup()
{
  Serial.begin(9600);
  Serial.println("Start!");  
  pinMode(IRsensor,INPUT);

}

void loop(){ 
  static unsigned long timepoint = millis();
  static unsigned long timepoint1 = millis();
  static byte count = 0;

  if(millis()-timepoint > 2)
  {
    timepoint = millis();
    if(digitalRead(IRsensor)) count = 0;
    if( (count<30) && (++count==10) )
      Speed++;
  }

  if(millis()-timepoint1 > 10000){
    timepoint1 = millis();
    Speed = Speed * 6;
    Serial.print("Speed is: ");
    Serial.println(Speed);
    Speed = 0;
  }
}