|
想請問如何使用紅外線感測計數輪子轉動的圈數程式如下- const int PIRSensor = 2;
- int val = 0;
- void setup()
- {
- Serial.begin(9600);
- pinMode(PIRSensor,INPUT);
- pinMode(13,OUTPUT);
- }
- void loop(){
-
- int i;
- i = digitalRead(PIRSensor);
- if(i == LOW){
- digitalWrite(13,HIGH);
- val += val + 1;
-
- }
-
-
- Serial.print(val);
- delay(1000);
- }
複製代碼
這是我計數的程式可是我要如何加到我的自動車程式中,我想再直線前進的時候用紅外感測來控制輪子轉的圈數達到我要的,已圈數控制直線距離。下面是車子的程式
- #include <HMC5883L.h>
- #include <Wire.h>
- HMC5883L compass;
- void Angle()
- {
- MagnetometerRaw raw = compass.ReadRawAxis();
- MagnetometerScaled scaled = compass.ReadScaledAxis();
- float yHeading = atan2(scaled.ZAxis, scaled.XAxis);
- if(yHeading < 0) yHeading += 2*PI;
- if(yHeading > 2*PI) yHeading -= 2*PI;
- int yDegrees = yHeading * 180/M_PI;
-
- Serial.print(yDegrees);
- Serial.print(",");
- delay(500);
- }
- void setup()
- {
- Serial.begin(9600);
- Wire.begin();
- compass = HMC5883L();
- compass.SetScale(1.3);
- compass.SetMeasurementMode(Measurement_Continuous);
- pinMode(9, OUTPUT);
- pinMode(DAC0, OUTPUT);
- pinMode(11, OUTPUT);
- pinMode(DAC1, OUTPUT);
- }
- void loop()
- {
- int i;
- do{
- digitalWrite(9, LOW);
- analogWrite(DAC0, 35);
- digitalWrite(11, LOW);
- analogWrite(DAC1, 90);
- MagnetometerRaw raw = compass.ReadRawAxis();
- MagnetometerScaled scaled = compass.ReadScaledAxis();
- float yHeading = atan2(scaled.ZAxis, scaled.XAxis);
- if(yHeading < 0) yHeading += 2*PI;
- if(yHeading > 2*PI) yHeading -= 2*PI;
- int yDegrees = yHeading * 180/M_PI;
- Serial.print(yDegrees);
- Serial.print(",");
- i=yDegrees;
- }
- while((i > 29) || (i < 27));
- digitalWrite(9, LOW);
- analogWrite(DAC0, 35);
- digitalWrite(11, LOW);
- analogWrite(DAC1, 35);
- delayMicroseconds(500);
- do{
- digitalWrite(9, LOW);
- analogWrite(DAC0, 110);
- digitalWrite(11, LOW);
- analogWrite(DAC1, 110);
- MagnetometerRaw raw = compass.ReadRawAxis();
- MagnetometerScaled scaled = compass.ReadScaledAxis();
- float yHeading = atan2(scaled.ZAxis, scaled.XAxis);
- if(yHeading < 0) yHeading += 2*PI;
- if(yHeading > 2*PI) yHeading -= 2*PI;
- int yDegrees = yHeading * 180/M_PI;
- Serial.print(yDegrees);
- Serial.print(",");
- i=yDegrees;
- }
- while((i > 29) || (i < 27));
- delayMicroseconds(8000000);
- do{
- digitalWrite(9, LOW);
- analogWrite(DAC0, 35);
- digitalWrite(11,LOW);
- analogWrite(DAC1, 90);
- MagnetometerRaw raw = compass.ReadRawAxis();
- MagnetometerScaled scaled = compass.ReadScaledAxis();
- float yHeading = atan2(scaled.ZAxis, scaled.XAxis);
- if(yHeading < 0) yHeading += 2*PI;
- if(yHeading > 2*PI) yHeading -= 2*PI;
- int yDegrees = yHeading * 180/M_PI;
- Serial.print(yDegrees);
- Serial.print(",");
- i=yDegrees;
- }
- while((i > 278) || (i < 276));
- digitalWrite(9, LOW);
- analogWrite(DAC0, 35);
- digitalWrite(11, LOW);
- analogWrite(DAC1, 35);
- delayMicroseconds(500);
- do{
- digitalWrite(9, LOW);
- analogWrite(DAC0, 110);
- digitalWrite(11, LOW);
- analogWrite(DAC1, 110);
- MagnetometerRaw raw = compass.ReadRawAxis();
- MagnetometerScaled scaled = compass.ReadScaledAxis();
- float yHeading = atan2(scaled.ZAxis, scaled.XAxis);
- if(yHeading < 0) yHeading += 2*PI;
- if(yHeading > 2*PI) yHeading -= 2*PI;
- int yDegrees = yHeading * 180/M_PI;
- Serial.print(yDegrees);
- Serial.print(",");
- i=yDegrees;
- }
- while((i > 278) || (i < 276));
- delayMicroseconds(8000000);
- }
複製代碼 |
|