0

lcd servo help
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

lcd servo help

by hsub on Sat Apr 19, 2008 12:53 am

Hello All
I have an lcd screen and a servo run at the same time. The servo is cycling in a series of rotation, while the messages to lcd screen are being sent from a form online. The problem i am having is that the cycle stops as the serial opens and messages come in. The problem could be the structure of my code or arduino interrupt. Here is what i have so far.
Any help/suggestions would be great.

thank you again.
Code: Select all | TOGGLE FULL SIZE


//LED Imports
/* 10º, 90º, 170º
Moves servo to 3 different angles
with a pause of 1 second each.
*/
char stringIn[] = {' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' '};

int ledPin6 = 8;
int ledPin3 = 5;
int ledPin = 2;

int servoPin = 14; // servo connected to digital pin 2

int myAngle; // angle of the servo roughly 0-180
int pulseWidth; // servoPulse function variable
void setup()
{
Serial.begin(9600);
backlightOn();
clearLCD();
pinMode(servoPin, OUTPUT); // sets pin 2 as output
pinMode(ledPin, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin6, OUTPUT);
}
void servoPulse(int servoPin, int myAngle)
{
pulseWidth = (myAngle * 10) + 600; // determines delay
digitalWrite(servoPin, HIGH); // set servo high
delayMicroseconds(pulseWidth); // micro pause
digitalWrite(servoPin, LOW); // set servo low
delay(20);
// refresh cycle
}

void loop()
{
if (nextByte() == 126) { // header byte ('~' character)
char charIn = 0;
byte i = 0;
while (charIn != 126) { // wait for header byte again
charIn = nextByte();
stringIn[i] = charIn;
i += 1;
} // stringIn is finished
for (int j=i-1; j<16; j++) {
stringIn[j] = ' '; // null out string
}
clearLCD();
delay(2);
selectLineOne();
delay(2);
Serial.println(stringIn);
delay(10);
}
digitalWrite(ledPin, HIGH); // sets the LED on
myAngle = 10; // starts at 10º
for(int i=0; i<3000; i++) // loops 50 times
{
servoPulse(servoPin, myAngle);
}
digitalWrite(ledPin, LOW); // sets the LED off

// waits for a second
// wk_2
digitalWrite(ledPin3, HIGH); // sets the LED on
myAngle = 90; // moves to 90º
for(int i=0; i<3000; i++) // loops 50 times
{
servoPulse(servoPin, myAngle);
}
digitalWrite(ledPin3, LOW); // sets the LED off


// wk_3
digitalWrite(ledPin6, HIGH); // sets the LED on
myAngle = 170; // then moves to 170º
for(int i=0; i<3000; i++) // loops 50 times
{
servoPulse(servoPin, myAngle);
}
digitalWrite(ledPin6, LOW); // sets the LED off

}
byte nextByte() {
while(1) {
if(Serial.available() > 0) {
byte b = Serial.read();
return b;
}
}
}

void selectLineOne(){ //puts the cursor at line 0 char 0.
Serial.print(0xFE, BYTE); //command flag
Serial.print(128, BYTE); //position
}
void clearLCD(){
Serial.print(0xFE, BYTE); //command flag
Serial.print(0x01, BYTE); //clear command.
}
void backlightOn(){ //turns on the backlight
Serial.print(0x7C, BYTE); //command flag for backlight stuff
Serial.print(157, BYTE); //light level.
}
[/code]
hsub
 
Posts: 1
Joined: Fri Apr 18, 2008 11:53 pm

by adafruit on Sat Apr 19, 2008 1:10 am

diecimila arduinos auto-reset every time the serial port is opened. you can look online (ie google, look at arduino.cc etc) there was a tutorial on how to remove the capacitor to keep this from happening

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: lcd servo help

by mtbf0 on Sat Apr 19, 2008 8:42 am

hsub wrote:
Code: Select all | TOGGLE FULL SIZE
byte nextByte() {
  while(1) {
    if(Serial.available() > 0) {
      byte b = Serial.read();
      return b;
    }
  }
}

note that while this routine waits for input your servo is not being updated.

also during the three seconds that you are sending pulses to the servos you could receive ~2700 characters, only 128 of which will fit in the arduino buffer.

and your servo timing isn't all that good. somebody's probably written a servo library you could use. hopefully there's something driven by a timer interrupt that refreshes them in the background. you might look in the code for lady ada's motor shield.

gotta go to work. good luck.
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Please be positive and constructive with your questions and comments.