Last active
December 23, 2016 00:36
-
-
Save takehaya/41d58f69132ddd06c82309d8763b343a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Takeru Hayasaka Write Start to 11/9 | |
All rights reserved. | |
/* Create a WiFi access point and provide a web server on it. */ | |
#include <ESP8266WiFi.h> | |
#include <WiFiUdp.h> | |
#include <Servo.h> | |
#include "WifiControll.h" | |
Servo myservo; | |
int g_iMotor = 0; | |
int g_iIncrease = 10; | |
WiFiUDP udp; | |
void setup() { | |
Serialbegin(115200);//Serial | |
WiFi.config(ip, WiFi.gatewayIP(), WiFi.subnetMask()); // setting IP Address | |
WiFi.softAP(ssid, pass);//Wifi | |
IPAddress myIP = WiFi.softAPIP(); | |
Serialprint("AP IP address: "); Serialprintln(myIP); | |
Serial.println("Starting UDP"); | |
udp.begin(localPort); | |
Serialprint("Local port: "); Serialprintln(udp.localPort()); | |
// ESP8266のanalogWrite()の引数の範囲の初期値は[0-1023]。 | |
// 通常のArduinoのanalogWrite()の引数の範囲と同じ[0-255]に変更する。 | |
analogWriteRange(255); | |
pinMode(DC_MOTER_IN1_1, OUTPUT); | |
pinMode(DC_MOTER_IN1_2, OUTPUT); | |
pinMode(LED_Light, OUTPUT); | |
myservo.attach(SERVO_IN); | |
delay(10); | |
analogWrite(DC_MOTER_IN1_1, 0); | |
analogWrite(DC_MOTER_IN1_2, 0); | |
} | |
void loop() { | |
//signalData:L,M,S<=>LED点灯・Moter駆動・ステアリング駆動 | |
//ActionParameter:Type of Integer | |
//データフォーマット[signalData ActionParameter] | |
int rlen, NoData = 0;//データの長さ、データのこない分のカウンターバッファ | |
int Val_L = 0, Val_M = 0, Val_S = 0;//LED点灯・Moter駆動・ステアリング駆動 | |
while (true) { | |
//データが来ないので待つ | |
if (!(rlen = udp.parsePacket())) { | |
if (++NoData > 50) { | |
analogWrite(DC_MOTER_IN1_1, 0); | |
analogWrite(DC_MOTER_IN1_2, 0); | |
} | |
delay(10); | |
continue; | |
} | |
NoData = 0;//データがきたので来なかった記録をリセット | |
udp.read(packetBuffer, (rlen > OSC_PACKET_SIZE) ? OSC_PACKET_SIZE : rlen);//データを読み出す。 | |
Serialprint("L= "); Serialprintln(packetBuffer); | |
//指定の先頭文字でチェック | |
if (strncmp(&packetBuffer[0], "/osc/", 5) == 0) { | |
Serialprintln(packetBuffer[5]); | |
//指定のsignalDataでチェック | |
switch (packetBuffer[5]) { | |
case 'L'://LED | |
Serialprintln("signalDataでチェック"); | |
Serialprintln(packetBuffer[6]); | |
strcpy(packetBuffer, packetBuffer + 6); | |
Val_L = atoi(packetBuffer); | |
Serialprintln(Val_L); | |
analogWrite(LED_Light, Val_L); | |
break; | |
case 'S'://ステアリング駆動 | |
//analogWrite(SERVO_IN, 0); | |
strcpy(packetBuffer, packetBuffer + 6); | |
Val_S = atoi(packetBuffer); | |
//g_iMotor = Val_S; | |
if (Val_S >= (45 - 4) && Val_S <= (45 + 4)) { | |
Val_S = 45; | |
} | |
if (Val_S >= 45) { | |
//analogWrite(SERVO_IN, (Val_S - 64) * 16); | |
myservo.write(Val_S); | |
} else { | |
//analogWrite(SERVO_IN, (63 - Val_S) * 16); | |
myservo.write(Val_S); | |
} | |
break; | |
case 'M'://モーター駆動 | |
analogWrite(DC_MOTER_IN1_1, 0); analogWrite(DC_MOTER_IN1_2, 0); | |
strcpy(packetBuffer, packetBuffer + 6); | |
Val_M = atoi(packetBuffer); | |
if (Val_M >= (64 - 8) && Val_M <= (64 + 8)) { | |
Val_M = 64; | |
} | |
if (Val_M >= 64) { | |
analogWrite(DC_MOTER_IN1_1, (Val_M - 64) * 2); | |
} else { | |
analogWrite(DC_MOTER_IN1_2, (64 - Val_M) * 2); | |
} | |
break; | |
} | |
Serialprint("L= "); Serialprint(Val_L); | |
Serialprint("M= "); Serialprint(Val_M); | |
Serialprint("S="); Serialprintln(Val_S); | |
} | |
delay(10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment