AndrewNohawk
Coding General

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//include Libraries
#include <LiquidCrystal.h>
#include <String.h>
#include <Servo.h>

//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();
 
}

Comments

  1. […] Arduino IPCam – Part 2 (Arduino Code) […]

Leave a Reply to AndrewNohawk » Blog Archive » Arduino IPCam Cancel reply

Your email address will not be published. Required fields are marked *