I have been working on this on and off for over 3 hours now and it is only my stubbornness and the fact I don’t want to make the same error again that has stopped me from just rewriting it all over again. my project is going to be a basic S,L,A,M but right now it is a mapping robot.
When I run my code it returns a distance of 0. I had never worked with this type of range sensor before, so I loaded a sample sketch to test that I had it hooked up right and that ran no problem. However my code still will not work. I even coped and pasted the part of the sample sketch that runs the sensor and it still did not work. I am at my wits end with it and need a new set of eyes to look over it or I’m just going to rewrite it all.
edit: my sensor is a HC-sr04 sonic range finder.
my sketch:
// SLAM
// Levi
// code on: 6/14/16
#include <Servo.h> // import servo library
#include <Ethernet.h>
// pin setup
#define servoPin 6
#define trigPin 9
#define echoPin 10
#define error 4
// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//the IP address for the shield:
byte ip[] = { 192, 168, 1, 20 };
byte server[] = {192,168,1,3};
EthernetClient client;
// servo setup
Servo servo1;
//setup vars
int theta = 0;
int stepSize = 3; // degreas between meserments
void setup() {
// set pin mode's
pinMode(servoPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//debug
Serial.begin(9600);
Ethernet.begin(mac, ip);
delay(1000);
client.connect(server, 4444);
delay(5000);
client.println("SLAM");
delay(5000);
// setup the servo
servo1.attach(servoPin);
servo1.write(90);
}
void loop() {
servo1.write(theta);
delay(500);
int range = dist();
client.println(String(range) + "," + String(theta));
theta += stepSize;
if (theta > 180) theta = 0;
}
long dist() {
long dist1, dist2;
while (true) {
dist1 = getRange();
delay(200);
dist2 = getRange();
if (abs(dist1 - dist2) < error) {
return (dist1 + dist2) / 2;
}
}
}
long getRange() {
long duration;
digitalWrite(trigPin, LOW);
delay(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
int distance = duration*0.034/2;
// I put a Serial.println(duration); here and it still only returned 0 both to the server and the serial monitor
return distance;
}
sample sketch:
/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* Crated by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}