]> Zhao Yanbai Git Server - acecode.git/commitdiff
....
authorAceVest <zhaoyanbai@126.com>
Sun, 19 Oct 2014 02:59:23 +0000 (10:59 +0800)
committerAceVest <zhaoyanbai@126.com>
Sun, 19 Oct 2014 02:59:23 +0000 (10:59 +0800)
arduino/LightSensor/LightSensor.ino [new file with mode: 0644]
arduino/UltrasonicSensor/UltrasonicSensor.ino
arduino/l/sketch_oct18a/sketch_oct18a.ino [new file with mode: 0644]
arduino/l/sketch_oct18b/sketch_oct18b.ino [new file with mode: 0644]

diff --git a/arduino/LightSensor/LightSensor.ino b/arduino/LightSensor/LightSensor.ino
new file mode 100644 (file)
index 0000000..aaa6446
--- /dev/null
@@ -0,0 +1,31 @@
+const int ledPin = 12;
+int cnt = 0;
+
+void setup() {
+  Serial.begin(9600);
+  pinMode(ledPin, OUTPUT);
+}
+
+void loop() {
+  cnt ++;
+  
+  int lv = analogRead(0);  // Light
+  delay(1);
+  
+  if(cnt % 10 == 0)
+  {
+    Serial.print("Light Sensor Value: ");
+    Serial.println(lv);
+  }
+  
+  if(lv < 400)
+  {
+    digitalWrite(ledPin, HIGH);
+  }
+  else
+  {
+    digitalWrite(ledPin, LOW);
+  }
+  
+}
index 4462e41da39d9401ef752914f265f0514e6bb88d..4290a8f6b62c4fe2e86d197a677feeb76bf3d29b 100644 (file)
@@ -19,7 +19,10 @@ void loop()
   delayMicroseconds(10);
   digitalWrite(TrigPin, LOW);
   
-  dist = pulseIn(EchoPin, HIGH) / 58.0;
+  // distance = (HIGH_TIME*(340m/s))/2;
+  // pulseIn get us
+  // distance = ((pulseIn/10E6)*340/2)*100
+  dist = pulseIn(EchoPin, HIGH) * 17 / 1000;
   
   Serial.print(dist);
   Serial.println("cm");
diff --git a/arduino/l/sketch_oct18a/sketch_oct18a.ino b/arduino/l/sketch_oct18a/sketch_oct18a.ino
new file mode 100644 (file)
index 0000000..ff669b7
--- /dev/null
@@ -0,0 +1,32 @@
+const int LedPins[] = {8, 9, 10, 11, 12};
+const int cnt = 5;
+void setup() {
+  int i;
+  for(i=0; i<cnt; ++i)
+  {
+    pinMode(LedPins[i], OUTPUT); 
+  }
+}
+
+static int curt = 0;
+
+void loop() {
+  int i;
+  for(i=0; i<cnt; ++i)
+  {
+    int ledPin = LedPins[i];
+    
+    if(i == curt)
+    {
+      digitalWrite(ledPin, LOW);
+    }
+    else
+    {
+      digitalWrite(ledPin, HIGH);
+    }
+  }
+  
+  curt = ++curt % cnt;
+  delay(200);
+}
+
diff --git a/arduino/l/sketch_oct18b/sketch_oct18b.ino b/arduino/l/sketch_oct18b/sketch_oct18b.ino
new file mode 100644 (file)
index 0000000..2dc9b84
--- /dev/null
@@ -0,0 +1,62 @@
+#include <Servo.h> 
+Servo myservo;  // create servo object to control a servo 
+                // a maximum of eight servo objects can be created 
+
+const int RLedPin = 11;
+const int BLedPin = 12;
+void setup() 
+{ 
+  Serial.begin(9600);
+  pinMode(RLedPin, OUTPUT);
+  pinMode(BLedPin, OUTPUT);
+  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
+} 
+int pos = 0;
+int last_pos = 0;
+
+void loop() 
+{ 
+  int lv = analogRead(0);  // Light
+  int sign = 1;
+  if(lv < 400)
+  {
+    sign = -1;
+  }
+  
+  int st = sign * 10;
+  
+  pos += st;
+  
+  if(pos <= 30) pos = 30;
+  if(pos >= 120) pos = 120;
+
+  if(pos != last_pos)
+  {
+    if(sign == 1)
+    {
+      digitalWrite(RLedPin, HIGH);
+      digitalWrite(BLedPin, LOW);
+    }
+    else
+    {
+      digitalWrite(RLedPin, LOW);
+      digitalWrite(BLedPin, HIGH);      
+    }
+    Serial.print("<> pos");
+    Serial.println(pos);
+    myservo.write(pos);
+    delay(15);
+  }
+  else
+  {
+   
+      digitalWrite(RLedPin, LOW); 
+      digitalWrite(BLedPin, LOW);
+    Serial.print("== pos");
+    Serial.println(pos);
+  }
+  
+  last_pos = pos;
+}