LED hitcounter arduino script problem

Hello,

I'm new to the whole Arduino thing and I've got this little problem.

I want to make an LED hitcounter for my website but the script seems to be wrong..

Already searched on the internet and found this website,
christinamday .com/blog/?p=25.

Got the whole thing running, the Arduino can connect to my website but the script doesn't function correctly.

This is my python script:

fetch counter

import time
import urllib
import serial

usb serial connection to arduino

ser = serial.Serial(
'/dev/cu.usbserial-A600ai7x', 9600)
myUrl = 'blablabla/hitcounter.txt'

last_counter = urllib.urlopen(myUrl).read()
while (True):
counter = urllib.urlopen(myUrl).read()
delta = int(counter) - int(last_counter)
print "counter: %s, delta: %s" % (counter, delta)
ser.write(chr(ord(chr(delta))))
last_counter = counter
time.sleep(1)

And this is my Arduino script:

int blinks = 0; // amount of blinks

void setup() {

int ledPin = 13;

pinMode(ledPin, OUTPUT); // Set LED pin as an output pin

Serial.begin(9600);

}

void loop() {

if (Serial.available() > 0) {
blinks += Serial.read();
Serial.println(blinks);

if (blinks > 0) {
int ledPin = 13;
digitalWrite(ledPin, HIGH); //sets LED on
delay(1000); //waits for a second
digitalWrite(ledPin, LOW); //sets LED off
blinks--;
}
}

}

Can you help me out?

Gr. Koen Muller

You need to tell us more about what the problem is. Does the perl script not get executed? Does it call called, but not behave as expected? Does the Arduino sketch not read any serial data? Does it not blink the LED at all? Does it not blink the correct number of times?

I'm not a perl expert by any stretch of the imagination, but this looks strange:

 ser.write(chr(ord(chr(delta))))

delta looks like an integer, since it is the result of subtracting one integer from another. Suppose delta was 3. chr(delta) returns the ascii character that is the 3rd letter in the ascii collating sequence. Then, ord(chr(delta)) converts that letter back to a number (3). Then, chr(ord(chr(delta))) converts it back to a letter. Why?

What happens if delta does not represent a valid letter in the ascii collating sequence?

Some value is read by the Arduino, and assumed to be a number. The LED on pin 13 is turned on, and left on for one second. Then, it is turned off. If blinks is greater than 0, it is almost immediately turned back on again. (The off time is the length of time it takes to toggle a pin to off, to decrement blinks, and to test if blinks is still greater than 0. That's maybe a dozen clock cycles, at 16 million clock cycles per second. I doubt that the human eye can see the off time.)

Did some testing and the LED works!

The Arduino script:

int blinks = 0; // amount of blinks

void setup() {

int ledPin = 13;

pinMode(ledPin, OUTPUT); // Set LED pin as an output pin

Serial.begin(9600);

}

void loop() {

if (Serial.available() > 0) {
blinks += Serial.read();
Serial.println(blinks);

if (blinks > 0) {
int ledPin = 13;
digitalWrite(ledPin, HIGH); //sets LED on
delay(3000); //waits for 3 seconds
digitalWrite(ledPin, LOW); //sets LED off
blinks--;
}
}

}

And here the Python script:

fetch counter

import time
import urllib
import serial

usb serial connection to arduino

ser = serial.Serial(
'/dev/cu.usbserial-A600ai7x', 9600)
myUrl = 'http://mywebsite.nl/hitcounter2.txt'

last_counter = urllib.urlopen(myUrl).read()
while (True):
counter = urllib.urlopen(myUrl).read()
delta = int(counter) - int(last_counter)
print "counter: %s, delta: %s" % (counter, delta)
ser.write(chr(ord(chr(delta))))
last_counter = counter
time.sleep(1)

;D