Arduino IPCam – Part 2 (Arduino Code)
So i’ve commented most of the lines and you should be able to easily follow what has happened in the code. Leave a comment if there are any questions :)
Code after the break!
//include Libraries
#include
#include
#include
//Our Two servos - Pan and Tilt
Servo panServo;
Servo tiltServo;
// initialize the LCD with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//Some default vars
//Where the default positions should be, servos go from 0-180 so 90 is centered for horizontal and vertical
int currentPos = 90;
int verticalPos = 90;
//Where our scrolling text position is
int currentTextPos = 0;
//Which pin holds our green LED (that we can blink)
int greenLed = 13;
//Default display text
String dispText = "";
//Initialisation Function - Setup the LCD[1],Serial[2],LED[3] and Servos[4-7]
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(greenLed, OUTPUT);
tiltServo.attach(9);
tiltServo.write(currentPos);
panServo.attach(8);
panServo.write(verticalPos);
}
//What to do when we recieve a command in, pretty self containing, not nice code but meh, PoC baby.
void parseCommand(String command)
{
if(command == "clearLCD")
{
clearLCD();
}
else if (command == "blinkLEDs")
{
blinkLEDs();
}
else if(command == "servoLeft")
{
moveServo("left");
}
else if(command == "servoRight")
{
moveServo("right");
}
else if(command == "servoDown")
{
moveServo("down");
}
else if(command == "servoUp")
{
moveServo("up");
}
else if(command == "fullRight")
{
currentPos = 0;
panServo.write(currentPos);
}
else if(command == "centerServo")
{
currentPos = 90;
panServo.write(currentPos);
}
else if(command == "fullLeft")
{
currentPos = 180;
panServo.write(currentPos);
}
else if(command == "fullDown")
{
verticalPos = 180;
tiltServo.write(verticalPos);
}
else if(command == "fullUp")
{
verticalPos = 0;
tiltServo.write(verticalPos);
}
else if(command == "vcenterServo")
{
verticalPos = 90;
tiltServo.write(verticalPos);
}
}
//Function to move the servos based on text, basically does ++ and -- if within range
void moveServo(String position)
{
//++Left and Right
if (position == "left")
{
if(currentPos < 180)
{
currentPos = currentPos + 10;
panServo.write(currentPos);
}
}
else if (position == "right")
{
if(currentPos > 0)
{
currentPos = currentPos - 10;
panServo.write(currentPos);
}
}
//--Left and Right
//++Up and Down
if (position == "up")
{
if(verticalPos > 0)
{
verticalPos = verticalPos - 30;
tiltServo.write(verticalPos);
}
}
else if (position == "down")
{
if(verticalPos < 180)
{
verticalPos = verticalPos + 30;
tiltServo.write(verticalPos);
}
}
//--Up and Down
}
//Silly little function to blink the LED, waits 2 seconds then does ON/OFF with 100ms delay after each
void blinkLEDs()
{
delay(2000);
for(int i=0;i<20;i++)
{
digitalWrite(greenLed,HIGH);
delay(100);
digitalWrite(greenLed,LOW);
delay(100);
}
}
//Main command checker - basically checks for commands, which are between {}, eg. {command}, if it finds it, sends it to the parser
void checkText()
{
if(dispText.length() > 1)
{
int commandStart = dispText.indexOf('{');
int commandEnd = dispText.indexOf('}');
if(commandStart != -1 && commandEnd != -1)
{
String commandText = dispText.substring(commandStart+1,commandEnd);
dispText = dispText.substring(0,commandStart);
parseCommand(commandText);
}
}
}
//Clear LCD
void clearLCD()
{
dispText = "";
lcd.clear();
}
//Main Loop, waits for bytes to come in, adds them to our dispText, otherwise it scrolls it, and then does the checking of that text
void loop()
{
char incomingByte = 0;
if (Serial.available())
{
while(Serial.available() > 0)
{
incomingByte = Serial.read();
dispText = dispText + incomingByte;
lcd.clear();
lcd.print(dispText);
}
}
else
{
currentTextPos++;
int txtLen = dispText.length() + 16;
if(currentTextPos > txtLen)
{
lcd.clear();
lcd.print(" ");
lcd.print(dispText);
currentTextPos = 0;
}
lcd.scrollDisplayLeft();
delay(300);
}
checkText();
}
Categories
Tags
Arduino
IPCam
LCD
LED
Serial
Servo
Webcam