Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
robit:sweep_n_scan [2021/02/25 13:00] john removed |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <file c sweep_n_scan.ino> | ||
- | #include < | ||
- | const int TRIG_PIN = 9; | ||
- | const int ECHO_PIN = 10; | ||
- | |||
- | float inches; | ||
- | |||
- | Servo myservo; | ||
- | |||
- | int pos = 60; | ||
- | |||
- | void setup() { | ||
- | myservo.attach(13); | ||
- | Serial.begin(9600); | ||
- | pinMode(TRIG_PIN, | ||
- | digitalWrite(TRIG_PIN, | ||
- | } | ||
- | |||
- | void loop() { | ||
- | for (pos = 30; pos <= 180; pos += 15) { | ||
- | myservo.write(pos); | ||
- | FindRange(); | ||
- | Serial.print(" | ||
- | Serial.print(myservo.read()); | ||
- | Serial.print(" | ||
- | Serial.println(inches); | ||
- | delay(1000); | ||
- | } | ||
- | for (pos = 180; pos >= 30; pos -= 15) { | ||
- | myservo.write(pos); | ||
- | FindRange(); | ||
- | Serial.print(" | ||
- | Serial.print(myservo.read()); | ||
- | Serial.print(" | ||
- | Serial.println(inches); | ||
- | delay(1000); | ||
- | } | ||
- | } | ||
- | |||
- | float FindRange () { | ||
- | unsigned long t1; | ||
- | unsigned long t2; | ||
- | unsigned long pulse_width; | ||
- | float cm; | ||
- | float inches; | ||
- | | ||
- | // start finding the distance | ||
- | digitalWrite(TRIG_PIN, | ||
- | delayMicroseconds(10); | ||
- | digitalWrite(TRIG_PIN, | ||
- | |||
- | while ( digitalRead(ECHO_PIN) == 0 ); | ||
- | |||
- | t1 = micros(); | ||
- | while ( digitalRead(ECHO_PIN) == 1); | ||
- | t2 = micros(); | ||
- | pulse_width = t2 - t1; | ||
- | inches = pulse_width / 148.0; | ||
- | return inches; | ||
- | } | ||
- | </ |