Help needed Arduino Programming Escape room Software

Hi,

I got a example code for my escape room software to interact with Arduino.
In the example they use a button on a digital port. And if button is pressed arduino sends a custom term to the escape room PC.

I want to connect 5 buttons on the analog ports of the arduino, and if button 1 is pressed. The arduino has to send 1 as custom term to the Escape room PC. and if button 2 is pressed the software has to send 2.

The buttons are pressed long time, and arduino must only send 1 time his command.

Here is the example code:

And here the link with all info:

The easiest way to shape a GET request is the following. Connect a push button as shown in figure above. Then use the following code:

#include <SPI.h>
#include <Ethernet2.h>

// this must be unique

byte mac[] = {
0xFE, 0xE7, 0xDE, 0xB0, 0xDC, 0x9B };

int systemState = 0;

// change to your network settings

IPAddress ip(192,168,1,110);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// change to your server

IPAddress server(192.168.178.104);

char serverName[] = “192.168.178.104”;

// change to your server’s port

int serverPort = 14999;

EthernetClient client;
int totalCount = 0;
char pageAdd[64];

//buttonPin

const int buttonPin = 2; // the number of the pushbutton pin
void setup() {

Serial.begin(9600);

// disable SD SPI

pinMode(4,OUTPUT);
digitalWrite(4,HIGH);

//button pin mode

pinMode(buttonPin, INPUT);

// Start ethernet

Serial.println(“Starting ethernet…”);
Ethernet.begin(mac, ip, gateway, gateway, subnet);

Serial.println(Ethernet.localIP());

delay(2000);
Serial.println(“Ready”);

}

void loop() {

if (systemState == 0){
pushButton();
}

}

byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
int inChar;
char outBuf[128];

Serial.print(F(“connecting…”));

if(client.connect(ipBuf,thisPort) == 1)
{
Serial.println(F(“connected”));

sprintf(outBuf,”GET %s HTTP/1.1″,page);
client.println(outBuf);
sprintf(outBuf,”Host: %s”,serverName);
client.println(outBuf);
client.println(F(“Connection: close\r\n”));
}
else
{
Serial.println(F(“failed”));
return 0;
}

// connectLoop controls the hardware fail timeout

int connectLoop = 0;

while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);

// set connectLoop to zero if a packet arrives

connectLoop = 0;
}

connectLoop++;

// if more than 10000 milliseconds since the last packet

if(connectLoop > 10000)
{

// then close the connection from this end.

Serial.println();
Serial.println(F(“Timeout”));
client.stop();
}

// this is a delay for the connectLoop timing

delay(1);
}

Serial.println();

Serial.println(F(“disconnecting.”));

// close client end

client.stop();

return 1;
}

//Send get request to Houdini if button was pressed

void pushButton(){
if(digitalRead(buttonPin) == HIGH){

// send request to Houdini

sprintf(pageAdd,”/Custom_Term”); (!getPage(server, serverPort, pageAdd));
Serial.println(“Button pressed”);
systemState == 1;

}
}

Please use [code]  [/code] tags when posting code

http://forum.arduino.cc/index.php?topic=525335.0

  • as noted, format your code... what a pain for someone to waste their time doing so.. just in an effort to help YOU.

  • What is that example code? Just an example? or YOUR example from YOUR working project?

  • You can use Analog pins just like digital pins (no magic).

I'm not clear what your getpage function is supposed to be doing...
nor am I clear what variable/value is used to communicate with the "PC" you mention..

maybe something like this helps.

#include <SPI.h>
#include <Ethernet2.h>

//this must be unique
byte mac[] = {0xFE, 0xE7, 0xDE, 0xB0, 0xDC, 0x9B };

int systemState = 0;

//change to your network settings
IPAddress ip(192,168,1,110);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

//change to your server
IPAddress server(192.168.178.104);
char serverName[] = "192.168.178.104";

//change to your server's port
int serverPort = 14999;

EthernetClient client;
int totalCount = 0;
char pageAdd[64];

//buttonPin1

const int buttonPin1 = 14; // the number of the pushbutton pin (A0)
const int buttonPin2 = 15; // the number of the pushbutton pin (A1)
const int buttonPin3 = 16; // the number of the pushbutton pin (A2)
const int buttonPin4 = 17; // the number of the pushbutton pin (A3)
const int buttonPin5 = 18; // the number of the pushbutton pin (A4)
    
void setup() {

    Serial.begin(9600);

	// disable SD SPI
    pinMode(4,OUTPUT);
    digitalWrite(4,HIGH);

	//set button(s) pin mode
	for(i=14; i<=18; i++){
	    pinMode(i, INPUT);
	}

	// Start ethernet
    Serial.println("Starting ethernet…");
    Ethernet.begin(mac, ip, gateway, gateway, subnet);

    Serial.println(Ethernet.localIP());

    delay(2000);
    Serial.println("Ready");

}


void loop() {

    if (systemState == 0){
		pushButton();
    }

}

byte getPage(IPAddress ipBuf,int thisPort, char *page){
    int inChar;
    char outBuf[128];

    Serial.print(F("connecting…"));

    if(client.connect(ipBuf,thisPort) == 1){
		Serial.println(F("connected"));
		sprintf(outBuf,"GET %s HTTP/1.1?,page);
		client.println(outBuf);
		sprintf(outBuf,"Host: %s",serverName);
		client.println(outBuf);
		client.println(F("Connection: close\r\n"));
    }else{
		Serial.println(F("failed"));
		return 0;
    }

	// connectLoop controls the hardware fail timeout
    int connectLoop = 0;

    while(client.connected()){
		while(client.available()){
			inChar = client.read();
			Serial.write(inChar);

			// set connectLoop to zero if a packet arrives
			connectLoop = 0;
		}

		connectLoop++;

		// if more than 10000 milliseconds since the last packet	
		if(connectLoop > 10000){

			// then close the connection from this end.
			Serial.println();
			Serial.println(F("Timeout"));
			client.stop();
		}

		// this is a delay for the connectLoop timing
		delay(1);
    }

    Serial.println();
    Serial.println(F("disconnecting."));

	// close client end
    client.stop();
    
	return 1; //what is this for?
}

//Send get request to Houdini if button was pressed
void pushButton(){
    if(digitalRead(buttonPin1) == HIGH){
		// send request to Houdini
		sprintf(pageAdd,"/Custom_Term"); (!getPage(server, serverPort, pageAdd));
		Serial.println("Button 1 pressed");
		systemState == 1;
    }
	
	//button 2
	 if(digitalRead(buttonPin2) == HIGH){
		// send request to Houdini
		sprintf(pageAdd,"/Custom_Term"); (!getPage(server, serverPort, pageAdd));
		Serial.println("Button 2 pressed");
		systemState == 2;
    }
	
	//button 3
	 if(digitalRead(buttonPin3) == HIGH){
		// send request to Houdini
		sprintf(pageAdd,"/Custom_Term"); (!getPage(server, serverPort, pageAdd));
		Serial.println("Button 3 pressed");
		systemState == 3;
    }
	
	//button 4
	 if(digitalRead(buttonPin4) == HIGH){
		// send request to Houdini
		sprintf(pageAdd,"/Custom_Term"); (!getPage(server, serverPort, pageAdd));
		Serial.println("Button 4 pressed");
		systemState == 4;
    }
	
	//button 5
	 if(digitalRead(buttonPin5) == HIGH){
		// send request to Houdini
		sprintf(pageAdd,"/Custom_Term"); (!getPage(server, serverPort, pageAdd));
		Serial.println("Button 5 pressed");
		systemState == 5;
    }
}

Hi NiceGuy. See my PM.

I know this is an old post but did you get this going?

POST example, debounce added to 5 buttons that you can use as input. Used INPUT_PULLUP connection.
Arduino + Ethernet W5200-W5500 sketch:

/*|----------------------------------------------------------|*/
/*|SKETCH FOR ESCAPE ROOM - Arduino + Ethernet W5500         |*/
/*|Author: Bc. MARTIN CHLEBOVEC                              |*/
/*|FB: https://www.facebook.com/martin.s.chlebovec           |*/
/*|EMAIL: martinius96@gmail.com                              |*/
/*|WEB: https://arduino.php5.sk?lang=en                      |*/
/*|----------------------------------------------------------|*/
#include <SPI.h>
#include <Ethernet2.h>

byte mac[] = { 0xAA, 0xBB, 0xCC, 0x81, 0x7B, 0x4A };
IPAddress ip(192, 168, 1, 110);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
char serverName[] = "192.168.178.104"; //or use URL/domain
int serverPort = 14999;

const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;

int buttonState1 = HIGH;
int buttonState2 = HIGH;
int buttonState3 = HIGH;
int buttonState4 = HIGH;
int buttonState5 = HIGH;

int lastButtonState1 = HIGH;
int lastButtonState2 = HIGH;
int lastButtonState3 = HIGH;
int lastButtonState4 = HIGH;
int lastButtonState5 = HIGH;

unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
unsigned long lastDebounceTime4 = 0;
unsigned long lastDebounceTime5 = 0;
unsigned long debounceInterval = 50;



EthernetClient client;
int number = 0;
//your .php file that will get variable from Ethernet shield and Arduino
//in this case it is like: 192.168.178.104/sendnumber.php
String url = "/sendnumber.php";
void setup() {
  Serial.begin(115200);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  pinMode(buttonPin5, INPUT_PULLUP);
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.println("IP address set to Ethernet shield:");
  Serial.println(Ethernet.localIP());
  delay(2000);
  Serial.println("Ready");
}

void send_datas() {
  String my_datas = String(number);
  String data = "number=" + my_datas;
  if (client.connect(serverName, serverPort)) {
    client.println("POST " + url + " HTTP/1.0");
    client.println("Host: " + (String)serverName);
    client.println("User-Agent: ArduinoEthernetW5500");
    client.println("Connection: close");
    client.println("Content-Type: application/x-www-form-urlencoded;");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.println(data);
    while (client.connected()) {
      //HTTP HEADER
      String line = client.readStringUntil('\n');
      if (line == "\r") {
        break;
      }
    }
    //variable from webserver
    String line = client.readStringUntil('\n');
    //if (line == "OK") {
    //  Serial.println("DATAS arrived OKAY!");
    //}
    //for instance: OK when datas are okay (only php echo "OK"; script on webserver)
  } else {
    Serial.println("Error connecting to webserver!");
  }
  client.stop();
}

void loop() {
  int reading1 = digitalRead(buttonPin1);
  int reading2 = digitalRead(buttonPin2);
  int reading3 = digitalRead(buttonPin3);
  int reading4 = digitalRead(buttonPin4);
  int reading5 = digitalRead(buttonPin5);

  if (reading1 != lastButtonState1) {
    lastDebounceTime1 = millis();
  }

  if (reading2 != lastButtonState2) {
    lastDebounceTime2 = millis();
  }

  if (reading3 != lastButtonState3) {
    lastDebounceTime3 = millis();
  }

  if (reading4 != lastButtonState4) {
    lastDebounceTime4 = millis();
  }

  if (reading5 != lastButtonState5) {
    lastDebounceTime5 = millis();
  }
  if ((millis() - lastDebounceTime1) > debounceInterval) {
    if (reading1 != buttonState1) {
      buttonState1 = reading1;
      if (buttonState1 == HIGH) {
        number = 1;
        send_datas();
      }
    }
  }

  if ((millis() - lastDebounceTime2) > debounceInterval) {
    if (reading2 != buttonState2) {
      buttonState2 = reading2;
      if (buttonState2 == HIGH) {
        number = 2;
        send_datas();
      }
    }
  }

  if ((millis() - lastDebounceTime3) > debounceInterval) {
    if (reading3 != buttonState3) {
      buttonState3 = reading3;
      if (buttonState3 == HIGH) {
        number = 3;
        send_datas();
      }
    }
  }

  if ((millis() - lastDebounceTime4) > debounceInterval) {
    if (reading4 != buttonState4) {
      buttonState4 = reading4;
      if (buttonState4 == HIGH) {
        number = 4;
        send_datas();
      }
    }
  }
  if ((millis() - lastDebounceTime5) > debounceInterval) {
    if (reading5 != buttonState5) {
      buttonState5 = reading5;
      if (buttonState5 == HIGH) {
        number = 5;
        send_datas();
      }
    }
  }
  lastButtonState1 = reading1;
  lastButtonState2 = reading2;
  lastButtonState3 = reading3;
  lastButtonState4 = reading4;
  lastButtonState5 = reading5;

}

Sketch for webserver (PHP):

<?php 
$con = mysqli_connect("localhost","root_user","password","db_name");
mysqli_set_charset($con, "utf8");
if (mysqli_connect_errno($con)){
  echo "Error connecting to MySQL db: " . mysqli_connect_error();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $number = mysqli_real_escape_string($con, $_POST['number']);
  $number = trim( $number );
  if (is_numeric($number)){
    $insert1 = mysqli_query($con,"INSERT INTO `your_table` (`VARIABLE_COLUMN`) VALUES ('$number')") or die (mysqli_error($con));
    echo "OK";
  }
}else{
  echo "Unsupported METHOD! Use POST instead of GET/PUT/PATCH!";
}   
?>

Schematics (LCD not used in this connection, just use buttons connection!):