Skip to content

Instantly share code, notes, and snippets.

@kaitoh
Last active October 21, 2015 00:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaitoh/06d5e50089d9d50cb780 to your computer and use it in GitHub Desktop.
Save kaitoh/06d5e50089d9d50cb780 to your computer and use it in GitHub Desktop.
ArduinoとFlashairを使ってスマホからサーボモーターを動かす。index.htmlファイルをFlashairに保存しといて、スマホからアクセスする。
#include <Servo.h>
#include <iSdio.h>
#include <utility/Sd2CardExt.h>
const int ctrl_pin = 2;
Servo servo;
const int chipSelectPin = 4;
Sd2CardExt card;
uint8_t buffer[512];
void printByte(uint8_t value) {
Serial.print(value >> 4, HEX);
Serial.print(value & 0xF, HEX);
}
void printBytes(uint8_t* p, uint32_t len) {
for (int i = 0; i < len; ++i) {
printByte(p[i]);
}
}
void setup() {
// Initialize UART for message print.
Serial.begin(9600);
while (!Serial) {
;
}
// Initialize SD card.
Serial.print(F("\nInitializing SD card..."));
if (card.init(SPI_HALF_SPEED, chipSelectPin)) {
Serial.print(F("OK"));
} else {
Serial.print(F("NG"));
abort();
}
memset(buffer, 0, 0x200);
servo.attach(ctrl_pin);
}
void loop() {
char str[5];
int val;
if (card.readExtMemory(1, 1, 0x1000, 0x200, buffer)) {
Serial.print("byte: ");
printBytes(buffer, 8);
Serial.println("");
str[0] = buffer[0];
str[1] = buffer[1];
str[2] = buffer[2];
str[3] = buffer[3];
str[4] = 0;
val = atoi(str);
Serial.println(val);
servo.write(val);
}
delay(2000);
}
<html>
<head>
<title>Title</title>
<script>
var datawrite = "http://flashair/command.cgi?op=131&ADDR=0&LEN=3&DATA=";
function servo_ctrl() {
var str = datawrite + document.getElementById('range').value;
console.log(str);
var xhr = new XMLHttpRequest();
xhr.open("GET", str);
xhr.send(null);
}
function change() {
servo_ctrl();
}
function st_click() {
servo_ctrl();
}
</script>
</head>
<body>
<div>
</div>
<div>
<form>
<input type=range id="range" min=0 max=160 onchange="change()">
<button id="start" onclick="st_click()">send</button>
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment