We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I no longer know if this is a Arduino problem, or a Processing problem, so I will be posting this on both sites.
My problem is here in this video:
As you can see some time in the video after some time the serial transmission or receiving just slows down and gets a delay! I really don't know what is causing this and would appreciate help. Computer specs: http://i.imgur.com/MMs5I58.png
Codes:
Arduino:
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: 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)
{
textSize(25);
text(key, width/2, 16);
}
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');
}
THANK YOU!
Answers
I don't have Arduino, but I've re-factored your Processing code as much as I could! =:)
Dunno if it's gonna be any speedier though! :-??
It turned out that it was a problem with Arduino, that had to be fixed by changing the processing code. It would seem that the arduino's serial buffer was being overloaded. I overlooked it because the fact that it was sending it at 60 times per second, but that was every 16ms and I thought it was 41ms. So I quick fixed it to fps of 20 and works great.
Loop your code didn't work :/ The rover didn't move when I pressed wasd or h... However there are some concepts I could learn from this code. Such as almost everything you added :P
Loop were did you learn all this?
Oops! A little mistake here. I've failed to realize you were sending an ASCII value rather than a number value! :@)
For example, in
port.write( '3' );
, I was thinkingport.write( 3 );
! b-(Please, replace the buggy initLetters() w/ the fixed 1 below:
You mean the ASCII/UNICODE
char
values? I've learned about it since when I programmed in BASIC a long time ago! :PI meant like "static final void", "java.util.Arrays.fill(letters, (byte) '4');" what in the world is that, and all that other stuff that makes noobs scratch their heads :?
And funny thing was it wasn't working until I sent it as a char myself. I was bummed it wasn't working then I tried ('3') and voila. The code for serial transmission is from Jeremy Blum and the - '0' part was needed at the time. I know little to nothing about serial transmission stuffz.
Surprisingly, Processing's reference page got definition for those 2:
final
http://processing.org/reference/final.htmlstatic
http://processing.org/reference/static.htmlMethod fill() is from class Arrays:
http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#fill-byte:A-byte-
Oh yeah, I forgot again that processing uses final, not const
Not Processing itself, but its compiled language -> Java! :P