I have a strange issue with Serial: it will sometimes freeze mid-message (i.e. Serial.print("This is my message"); will only print "This is m" and then nothing). The TX light on the Uno is no longer lit, and as far as I can tell, the entire program locks up. Any idea what could be causing this and/or what I might try to fix it?
Any idea what could be causing this and/or what I might try to fix it?
Posting your code is the usual start.
Probably a memory issue, but without your code it's impossible to tell.
It is also important to tell us what you are talking to.
I have a 360 degree continuous motion servo attached to a rod. The interrupt is one of the channel pins on a wheel watcher watching the rotation of the rod. At the top of the rod is an infrared distancer and an ultrasonic one.
#include <Servo.h>
#define SCAN_RESOLUTION 1
#define ULTRASONIC_CONTROL_PIN 7
#define ULTRASONIC_READ_PIN A1
#define INFRARED_PIN A0
unsigned int ultraSonicDistance = 0;
double infraredValue = 0.0;
double infraredDistance = 0.0;
volatile int xAngle = 0; //Goes from 0-300 covering 1 full circle
//int yAngle = 0;
Servo xServo;
//Servo yServo;
int xServoDirection = SCAN_RESOLUTION;
int yServoDirection = SCAN_RESOLUTION;
boolean highResScan = false;
double surroundingsMap[300] = {0};
void setup()
{
Serial.begin(9600);
xServo.attach(9,492,2348); //values found experimentally to make write(90) say stop
//yServo.attach(10);
attachInterrupt(0,stopXServo,FALLING);
pinMode(ULTRASONIC_CONTROL_PIN,OUTPUT);
digitalWrite(ULTRASONIC_CONTROL_PIN,LOW);
}
void loop()
{
xServo.write(90 + (xServoDirection*2)); //90 is "don't move"
Serial.print(90 + (xServoDirection*2));
Serial.print(" ");
Serial.println(xAngle);
if(highResScan)
delay(100);
}
void readInfrared()
{
infraredValue = analogRead(INFRARED_PIN);
infraredValue *= .0049;
infraredDistance = 3*pow((infraredValue-3),4) + 15;
}
void readUltrasonic()
{
digitalWrite(ULTRASONIC_CONTROL_PIN,HIGH);
delayMicroseconds(20);
ultraSonicDistance = analogRead(ULTRASONIC_READ_PIN);
digitalWrite(ULTRASONIC_CONTROL_PIN,LOW);
}
void stopXServo()
{
xServo.write(90);
readInfrared();
ultraSonicDistance = infraredDistance;
if(highResScan)
readUltrasonic();
surroundingsMap[xAngle] = (infraredDistance+ultraSonicDistance)/2;
xAngle += xServoDirection;
if(xAngle == 300)
{
xAngle = 0;
//xServoDirection *= -1;
/*yServo.write(yServo.read()+yServoDirection);
if(yAngle == 90 || yAngle == 0)
yServoDirection *= -1;*/
}
}
The serial communication stops, as does the rotation of my servo/rod setup, around xAngle = 295 to 298.
double surroundingsMap[300] = {0};
1200 bytes of memory used, right there. Is this on a 328-based Arduino, with 2048 bytes of memory?
Changing the resolution of the values stored, so that one unit is say 1/10" of an inch or 1/4 of a centimeter would cut your storage requirements in half.
Changing the resolution of the values stored, so that one unit is say 1/10" of an inch or 1/4 of a centimeter would cut your storage requirements in half.
While you're correct, and I probably should change the resolution of my scan, I'm not sure what you mean by "1/10" " since this is a rotational scale, not a linear one. The 300 is the number of ticks in a complete turn on my encoder.
I'm not sure what you mean by "1/10" " since this is a rotational scale, not a linear one.
The index is a rotational value. The data stored in the array is distance data. If you used ints, and stored 10ths of inches (78, instead of 7.8, for example), you'd use only half as much memory.
Oh, I see what you're getting at now. I found my problem with Serial freezing, too. I noticed I had a print in an interrupt, which doesn't work.