This is a page for code examples. // Reads an analog input on 0, prints the result as an ASCII-formatted decimal value // This can be repurposed from Analog to Digital by swapping around the commented lines int sensorValue; //int digIn; void setup() { Serial.begin(9600); pinMode(0, INPUT); } void loop() { sensorValue = analogRead(0); //digIn=digitalRead(0); Serial.println(sensorValue, DEC); //Serial.println(digIn); delay(10); } // Code for a Parallax PING))) sensor // See also http://arduino.cc/en/Tutorial/Ping int pin = 7; // Sets the sensor signal pin unsigned long duration; // Code for a Memsic accelerometer for detecting rocking on one axis // See also http://arduino.cc/en/Tutorial/Memsic2125 int pin = 7; // Sensor signal pin unsigned long duration; // SmartSurfaces 2010 // Project 2 // Design Group 1 // // Alex Carmichael, Jason Prasad, Beth Glesner // Joyce Tseng, Jim Christian, Chris Parker // // Written by Chris Parker #include <Servo.h> // The servo and light-dependent resistor (LDR) arrays // are both ordered in a clockwise fashion, like so: // // 0 // / \ // 2 --- 3 Servo servos[3]; int ldrs[3]; // The maximum # of degrees each servo moves before checking // the LDR array again. float servoSpeed = 10.0; // The standard deviation within which the solar tracker // decides it has found the light source to the best of its // ability. float seekThreshold = 60.0; // If the standard deviation of the LDRs is within , // is turned to false. Else, it is true. boolean seeking = true; // If the solar tracker is "open," is true. Else, it is // false. boolean isOpen = false; // The two green LEDs indicate that is true. The blue // LED indicates that the solar tracker has found its light source, // and that is false. int ledGreen1 = 3; int ledGreen2 = 4; int ledBlue = 5; void setup() { Serial.begin(9600); servos[0].attach(9); servos[1].attach(10); servos[2].attach(11); pinMode(ledGreen1, OUTPUT); pinMode(ledGreen2, OUTPUT); pinMode(ledBlue, OUTPUT);\ // The solar tracker ought to start closed. close(); } void loop() { ldrs[0] = analogRead(0); ldrs[1] = analogRead(1); ldrs[2] = analogRead(2); Serial.print("LDR Readings: {"); Serial.print(ldrs[0]); Serial.print(", "); Serial.print(ldrs[1]); Serial.print(", "); Serial.print(ldrs[2]); Serial.println(); if(ldrs[0] + ldrs[1] + ldrs[2] < 1200) { // The average LDR reading is pretty dim, so the solar // tracker simply closes until it has more light to track. digitalWrite(ledGreen1, LOW); digitalWrite(ledGreen2, LOW); digitalWrite(ledBlue, LOW); close(); } else if(!isOpen) { // The solar tracker is closed, but the LDRs are bright enough to // track light. So, open the solar tracker. digitalWrite(ledGreen1, HIGH); digitalWrite(ledGreen2, HIGH); digitalWrite(ledBlue, LOW); open(); } else { // The LDRs are bright, and the solar tracker is open. If the standard deviation // of the LDRs are within , stop looking for the light source. Else, // start looking again. if(sqrt(sq(ldrs[0] - ldrs[1]) + sq(ldrs[0] - ldrs[2]) + sq(ldrs[1] - ldrs[2])) < seekThreshold) { seeking = false; } else { seeking = true; } switch(seeking) { case true: // The solar tracker is still looking. Green LEDs on, blue LED off. digitalWrite(ledGreen1, HIGH); digitalWrite(ledGreen2, HIGH); digitalWrite(ledBlue, LOW); // This bit is tricky. I take each LDR and compare it to the other two using // some modular arithmetic. If it is the greatest LDR, it is facing the light, // and the corresponding servo needs to wind in. Else, if it is the least LDR, it // is facing away from the light, and the corresponding servo needs to unwind. Finally, // if the LDR is in the middle, it moves "mapped" to the other two LDRs. for(int i = 0; i < 3; i++) { if(ldrs[i] > ldrs[(i + 1) % 3] && ldrs[i] > ldrs[(i + 2) % 3]) servos[i].write(servos[i].read() - servoSpeed); else if(ldrs[i] < ldrs[(i + 1) % 3] && ldrs[i] < ldrs[(i + 2) % 3]) servos[i].write(servos[i].read() + servoSpeed); else servos[i].write(servos[i].read() - map(ldrs[i], minimumLDR(), maximumLDR(), -servoSpeed, servoSpeed)); } break; case false: // The solar tracker has found its light source, so it lets // the world know by turning on its big bright blue LED. digitalWrite(ledGreen1, LOW); digitalWrite(ledGreen2, LOW); digitalWrite(ledBlue, HIGH); break; } } // Don't overload the Arduino! delay(5); } int minimumLDR() { // Return the lowest LDR reading, as an integer return min(ldrs[0], min(ldrs[1], ldrs[2])); } int maximumLDR() { // Return the lowest LDR reading, as an integer return max(ldrs[0], max(ldrs[1], ldrs[2])); } void open() { // Until all the servos are maxed out, continue // to unwind all of the servos. This "opens" // the solar tracker. while(!allServosMax()) { for(int i = 0; i < 3; i++) { servos[i].write(servos[i].read() + servoSpeed); } } isOpen = true; delay(10); } void close() { // Until all the servos are minned out, continue // to wind all of the servos. This "closes" // the solar tracker. while(!allServosMin()) { for(int i = 0; i < 3; i++) { servos[i].write(servos[i].read() - servoSpeed); } } isOpen = false; delay(10); } boolean allServosMax() { if(servos[0].read() <= 178 && servos[1].read() <= 178 && servos[2].read() <= 178) return true; else return false; } boolean allServosMin() { if(servos[0].read() <= 1 && servos[1].read() <= 1 && servos[2].read() <= 1) return true; else return false; } |