2013年11月24日 星期日

week12

lesson 03:
const int sensorPin = A0;
const float baselineTemp = 25.0;

void setup()
{
  
  Serial.begin(9600);
  
  for (int pinNumber = 2; pinNumber < 5; pinNumber++)
  {
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }
}

void loop()
{
  int sensorVal = analogRead(sensorPin);
  
  Serial.print("Sensor Value: ");
  Serial.print(sensorVal);
  
  float voltage = (sensorVal/1024.0) * 5.0;
  
  Serial.print(", Volts:");
  Serial.print(voltage);
  
  Serial.print(", degrees C: ");
  float temperature = (voltage - .5) * 100;
  Serial.println(temperature); 
  
  if (temperature < baselineTemp)
  {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }
  
  else if (temperature >= baselineTemp + 2 && temperature < baselineTemp + 4)
  {
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }
  
  else if (temperature >= baselineTemp + 4 && temperature < baselineTemp + 6)
  {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
  }
  
  else if (temperature > baselineTemp + 6)
  {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
  }
  
  delay(1); 







  
}


lesson 04:

const int Brightness = 20;  // set overall brightness, use lower value in darkroom to protect your eyes (0-255)
const int greenLEDPin = 9;    // LED connected to digital pin 9
const int redLEDPin = 10;     // LED connected to digital pin 10
const int blueLEDPin = 11;    // LED connected to digital pin 11
const int redSensorPin = A0;  // pin with the photoresistor with the red gel
const int greenSensorPin = A1;   // pin with the photoresistor with the green gel
const int blueSensorPin = A2;   // pin with the photoresistor with the blue gel
int redSensorValue = 0; // variable to hold the value from the red sensor
int greenSensorValue = 0; // variable to hold the value from the green sensor
int blueSensorValue = 0; // variable to hold the value from the blue sensor
int redSensorHigh = 0; //High and low value for mapping. Notice that 'High' value were set low at 0, so that during calibration, its value would be be pushed up. See project 6.
int greenSensorHigh = 0;
int blueSensorHigh = 0;
int redSensorLow = 1023; // the opposite is true for 'Low' value
int greenSensorLow = 1023;
int blueSensorLow = 1023;
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  // set the digital pins as outputs
  pinMode(greenLEDPin,OUTPUT);
  pinMode(redLEDPin,OUTPUT);
  pinMode(blueLEDPin,OUTPUT);

  //switching on your lamp before calibration, as it would affect calibration too, especially in dark environment
  analogWrite(redLEDPin, Brightness);
  analogWrite(greenLEDPin, Brightness);
  analogWrite(blueLEDPin, Brightness);

  //actual calibration which takes 10 seconds. During calibration, LED would shine at constant brightness. After that, it'll start to respond.
  while (millis() < 10000) {
  redSensorValue = analogRead(redSensorPin);  //read pin
  if (redSensorValue > redSensorHigh) {  //push 'High' value up based on sensor value. At the end of calibration, 'High' value would take the highest 'SensorValue' during the 10 seconds.
  redSensorHigh = redSensorValue;
  }
  if (redSensorValue < redSensorLow) { //opposite is true for 'Low' value
  redSensorLow = redSensorValue;
  }
  delay(5);

  greenSensorValue = analogRead(greenSensorPin);
  if (greenSensorValue > greenSensorHigh) {
  greenSensorHigh = greenSensorValue;
  }
  if (greenSensorValue < greenSensorLow) {
  greenSensorLow = greenSensorValue;
  }
  delay(5);
  blueSensorValue = analogRead(blueSensorPin);
  if (blueSensorValue > blueSensorHigh) {
  blueSensorHigh = blueSensorValue;
  }
  if (blueSensorValue < blueSensorLow) {
  blueSensorLow = blueSensorValue;
  }
  delay(5);
  }
}
void loop() {
  // Read the sensors first:

  // read the value from the red-filtered photoresistor:
  redSensorValue = analogRead(redSensorPin);

  // give the ADC a moment to settle
  delay(5);
  // read the value from the green-filtered photoresistor:
  greenSensorValue = analogRead(greenSensorPin);
  // give the ADC a moment to settle
  delay(5);
  // read the value from the blue-filtered photoresistor:
  blueSensorValue = analogRead(blueSensorPin);
  // print out the values to the serial monitor
  Serial.print("raw sensor Values \t red: ");
  Serial.print(redSensorValue);
  Serial.print("\t green: ");
  Serial.print(greenSensorValue);
  Serial.print("\t Blue: ");
  Serial.println(blueSensorValue);
  /*
  In order to use the values from the sensor for the LED,
  you need to do some math. The ADC provides a 10-bit number,
  but analogWrite() uses 8 bits. You'll want to divide your
  sensor readings by 4 to keep them in range of the output.
  */
  /*
  Changed from a mere division by 4 in the original code to mapping.
  Lowest value recorded at calibration would be mapped to 0, and highest value would be mapped to brightness set earlier.
  The constraint function is set to prevent flickering, when value are negative or exceeds 255.
  */

  int redValue = map(redSensorValue,redSensorLow,redSensorHigh,0,Brightness);
  redValue = constrain(redValue,0,255);
  int greenValue = map(greenSensorValue,greenSensorLow,greenSensorHigh,0,Brightness);
  greenValue = constrain(greenValue,0,255);
  int blueValue = map(blueSensorValue,blueSensorLow,blueSensorHigh,0,Brightness);
  blueValue = constrain(blueValue,0,255);
  //  print out the mapped values
  Serial.print("Mapped sensor Values \t red: ");
  Serial.print(redValue);
  Serial.print("\t green: ");
  Serial.print(greenValue);
  Serial.print("\t Blue: ");
  Serial.println(blueValue);
  /*
  Now that you have a usable value, it's time to PWM the LED.
  */
  analogWrite(redLEDPin, redValue);
  analogWrite(greenLEDPin, greenValue);
  analogWrite(blueLEDPin, blueValue);
}


沒有留言:

張貼留言