Things used in this project

Schematics

Wiring
Wiring i3uunejif2

Code

Normal code for controlling LED via webpageArduino
/* arduino web server - remote control (push button) */

#include "SPI.h"
#include "Phpoc.h"

#define STATE_YELLOW	'A'
#define STATE_GREEN		'B'
#define STATE_RED		'C'

#define PIN_LED_YELLOW 		3
#define PIN_LED_GREEN 		8
#define PIN_LED_RED			9

PhpocServer server(80);

char current_led = STATE_RED;
int pins[3] = {PIN_LED_YELLOW, PIN_LED_GREEN, PIN_LED_RED};

void handleEvent(char stt);

void setup(){
	Serial.begin(9600);
	while(!Serial)
		;

	Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
	//Phpoc.begin();

	server.beginWebSocket("remote_push");

	Serial.print("WebSocket server address : ");
	Serial.println(Phpoc.localIP());  
	
	pinMode(PIN_LED_YELLOW, OUTPUT);
	pinMode(PIN_LED_GREEN, OUTPUT);
	pinMode(PIN_LED_RED, OUTPUT);
	
	digitalWrite(PIN_LED_RED, HIGH);
	
}

void loop() {
	//Wait for a new client:
	PhpocClient client = server.available();

	if (client) {
		if (client.available() > 0) {
			//Read the bytes incoming from the client:
			char event = client.read();
			if(event == 'A' || event == 'B' || event == 'C')
				if(event != current_led)
					handleEvent(event);
		}
	}
}

void handleEvent(char event){
	
	for(int i = 0; i < 5; i++){
		digitalWrite(pins[current_led - 'A'], (i%2));
		delay(100);
	}
	//Turn on green led
	digitalWrite(pins[event - 'A'], HIGH);
	
	// update current state
	current_led = event;
}
Finite State Machine Code for controlling LED via webpageArduino
This code have the same functionality as normal code but using Finite State Machine model
/* arduino web server - remote control (push button) */

#include "SPI.h"
#include "Phpoc.h"

#define STATE_YELLOW	'A'
#define STATE_GREEN		'B'
#define STATE_RED		'C'

#define PIN_LED_YELLOW 		3
#define PIN_LED_GREEN 		8
#define PIN_LED_RED			9

PhpocServer server(80);

char fsm_state = STATE_RED;

void FSMEvent(char stt);

void setup(){
	Serial.begin(9600);
	while(!Serial)
		;

	Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
	//Phpoc.begin();

	server.beginWebSocket("remote_push");

	Serial.print("WebSocket server address : ");
	Serial.println(Phpoc.localIP());  
	
	pinMode(PIN_LED_YELLOW, OUTPUT);
	pinMode(PIN_LED_GREEN, OUTPUT);
	pinMode(PIN_LED_RED, OUTPUT);
	
	digitalWrite(PIN_LED_RED, HIGH);
}

void loop() {
	//Wait for a new client:
	PhpocClient client = server.available();

	if (client) {
		if (client.available() > 0) {
			//Read the bytes incoming from the client:
			char event = client.read();
			if(event == 'A' || event == 'B' || event == 'C')
				if(event != fsm_state)
					FSMEvent(event);
		}
	}
}

void FSMEvent(char event){
	
	//Action is taken based on current state and event
	switch(fsm_state){
		case STATE_YELLOW:
			
			if(event == STATE_GREEN){
				//Blink yellow led in a second and then turn off
				for(int i = 0; i < 5; i++){
					digitalWrite(PIN_LED_YELLOW, (i%2));
					delay(100);
				}
				//Turn on green led
				digitalWrite(PIN_LED_GREEN, HIGH);
			}
			else if(event == STATE_RED){
				//Blink yellow led in a second and then turn off
				for(int i = 0; i < 5; i++){
					digitalWrite(PIN_LED_YELLOW, (i%2));
					delay(100);
				}
				//Turn on red led
				digitalWrite(PIN_LED_RED, HIGH);
			}
			
			break;
			
		case STATE_GREEN:
			
			if(event == STATE_YELLOW){
				//Blink green led in a second and then turn off
				for(int i = 0; i < 5; i++){
					digitalWrite(PIN_LED_GREEN, (i%2));
					delay(100);
				}
				//Turn on yellow led
				digitalWrite(PIN_LED_YELLOW, HIGH);
			}
			else if(event == STATE_RED){
				//Blink green led in a second and then turn off
				for(int i = 0; i < 5; i++){
					digitalWrite(PIN_LED_GREEN, (i%2));
					delay(100);
				}
				//Turn on red led
				digitalWrite(PIN_LED_RED, HIGH);
			}
			
			break;
			
		case STATE_RED:
			
			if(event == STATE_YELLOW){
				//Blink red led in a second and then turn off
				for(int i = 0; i < 5; i++){
					digitalWrite(PIN_LED_RED, (i%2));
					delay(100);
				}
				//Turn on yellow led
				digitalWrite(PIN_LED_YELLOW, HIGH);
			}
			else if(event == STATE_GREEN){
				//Blink red led in a second and then turn off
				for(int i = 0; i < 5; i++){
					digitalWrite(PIN_LED_RED, (i%2));
					delay(100);
				}
				//Turn on green led
				digitalWrite(PIN_LED_GREEN, HIGH);
			}
			
			break;
	}
	
	// update current state
	fsm_state = event;
}

Credits

Replications

Did you replicate this project? Share it!

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

An Arduino-Powered Easter Egg Printer
Intermediate
  • 1,347
  • 18

Have you started dyeing Easter eggs yet? We at ELEKS have created a smart Egg Printer powered by Arduino Uno R3.

Shooting Game
Intermediate
  • 110
  • 5

Full instructions

A shooting game using a PHPoC Blue and toy gun.

Weekly Scheduling for IoT Smart Devices via Web
Intermediate
  • 256
  • 9

Protip

Smart devices may need to be able to be set weekly schedules. Some functionality can be activated/deactivated at some specific time.

DIGITAL BASCULE WITH ARDUINO UNO AND FSR SENSOR
Intermediate
  • 331
  • 1

Protip

The main goal is to make a practical digital bascule and using Arduino UNO board and FSR Sensor.

Automatic Fear 1.0
Intermediate
  • 313
  • 5

Full instructions

A device capable of frightening my sister and with compatibility with other members of the family

Arduino Addons : Battery Level Indicator
Intermediate
  • 1,871
  • 24

A RealTime Battery Indicator For Your Project Without Extra Components Or Using Any Analog I/Os

ProjectsCommunitiesContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Respect project
Feedback