#include<Servo.h>
//Declare Servos
Servo steerservo; //steering servo
Servo throttleservo; //drive servo
const int turntime=0; //number of milliseconds to hold turn when turning
const int turnaroundtime=0; // number of milliseconds to turn completely around
const int steerservopin=22; //pin number for steering servo
const int throttleservopin=24; //pin number for throttle servo
const int frontdistlimit=60;// starts to turn
const int sidedistlimit=35;// starts to turn
const int backupdistlimit=40; //backs up when this close to the front sensor
const int sidebackupdistlimit=20; //backs up when this close to a side sensor
#include <NewPing.h>
#define SONAR_NUM 3 // Number or sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0; // Keeps track of which sensor is active.
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(40, 41, MAX_DISTANCE), // Centre sensor (0) Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(42, 43, MAX_DISTANCE),// Left sensor (1)
NewPing(44, 45, MAX_DISTANCE), //Right sensor (2)
//NewPing(21, 22, MAX_DISTANCE),
//NewPing(23, 24, MAX_DISTANCE),
//NewPing(25, 26, MAX_DISTANCE),
//NewPing(27, 28, MAX_DISTANCE),
//NewPing(29, 30, MAX_DISTANCE),
//NewPing(31, 32, MAX_DISTANCE),
//NewPing(34, 33, MAX_DISTANCE),
//NewPing(35, 36, MAX_DISTANCE),
//NewPing(37, 38, MAX_DISTANCE),
//NewPing(39, 40, MAX_DISTANCE),
//NewPing(50, 51, MAX_DISTANCE),
//NewPing(52, 53, MAX_DISTANCE)
};
//Setup function. Runs once when Arduino starts or is reset
//webserver components of initialization
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x08, 0x57, 0x00, 0x41, 0x3d, 0x55 };
IPAddress ip(192,168,0,155);
EthernetServer server(80);
// end of webserver init
void setup() {
Serial.begin(9600);
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
steerservo.attach(steerservopin); // attaches the steering servo to its pin 12
throttleservo.attach(throttleservopin); // attaches the throttle servo to its pin 13
delay(1000); // delay one second
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop(){
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
if (millis() >= pingTimer[i]) { // Is it this sensor's time to ping?
pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
currentSensor = i; // Sensor being accessed.
cm[currentSensor] = 1000; // Make distance zero in case there's no ping echo for this sensor.
sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
}
}
communicate();
// the rest of your code would go here
}
void echoCheck() { // If ping received, set the sensor distance to array.
if (sonar[currentSensor].check_timer())
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}
void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
for (uint8_t i = 0; i < SONAR_NUM; i++) {
Serial.print(i);
Serial.print("=");
Serial.print(cm[i]);
Serial.print("cm ");
Serial.print("Front centre distance is ");
Serial.print(cm[0]);
Serial.println(" cm");
Serial.print("Left sensor distance is ");
Serial.print(cm[1]);
Serial.println(" cm");
Serial.print("Right sensor distance is ");
Serial.print(cm[2]);
Serial.println(" cm");
}
Serial.println();
if(cm[0]<frontdistlimit || cm[1]<sidedistlimit || cm[2]<sidedistlimit) {
char turndirection=scan(); // Goes to scan function to decide which way to turn based on l,r,s values.
switch (turndirection){
case 'l':
turnleft(turntime);
//delay(turntime);
break; // exits the case
case 'r':
turnright(turntime);
//delay(turntime);
break;
case 'b':
backup();
break;
case 's':
spin();
break;
}
}
else
{
go();
}
}
//Driving the servo motors
void go(){
steerservo.write(90); //for now, we don't go anywhere, thus value is 90 for both which is stopped
throttleservo.write(110);
Serial.println("Going forward ");
}
void turnleft(int t){
steerservo.write(120);
throttleservo.write(100);
delay(turntime);
Serial.println("Turning Left ");
}
void turnright(int t){
steerservo.write(60);
throttleservo.write(100);
delay(turntime);
Serial.println("Turning Right ");
}
void backup(){
steerservo.write(150); // 90 is stop
throttleservo.write(80);
Serial.println("Reversed ");
}
void spin(){
steerservo.write(130);
throttleservo.write(0);
}
char scan(){
char choice;
if (cm[0]<backupdistlimit && cm[1]>sidebackupdistlimit){
choice='s';
}
else if (cm[0]<backupdistlimit && cm[2]>sidebackupdistlimit){
choice='s';
}
else if (cm[0]<backupdistlimit || cm[1]<sidebackupdistlimit || cm[2]<sidebackupdistlimit){
choice='b';
}
else if (cm[1]>cm[2]){
choice='l';
}
else if (cm[2]>cm[1]){
choice='r';
}
else{
choice='b';
}
Serial.print("Choice: ");
Serial.println(choice);
return choice;
}
void communicate(){
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 1"); // refresh the page automatically every 1 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Here is a message from Tim's Arduino Mega Rover");
client.println("
");
// output some ping data
for (uint8_t i = 0; i < SONAR_NUM; i++) {
client.println(i);
client.println("=");
client.println(cm[i]);
client.println("cm ");
client.println("Front centre distance is ");
client.println(cm[0]);
client.println(" cm");
client.println("Left sensor distance is ");
client.println(cm[1]);
client.println(" cm");
client.println("Right sensor distance is ");
client.println(cm[2]);
client.println(" cm");
// end of ping function
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
//delay(1);// edited out delay
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
// */
}