Below are two programs, one for a rover, the other a processing sketch. The rover takes in a int from serial to control movement and the horn, and the processing sketch sends the commands, but also receives serial data from sensors coming in from the rover.
The problem is that after about 30 seconds, the transmission from the rover stops. The numbers no longer change in the processing sketch, and in the serial monitor you have to send a command to it to get the values back (which I'm not %100 sure why) so by repeatedly sending the value 4 (the stop command) I found the numbers that came back eventually stop changing as well.
What is the world could be causing this? Because as best I can tell, it's just stops sending data
Thank-you.
P.S. I added this to my code:
if (sonicData < 4)
{
tone(tonePin, 1000);
}
and it would seem that it only works when the processing sketch is open, or it's getting a command from the serial monitor. I encountered this before when I was trying to get my code to not have a huge delay in between pressing w and the rover actually moving. Is my code only working when it's receiving from serial?
//////////////////////////////////////////////////////////////////////////
Arduino Sketch:
const int S1 = 6; //D1 Speed Control
const int S2 = 5; //D2 Speed Control
const int D1 = 8; //D1 Direction Control
const int D2 = 7; //D2 Direction Control
const int rightEncoderPin = A0; // (If you and the rover are pointing the same direction)
const int leftEncoderPin = A1;
const int tonePin = 4;
const int leftLightPin = A3;
const int rightLightPin = A2;
const int leftTempPin = A4;
const int rightTempPin = A5;
const int rightEmfPin = A7;
const int leftEmfPin = A6;
const int trigPin = 3;
const int echoPin = 2;
int direct = 0;
int leftspeed = 255;
int rightspeed = 255;
int leftLight = 0;
int rightLight = 0;
int leftTemp = 0;
int rightTemp = 0;
int leftEmf = 0;
int rightEmf = 0;
int sonicData = 0;
String string = "";
int lastSample = 0;
void setup()
{
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(rightEncoderPin, INPUT);
pinMode(leftEncoderPin, INPUT);
pinMode(rightLightPin, INPUT);
pinMode(leftLightPin, INPUT);
pinMode(rightTempPin, INPUT);
pinMode(leftTempPin, INPUT);
pinMode(rightEmfPin, INPUT);
pinMode(leftEmfPin, INPUT);
pinMode(tonePin, INPUT);
Serial.begin(9600);
}
void loop()
{
while( Serial.available() == 0);
direct = Serial.read() -'0';
string = String(leftLight) + ", " + String(rightLight) + ", " + String(leftTemp) + ", " + String(rightTemp) + ", " + String(leftEmf) + ", " + String(rightEmf) + ", " + String(sonicData);
if (millis() >= lastSample + 50) // We can't be overloading our computers serial buffer now can we?
{
Serial.println(string);
lastSample = millis();
}
//Serial.println(direct);
sonic();
datas();
movement();
}
void datas()
{
leftLight = analogRead(leftLightPin);
leftTemp = analogRead(leftTempPin);
leftEmf = analogRead(leftEmfPin);
rightLight = analogRead(rightLightPin);
rightTemp = analogRead(rightTempPin);
rightEmf = analogRead(rightEmfPin);
}
void sonic()
{
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
sonicData = distance;
}
void movement()
{
switch(direct)
{
case 0://Move Forward
forward (leftspeed,rightspeed);
break;
case 1://Move Backwards
reverse (leftspeed,rightspeed);
break;
case 2://Turn Left
left (leftspeed,rightspeed);
break;
case 3://Turn Right
right (leftspeed,rightspeed);
break;
case 4:
stop();
noTone(tonePin);
break;
case 5:
tone(tonePin, 500);
break;
default:
stop();
break;
}
}
void stop(void) //Stop
{
digitalWrite(S1,LOW);
digitalWrite(S2,LOW);
}
void forward(char a,char b)
{
analogWrite (S1,a);
digitalWrite(D1,LOW);
analogWrite (S2,b);
digitalWrite(D2,LOW);
}
void reverse (char a,char b)
{
analogWrite (S1,a);
digitalWrite(D1,HIGH);
analogWrite (S2,b);
digitalWrite(D2,HIGH);
}
void left (char a,char b)
{
analogWrite (S1,a);
digitalWrite(D1,HIGH);
analogWrite (S2,b);
digitalWrite(D2,LOW);
}
void right (char a,char b)
{
analogWrite (S1,a);
digitalWrite(D1,LOW);
analogWrite (S2,b);
digitalWrite(D2,HIGH);
}
Processing Sketch:
import processing.serial.*;
Serial port;
String data = "";
void setup()
{
size(800, 165);
smooth();
background(0);
fill(255, 50, 0);
textAlign(CENTER, CENTER);
port = new Serial(this, "COM5", 9600);
port.bufferUntil('\n');
}
void draw()
{
background(0);
textSize(75);
text("PROGRAM RUNNING", width/2, 59);
textSize(15);
text("Use WASD keys to navigate RoboRat-V1", width/2, 104);
text("Incoming Serial Data: " + data, width/2, 140);
if (keyPressed)
{
if (key == 'w') {port.write('0');};
if (key == 's') {port.write('1');};
if (key == 'a') {port.write('2');};
if (key == 'd') {port.write('3');};
if (key == 'h') {port.write('5');};
}
else
{
port.write('4');
}
}
void serialEvent (Serial port)
{
data = port.readStringUntil('\n');
}