Go Down

Topic: lcd serial interrupts servo cycle (Read 567 times) previous topic - next topic

gtsown

Hi everyone, i'm working with a servo which cycles through 3 position for a couple of mins. I along with the servo, i have an lcd hook connected which takes in python messages. I need this serial connection to be open as the messages are coming frequently. The problem lies with the structure of my code. Once the serial connection is made, the servo cycle stops and only the lcd continues to work. I was looking into interrupt commands however i'm not sure as to how to pursue with this. any help on this would be great,

below  is the code i am working with.
thank you in advance.

Code: [Select]
#include "Wire.h"
// LCD
char stringIn[] = {' ', ' ', ' ', ' ', ' ',
                  ' ', ' ', ' ', ' ', ' ',
                  ' ', ' ', ' ', ' ', ' ', ' '};

/*  10º, 90º, 170º
   Moves servo to 3 different angles
   with a pause of 1 second each.
*/
int ledPin = 13;
int ledPin1 = 12;
int ledPin2 = 11;
 
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()
{
 pinMode(servoPin, OUTPUT);   // sets pin 2 as output
 pinMode(ledPin, OUTPUT);
 backlightOn();
 clearLCD();
 Serial.begin(9600);
}

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()
{
//wk_1
 digitalWrite(ledPin, HIGH);   // sets the LED on
 myAngle = 10;                  // starts at 10º
 for(int i=0; i<3000; i++)             // loops 300 times
 {
   servoPulse(servoPin, myAngle);
 }
 digitalWrite(ledPin, LOW);    // sets the LED off

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);
   }
}
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.
}

kuuk

hi,

when is your "header byte (~)" sent? if it's only at the beginning of a new serial message, it's clear why your servo won't do anyting after the first message is sent. i'm not sure if i understand your problem and code correctly. but maybe replace

Code: [Select]
while (charIn != 126) {
...
}

with something like

Code: [Select]
while (charIn != 126 && nextByte()) {
...
}


i didn't test this, but the idea is to leave the while loop when the serial message is over. not when the next message starts.


mem

This structure of this sketch is also being discussed in this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208832515/6

It may be more helpful if the OP would close one of the threads so that comments were in one place.

Go Up