I start a small project for a friend. We want to count passenger of a Lift for a wait time calculator
What should the program do?
Count the people
With a switch i send an input to pin 1,2,3 or 4 for my count system. The lift has 4 different speeds so he can transport more or less people.
belong to the speed the program evaluate status 1-3 1= less wait time 2= middle 3= many
send the status to a server so we can set a colour at a display (green yellow and red)
I have read many topics in the forum to come with the program so far but I can not get any further. Work with Arduino since a week so please be kind with your ratings. ;-D
Can anyone help me I would be so thankfull
Parts:
Arduino uno R3
Ethernet Shield with Wiznet W5100 Ethernet Chip Support Micro SD Car
Sketch:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <SPI.h>
#include <Ethernet.h>
unsigned long keyPrevMillis = 0;
unsigned long previousMillis = 0;
unsigned long interval1 = 60000; // reset counter in milliseconds
unsigned long postingInterval = interval1 - 1000; // postinginterval to the server one second before reset
const unsigned long keySampleIntervalMs = 25;
byte longKeySensorCountMax = 200; // Wert * 25 = x ms
byte longKeySensorCount = 0;
byte prevKeyState = HIGH; // Sensor is active low
byte keyPin = 7; // Sensor is connected to pin 7
int counter = 0;
int mode = 0;
int value = counter;
int status1 = 0;
int status2 = 0;
int status3 = 0;
int status4 = 0;
const int pin2 = 2;
const int pin3 = 3;
const int pin4 = 4;
const int pin5 = 5;
byte mac []= {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // IP Address of your Webseite
IPAddress ip(10,231,1,200); // IP Addresse of the Ehternet Shield
EthernetClient client;
char server[] = "Webspace"; // Name WEBSPACE
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
// HTTP connection to the server:
void httpRequest() {
Serial.println("try connecting...");
if (client.connect(server, 80)) {
Serial.println("connected");
client.print("GET /data2csv.php?Counter=");
client.println("");
client.print("Status");
client.print(status1);
client.print(status2);
client.print(status3);
client.print(status4);
client.println("");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
lastConnectionTime = millis();
}
else {
Serial.println("connection failed");
Serial.println("disconnecting...");
client.stop();
}
}
// called when Sensor is kept signal for less than 10 seconds
void shortKeySensor() {
counter = (counter + 1);
Serial.println ("counting");
}
// called when Sensor is kept signal for more than 10 seconds MANY PEOPLE
void longKeySensor() {
Serial.println("many people");
counter = (counter + 100);
}
// called when key goes from low to high
void keySensor() {
longKeySensorCount = 0;
}
// called when key goes from high to low
void keyRelease() {
if (longKeySensorCount >= longKeySensorCountMax) {
longKeySensor();
}
else {
shortKeySensor();
}
}
void setup() {
Serial.begin(25000);
delay(1000);
Ethernet.begin(mac, ip); // fixed IP
pinMode(keyPin, INPUT_PULLUP);
pinMode(pin2 , INPUT);
pinMode(pin3 , INPUT);
pinMode(pin4 , INPUT);
pinMode(pin5 , INPUT);
}
void loop(){
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting...");
client.stop();
}
if(!client.connected() && (millis() > lastConnectionTime + postingInterval)) {
httpRequest();
}
lastConnected = client.connected();
// key management
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
keyPrevMillis = millis();
byte currKeyState = digitalRead(keyPin);
if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
keySensor();
}
else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
keyRelease();
}
else if (currKeyState == LOW) {
longKeySensorCount++;
}
prevKeyState = currKeyState;
}
// counter evaluation
{
unsigned long currentMillis = millis();
// counter evaluation MODE 1
if (((unsigned long)(currentMillis - previousMillis) >= postingInterval) && (mode = 1)){
if((counter >= 0) && (counter <= 116)){
status1 = 1;
}
else if ((counter > 116) && (counter < 230)){
status1 = 2;
}
else if (counter >= 230){
status1 = 3;
}
}
// counter evaluation MODE 2
if (((unsigned long)(currentMillis - previousMillis) >= postingInterval) && (mode = 2)){
if((counter >= 0) && (counter <= 266)){
status2 = 1;
}
else if ((counter > 266) && (counter < 530)){
status2 = 2;
}
else if (counter >= 530){
status2 = 3;
}
}
// counter evaluation MODE 3
if (((unsigned long)(currentMillis - previousMillis) >= postingInterval) && (mode = 3)){
if((counter >= 0) && (counter <= 383)){
status3 = 1;
}
else if ((counter > 383) && (counter < 770)){
status3 = 2;
}
else if (counter >= 770){
status3 = 3;
}
}
// counter evaluation MODE 4
if (((unsigned long)(currentMillis - previousMillis) >= postingInterval) && (mode = 4)){
if((counter >= 0) && (counter <= 616)){
status4 = 1;
}
else if ((counter > 616) && (counter < 1230)){
status4 = 2;
}
else if (counter >= 1230){
status4 = 3;
}
previousMillis = currentMillis;
}
}
{ // counter reset
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= interval1){
counter = 0;
Serial.println ("");
Serial.println ("RESET COUNTERLOG");
Serial.println ("");
previousMillis = currentMillis;
}
}
if (digitalRead(pin2)== HIGH){ // Mode 1
mode = 1;
}
if (digitalRead(pin3)== HIGH){ // Mode 2
mode = 2;
}
if (digitalRead(pin4)== HIGH){ // Mode 3
mode = 3;
}
if (digitalRead(pin5)== HIGH){ // Mode 4
mode = 4;
}
}
You appear to be using an Ethernet Shield. Pins 4, 10, 11, 12, and 13 are used by the Ethernet Shield. Pin 4 is the Slave Select pin for the SD card slot on the Ethernet Shield. Things may go wrong if that pin is LOW. Try moving your 'pin4' input to some unused pin.
While you are at it, you should change the names to something like "Mode3Pin". It will make your code more portable if you add any functions that require you to move an input pin again.
You are not using any of the WiFi functions so you can comment out those lines at the top.
johnwasser:
You appear to be using an Ethernet Shield. Pins 4, 10, 11, 12, and 13 are used by the Ethernet Shield. Pin 4 is the Slave Select pin for the SD card slot on the Ethernet Shield. Things may go wrong if that pin is LOW. Try moving your 'pin4' input to some unused pin.
While you are at it, you should change the names to something like "Mode3Pin". It will make your code more portable if you add any functions that require you to move an input pin again.
You are not using any of the WiFi functions so you can comment out those lines at the top.
Done... thanks for that
My counter has already worked (3 days ago) but now nothing get displayed at the serial monitor and I think I have adjusted something the wrong way. Also the various modes do not work. Do you have ideas to help me?
Sketch:
#include <SPI.h>
#include <Ethernet.h>
unsigned long keyPrevMillis = 0;
unsigned long previousMillis = 0;
unsigned long resetinterval = 60000; // reset counter in milliseconds
unsigned long postingInterval = resetinterval - 1000; // postinginterval to the server one second before reset
const unsigned long keySampleIntervalMs = 25;
byte longKeySensorCountMax = 200; // Wert * 25 = x ms
byte longKeySensorCount = 0;
byte prevKeyState = HIGH; // Sensor is active low
byte keyPin = 2; // Sensor is connected to pin 2
int counter = 0;
int value = counter;
int people = 0;
const int ModePin1 = 5;
const int ModePin2 = 6;
const int ModePin3 = 7;
const int ModePin4 = 8;
byte mac []= {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // IP Address of your Webseite
IPAddress ip(10,231,1,200); // IP Addresse of the Ehternet Shield
EthernetClient client;
char server[] = "Webspace"; // Name WEBSPACE
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
// HTTP connection to the server:
void httpRequest1() {
Serial.println("try connecting...");
if (client.connect(server, 80)) {
Serial.println("connected");
client.print("GET /data2csv.php?Counter=");
client.println("");
client.print("Status");
client.print(people);
client.println("");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
lastConnectionTime = millis();
}
else {
Serial.println("connection failed");
Serial.println("disconnecting...");
client.stop();
}
}
// called when Sensor is kept signal for less than 10 seconds
void shortKeySensor() {
Serial.println ("counting");
counter = (counter + 1);
}
// called when Sensor is kept signal for more than 10 seconds MANY PEOPLE
void longKeySensor() {
Serial.println("many people");
counter = (counter + 100);
}
// called when key goes from low to high
void keySensor() {
longKeySensorCount = 0;
}
// called when key goes from high to low
void keyRelease() {
if (longKeySensorCount >= longKeySensorCountMax) {
longKeySensor();
}
else {
shortKeySensor();
}
}
// counter evaluation MODE 1
void mode1(){
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= postingInterval){
if((counter >= 0) && (counter <= 116)){
people = 1;
}
else if ((counter > 116) && (counter < 230)){
people = 2;
}
else if (counter >= 230){
people = 3;
}
previousMillis = currentMillis;
}
}
// counter evaluation MODE 2
void mode2(){
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= postingInterval){
if((counter >= 0) && (counter <= 266)){
people = 4;
}
else if ((counter > 266) && (counter < 530)){
people = 5;
}
else if (counter >= 530){
people = 6;
}
previousMillis = currentMillis;
}
}
// counter evaluation MODE 3
void mode3(){
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= postingInterval){
if((counter >= 0) && (counter <= 383)){
people = 7;
}
else if ((counter > 383) && (counter < 770)){
people = 8;
}
else if (counter >= 770){
people = 9;
}
previousMillis = currentMillis;
}
}
// counter evaluation MODE 4
void mode4(){
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= postingInterval){
if((counter >= 0) && (counter <= 616)){
people = 10;
}
else if ((counter > 616) && (counter < 1230)){
people = 11;
}
else if (counter >= 1230){
people = 12;
}
previousMillis = currentMillis;
}
}
void setup() {
Serial.begin(115200);
delay(1000);
Ethernet.begin(mac, ip); // fixed IP
pinMode(keyPin, INPUT_PULLUP);
pinMode(ModePin1 , INPUT);
pinMode(ModePin2 , INPUT);
pinMode(ModePin3 , INPUT);
pinMode(ModePin4 , INPUT);
}
void loop(){
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting...");
client.stop();
}
if(!client.connected()&& (millis() > lastConnectionTime + postingInterval)) {
httpRequest1();
}
lastConnected = client.connected();
// key management
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
keyPrevMillis = millis();
byte currKeyState = digitalRead(keyPin);
if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
keySensor();
}
else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
keyRelease();
}
else if (currKeyState == LOW) {
longKeySensorCount++;
}
prevKeyState = currKeyState;
}
{ // counter reset
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= resetinterval){
counter = 0;
Serial.println ("");
Serial.println ("RESET COUNTERLOG");
Serial.println ("");
previousMillis = currentMillis;
}
}
if (digitalRead(ModePin1)== HIGH){ // Mode 1
mode1();
}
if (digitalRead(ModePin2)== HIGH){ // Mode 2
mode2();
}
if (digitalRead(ModePin3)== HIGH){ // Mode 3
mode3();
}
if (digitalRead(ModePin4)== HIGH){ // Mode 4
mode4();
}
}
That is rubbish. Pay attention to what a GET request looks like. It does NOT have embedded carriage returns and line feeds. It DOES end with " HTTP/1.0" or " HTTP/1.1".
And, all the stuff after the ? is name equal value pairs. Not some random crap.