#include "SPI.h"
#include "Phpoc.h"
PhpocServer server(80);
char slideName;
int slideValue;
int pwm_a = 5;
int pwm_b = 6;
int dir_a = 4;
int dir_b = 7;
void setup() {
pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
analogWrite(pwm_a, 0); //set both motors to run at (100/255 = 39)% duty cycle (slow)
analogWrite(pwm_b, 0);
Serial.begin(9600);
while(!Serial)
;
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
//Phpoc.begin();
server.beginWebSocket("remote_slide");
Serial.print("WebSocket server address : ");
Serial.println(Phpoc.localIP());
}
void loop() {
// wait for a new client:
PhpocClient client = server.available();
if (client) {
String slideStr = client.readLine();
if(slideStr)
{
slideName = slideStr.charAt(0);
slideValue = slideStr.substring(1).toInt();
Serial.print(slideName);
Serial.print('/');
Serial.println(slideValue);
if(slideName == 'A'){
Serial.println(slideValue);
if(slideValue < 0) {
digitalWrite(dir_a, HIGH);
slideValue = slideValue * -1 * 255 / 100;
} else {
digitalWrite(dir_a, LOW);
slideValue = slideValue * 255 / 100;
}
Serial.println(slideValue);
analogWrite(pwm_a, slideValue);
}
if(slideName == 'B'){
Serial.println(slideValue);
if(slideValue < 0) {
digitalWrite(dir_b, HIGH);
slideValue = slideValue * -1 * 255 / 100;
} else {
digitalWrite(dir_b, LOW);
slideValue = slideValue * 255 / 100;
}
Serial.println(slideValue);
analogWrite(pwm_b, slideValue);
}
}
}
}
Comments