From cafd63923cabdb7c36ea359a5de3a65e60fdda98 Mon Sep 17 00:00:00 2001 From: AceVest Date: Sun, 19 Oct 2014 10:59:23 +0800 Subject: [PATCH] .... --- arduino/LightSensor/LightSensor.ino | 31 ++++++++++ arduino/UltrasonicSensor/UltrasonicSensor.ino | 5 +- arduino/l/sketch_oct18a/sketch_oct18a.ino | 32 ++++++++++ arduino/l/sketch_oct18b/sketch_oct18b.ino | 62 +++++++++++++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 arduino/LightSensor/LightSensor.ino create mode 100644 arduino/l/sketch_oct18a/sketch_oct18a.ino create mode 100644 arduino/l/sketch_oct18b/sketch_oct18b.ino diff --git a/arduino/LightSensor/LightSensor.ino b/arduino/LightSensor/LightSensor.ino new file mode 100644 index 0000000..aaa6446 --- /dev/null +++ b/arduino/LightSensor/LightSensor.ino @@ -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); + } + +} diff --git a/arduino/UltrasonicSensor/UltrasonicSensor.ino b/arduino/UltrasonicSensor/UltrasonicSensor.ino index 4462e41..4290a8f 100644 --- a/arduino/UltrasonicSensor/UltrasonicSensor.ino +++ b/arduino/UltrasonicSensor/UltrasonicSensor.ino @@ -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 index 0000000..ff669b7 --- /dev/null +++ b/arduino/l/sketch_oct18a/sketch_oct18a.ino @@ -0,0 +1,32 @@ +const int LedPins[] = {8, 9, 10, 11, 12}; +const int cnt = 5; +void setup() { + int i; + for(i=0; i + +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; +} -- 2.44.0