--- /dev/null
+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);
+ }
+
+}
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");
--- /dev/null
+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);
+}
+
--- /dev/null
+#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;
+}