Circuit diagram:
Arduino Code:
//---Tempreature Sensor---
#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
OneWire ds(DS18S20_Pin); // on digital pin 2
//---cmps---
#include "Wire.h"
#include "CMPS03.h"
CMPS03 cmps03;
volatile float cmpsValue; //compass reading
volatile float cmpsTurn; //the turning degree
//----infra---
const int InfraredSensorPin = 8;
volatile int countTurns = 0;
//---Motors---
int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
//---LED---
int rightLed= 13;
int leftLed= 12;
int infraLed=11;
void setup() {
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);
pinMode(rightLed,OUTPUT);
pinMode(leftLed,OUTPUT);
pinMode(infraLed,OUTPUT);
Serial.begin(9600);
pinMode(InfraredSensorPin,INPUT);
Wire.begin();
volatile char c;
volatile int a = 0;
while(Serial.available()){
c = Serial.read();
}
while(a !=1){
stopBoat();
c = Serial.read();
if(c == 'r'){
a=1;
}
if(c=='l'){
a=1;
}
}
if(c=='r'){
while(digitalRead(InfraredSensorPin) == HIGH){
forward();
}
Serial.println("wall");
digitalWrite(infraLed,HIGH);
reverseRight();
turnRight();
digitalWrite(infraLed,LOW);
digitalWrite(rightLed,LOW);
shortForward();
turnRight();
digitalWrite(rightLed,LOW);
countTurns++;
forward();
}
if(c=='l'){
while(digitalRead(InfraredSensorPin) == HIGH){
forward();
}
Serial.println("wall");
digitalWrite(infraLed,HIGH);
reverseLeft();
turnLeft();
digitalWrite(infraLed,LOW);
digitalWrite(leftLed,LOW);
shortForward();
turnLeft();
digitalWrite(leftLed,LOW);
forward();
}
}
void loop() {
volatile char c; // incoming data from phone
int toggle =0; // Keep track of whether its in auto or manual
while(Serial.available()){
c = Serial.read();
}
if(c == 'm'){
toggle =1;
while( toggle == 1){
switch(c){
case'a':manForward();
c= Serial.read();
break;
case'c':manRight();
c= Serial.read();
break;
case'd':manLeft();
c= Serial.read();
break;
case'u':toggle = 0;
//Serial.println("Now going to automatic mode");
break;
default: stopBoat();
c= Serial.read();
break;
}
}
}
else{
c = Serial.read();
if(c == 'm'){
toggle = 1;
}
/*if(digitalRead(InfraredSensorPin) == HIGH){
forward();
}
else{
if(countTurns%2 == 0){
reverseLeft();
digitalWrite(infraLed,HIGH);
turnRight();
digitalWrite(rightLed,LOW);
digitalWrite(infraLed,LOW);
shortForward();
turnRight();
digitalWrite(rightLed,LOW);
forward();
}
else{
reverseRight();
digitalWrite(infraLed,HIGH);
turnLeft();
digitalWrite(leftLed,LOW);
digitalWrite(infraLed,LOW);
shortForward();
turnLeft();
digitalWrite(leftLed,LOW);
forward();
}
}*/
}
}
void forward(){
volatile char c;
int stopcount = 0;
c = Serial.read();
if(c == 'r'){
stopcount = 1;
}
while(stopcount==1){
stopBoat();
c = Serial.read();
if(c=='l'){
stopcount=0;
}
}
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 88);
analogWrite(E2, 120);
digitalWrite(leftLed,LOW);
digitalWrite(rightLed,LOW);
/*float temperature = getTemp();
Serial.print("The Water is ");
Serial.print(temperature);
Serial.println("°C");*/
Serial.println("forward");
delay(1000);
}
void shortForward(){
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 88);
analogWrite(E2, 120);
float temperature = getTemp();
/*Serial.print("The Water is ");
Serial.print(temperature);
Serial.println("°C");*/
Serial.println("shortforward");
delay(5000);
}
void turnRight() {
for(int turnTimer=0; turnTimer <6; turnTimer++){
digitalWrite(rightLed,HIGH);
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
analogWrite(E1, 88);
analogWrite(E2, 120);
/*float temperature = getTemp();
Serial.print("The Water is ");
Serial.print(temperature);
Serial.println("°C");*/
Serial.println("right");
delay(500);
}
countTurns++;
}
void turnLeft() {
for(int turnTimer=0;turnTimer<6;turnTimer++){
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, 88);
analogWrite(E2, 120);
digitalWrite(leftLed,HIGH);
/*float temperature = getTemp();
Serial.print("The Water is ");
Serial.print(temperature);
Serial.println("°C");*/
Serial.println("Left");
delay(500);
}
countTurns++;
}
void reverseLeft(){
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, 150);
digitalWrite(E2, LOW);
Serial.println("reverse left");
delay(10000);
}
void reverseRight(){
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
digitalWrite(E1, LOW);
analogWrite(E2, 150);
Serial.println("reverse right");
delay(10000);
}
void manForward(){
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 88);
analogWrite(E2, 110);
float temperature = getTemp();
Serial.print("The Water is ");
Serial.print(temperature);
Serial.println("°C");
delay(3000);
}
void manRight(){
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
analogWrite(E1, 120);
analogWrite(E2, 80);
digitalWrite(rightLed,HIGH);
float temperature = getTemp();
Serial.print("The Water is ");
Serial.print(temperature);
Serial.println("°C");
delay(3000);
digitalWrite(rightLed,LOW);
}
void manLeft(){
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, 120);
analogWrite(E2, 87);
digitalWrite(leftLed,HIGH);
float temperature = getTemp();
Serial.print("The Water is ");
Serial.print(temperature);
Serial.println("°C");
delay(3000);
digitalWrite(leftLed,LOW);
}
void stopBoat(){
digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
digitalWrite(E1, LOW);
digitalWrite(E2, LOW);
float temperature = getTemp();
Serial.print("The Water is ");
Serial.print(temperature);
Serial.println("°C");
delay(1000);
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}