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.
#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.
}