2013年11月24日 星期日

week12

<week12>

lesson:3.

const int sensorPin=A0;
const float baselineTemp=22.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(", degree 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);

}


2.
lesson:4

因為有點錯誤
老師幫我們寫了一隻
debug的程式
發現我們的腳正負極接錯了
還有輸出的位子的腳也接錯了
測試的程式碼如下:
void setup(){
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
}
int r=0, g=0, b=255;
void loop(){
  analogWrite(9, g);
  analogWrite(10,r);
  analogWrite(11,b);
}
非常謝謝老師
再來將這周的程式碼放入板子中
程式碼如下:
const int greenLEDPin = 9; //Green pin in the RGB LED
const int redLEDPin = 11; //Red pin in the RGB LED
const int blueLEDPin = 10; //Blue pin in the RGB LED

const int redSensorPin = A0; //Photoresistor no. 1
const int greenSensorPin = A1; //Photoresistor no. 2
const int blueSensorPin = A2; //Photoresistor no. 3

int redValue = 0;
int greenValue = 0;
int blueValue = 0;
//These values can only be from 0 to 255.

int redSensorValue = 0;
int greenSensorValue = 0;
int blueSensorValue = 0;
//These values will be reading from the photoresistors.

void setup() {
  Serial.begin(9600);

  //Set up the RGB LED pins to be OUTPUT.
  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
}

void loop() {
  //Set up the XXSensorValues to read from the photoresistors.
  redSensorValue = analogRead(redSensorPin);
  delay(5);
  greenSensorValue = analogRead(greenSensorPin);
  delay(5);
  blueSensorValue = analogRead(blueSensorPin);
  delay(5);

  //Print on the serial monitor the values given by the photoresistors.
  Serial.print("Raw Sensor Values \t Red: ");
  Serial.print(redSensorValue);
  Serial.print("\t Green: ");
  Serial.print(greenSensorValue);
  Serial.print("\t Blue: ");
  Serial.print(blueSensorValue);

  //XXValue can only be from 0 to 255 because they are define the intensity of the pin on the RGB LED.
  redValue = redSensorValue/4;
  greenValue = greenSensorValue/4;
  blueValue = blueSensorValue/4;

  //Print on the serial monitor the values that the LED pin is on.
  Serial.print("Mapped Sensor Values \t Red: ");
  Serial.print(redValue);
  Serial.print("\t Green: ");
  Serial.print(greenValue);
  Serial.print("\t Blue: ");
  Serial.println(blueValue);

  //analogWrite is used here to write, instead of HIGH(1) or LOW(0), a certain intensity(0-255) that is more precise.
  analogWrite(redLEDPin, redValue);
  analogWrite(greenLEDPin, greenValue);
  analogWrite(blueLEDPin, blueValue);
}
得到的結果如下:
心得:
接腳跨接的時候
要小心正負極的相接
如果接錯就會發生問題
因次要小心
還有電阻是沒有正負極之分的
以上為我們兩個人這次的心得!!


沒有留言:

張貼留言