Step 3: The Cats Firmware
These counts are shown on the display. For the movements of the arm there's another counter so that multiple arm movements can be processed independent of the actual website-hits. That's because the arm swings a while after being hit by the servo.
The arduino checks the light so that at night, the servo won't work but the RGB LED will change it's color everytime a hit on the website happens (this part happens without a delay)
Here's the code:
/*
Web Server
Webserver listens to the web and moves a servo
LightSensor checks the light so that the servo isn't move when it's dark
A seven segment display works with the help of shiftregisters to show a counter
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 178, 188};
//for Digits:
int latchPin = 2;//muss noch angeschlossen werden
int clockPin = 4;
int dataPin = 3;
int yellowPin = 16;
int redPin = 18;
int bluePin = 17;
int greenPin = 19;
int btnPin = 7;
//colours
int red;
int green;
int blue;
int yellow;
int ydir; //direction of Yellow fade
//digit is the array for numbers 192 for 0, 249 for 1,... 255 for nothing and 191 for -
byte digit[] ={192, 249, 164, 176, 153, 146, 130, 248, 128, 144, 255, 191, 225, 163, 171, 198, 167, 123};
int dig1;
int dig2;
int number;
int light;
//light sensor:
int lightPin = 1;//light sensor
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
Servo myservo; //create servo object to control servo
int count = 0;
//time
unsigned long previousMillis = 0;
unsigned long currentMil = 0;
unsigned long previousMil = 0;
unsigned long mil = 0;
boolean timepassed = true;
void setup()
{
myservo.attach(6); // attaches the servo on pin 6
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(btnPin, INPUT);//switch
Serial.begin(9600);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digit[11]);//ones
shiftOut(dataPin, clockPin, MSBFIRST, digit[11]);//tens
digitalWrite(latchPin, HIGH);
number = 0;
red = 0;
green = 0;
blue = 0;
yellow = 250;
ydir = 1;
analogWrite(yellowPin, yellow);
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
myservo.write(0);
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop()
{
//randomSeed(analogRead(A0)); //Yellow random
//yellow = random(111, 256);
yellow = yellow + (1 * ydir); //kind of fade
if (yellow == 255) ydir= -1;
if (yellow == 110) ydir = 1;
//Serial.println(yellow);
analogWrite(yellowPin, yellow);//Eyes on
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
count = count +1;//there was a request
number = number+1;//Counter
SevenSegDis();//Display
if (number == 99) { //so the 100 is missing :( but who cares
//digitalWrite(yellowPin, HIGH); //not needed anymore because yellowPin is analogWrite all the time
number = 0;
}
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
randomSeed(analogRead(A0));
red = random(256);
randomSeed(analogRead(A0));
green = random(256);
randomSeed(analogRead(A0));
blue = random(256);
Serial.print("Servo: ");
Serial.println(myservo.read());
Serial.print("Timepassed: ");
Serial.println(timepassed);
Serial.print("Count: ");
Serial.println(count);
Serial.print("Number: ");
Serial.println(number);
Serial.print("Mil: ");
Serial.println(mil);
Serial.print("Licht: ");
Serial.println(light);
Serial.print("red: ");
Serial.println(red);
Serial.print("green: ");
Serial.println(green);
Serial.print("blue: ");
Serial.println(blue);
}
lightsens(); //checks the light
time();// checks the time
if (light <99 && timepassed == true && count >0 ){//so when its dark the servo doesn't work and
myservo.attach(6);//attaches Servo to Pin6 again
if (myservo.read()== 0 ) {
myservo.write(180); //arm up
timepassed = false;
count = count -1;
}
else if (myservo.read() == 180) {
myservo.write(0);//arm down
timepassed = false;
count = count -1;
}
delay(1000);
myservo.detach();//detaches Servo
//turn lights of if servo is running
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
else if (light >=99 && count >0){//so when its dark the light changes with every hit on the website
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
count = count-1;
timepassed = false;
}
if (digitalRead(btnPin) == HIGH){ //check if button is pressed
randomSeed(analogRead(A0));
red = random(256);
randomSeed(analogRead(A0));
green = random(256);
randomSeed(analogRead(A0));
blue = random(256);
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
}
//----Part two: Seven Segments Display
void SevenSegDis() {//controls display with two shift registers
number = constrain(number, 0, 99);//number >=0 and <99 because of display range
if (number<10){
dig1 = 10; //shows nothing
dig2 = number; //ones
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digit[dig2]);//ones
shiftOut(dataPin, clockPin, MSBFIRST, digit[dig1]);//tens here nothing
digitalWrite(latchPin, HIGH);
}
else {
dig1 = number/10;// first digit tens
dig2 = number - dig1*10; //second digit ones
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digit[dig2]);
shiftOut(dataPin, clockPin, MSBFIRST, digit[dig1]);
digitalWrite(latchPin, HIGH);
}
}
//---------Part Three: the light sensor part
void lightsens() {
light = analogRead(lightPin);
light /= 10;
}
//---------- Part Four: the time thingy
void time (){
currentMil = millis();
if (currentMil < previousMil) {// if millis overflows
mil += 34359738 - previousMil + currentMil;
}
else { // if millis has not overflown
mil += currentMil - previousMil;
if (mil >25000){//if 20 sec. passed
timepassed = true;//resets the timepassed to true for the servo
mil = mil-25000;
}
previousMil = currentMil;
}
}
Remove these ads by
Signing Up





















Not Nice















Visit Our Store »
Go Pro Today »



