OSDN Git Service

The Arduino sketch for Sample3_Arduino1 was wrong.
authortkawata <tkawata@users.sourceforge.jp>
Wed, 2 May 2012 14:18:41 +0000 (23:18 +0900)
committertkawata <tkawata@users.sourceforge.jp>
Wed, 2 May 2012 14:18:41 +0000 (23:18 +0900)
Fixed it.

Samples/Samples/Sample3_Arduino1/Arduino/sketch1/sketch1.ino

index fbd5e96..7b2350d 100644 (file)
@@ -1,21 +1,21 @@
 byte   gSeq = 0;
-String gInputString = "";         
+String gInputString = ""; 
 String gOutputString = "";
 String gInputBuffer = "";
-
-boolean stringComplete = false;  
+boolean gIsInputSuccess = true;
 
 void setup(){
   Serial.begin(9600);
 }
+
 void loop(){
-  
-  int val=analogRead(0);
-  String m = "analog input:";
-  m += val;
-  analogWrite(3,val/4);
-  sendMessage(m);
-  delay(50);
+  sendMessage("loop:" + String(millis()));
+  int a1=analogRead(0);
+  setData("/cells.xhtml#fromArduino1", a1);
+  int p = getData("/cells.xhtml#toArduino1");
+  if (gIsInputSuccess)
+    analogWrite(3,p);
+  delay(10);
 }
 
 void sendMessage(String message)
@@ -24,20 +24,41 @@ void sendMessage(String message)
   serialSend();
 }
 
-void setData()
+void setData(String path, int value)
 {
-  serialSendAndLoad();  
+  gOutputString = "S" + path + "," + String(value);
+  serialSend();  
 }
 
-void getData()
+int getData(String path)
 {
-  serialSendAndLoad();    
+  gOutputString = "G" + path;  
+  serialSendAndLoad();
+  if (gIsInputSuccess)
+    return int(gInputString.toInt());
+  else
+    return 0;  
+}
+
+void serialSend()
+{
+  gSeq ++;
+  if (gSeq > 99)
+  {
+    gSeq = 1;
+  }
+  if (gSeq < 10)
+  {
+    Serial.print("0");
+  }
+  Serial.print(gSeq, DEC);
+  Serial.println(gOutputString);
 }
 
 void serialSendAndLoad()
 {
   serialSend();
-  
+  gIsInputSuccess = false;
   unsigned long timeout = millis() + 1000;
   gInputString = "";
   int isDataRecieved = false;
@@ -59,13 +80,16 @@ void serialSendAndLoad()
         //check input data
         if (gInputBuffer.length() >= 2)
         {
-          char rsecChars[3];
-          gInputBuffer.toCharArray(rsecChars,2);
-          rsecChars[2] = 0;
-          if (int(rsecChars) == gSeq)
-          {
-            gInputString = gInputBuffer;
+          String s = gInputBuffer.substring(0,2);
+          if (s.toInt() == gSeq)
+          {          
+            gInputString = gInputBuffer.substring(2);
             isDataRecieved = true;
+            gIsInputSuccess = true;
+          }
+          else
+          {
+            gIsInputSuccess = false;
           }
         }
         gInputBuffer = "";
@@ -74,17 +98,3 @@ void serialSendAndLoad()
   }
 }
 
-void serialSend()
-{
-  gSeq ++;
-  if (gSeq > 99)
-  {
-    gSeq = 1;
-  }
-  if (gSeq < 10)
-  {
-    Serial.print("0");
-  }
-  Serial.print(gSeq, DEC);
-  Serial.println(gOutputString);
-}