Hej I kloge hoveder,
Jeg er lige i gang med at skrive en kode, hvor en temperatur sensor skal tænde for 3 lys:
- Øges temperaturen med 2 grader, tændes 1 LED.
- Øges temperaturen med 4 grader, tændes 2 LED.
- Øges temperaturen med 6 grader, tændes 3 LED.
Dette har jeg gjort!Men jeg vil gerne tage den et lille skridt videre, og gøre således, at så længe temperaturen er øges med mindst 8 grader, så skal alle 3 LED'ere blive ved med at blinke. Hvordan får jeg det ind i koden?- //DEFINING CONSTANTS:
- const int sensorPin = A0; //ANALOG INPUT 0
- const float baselineTemp = 21.0;
-
- void setup() {
- Serial.begin(9600); //OPEN A SERIAL PORT. THIS COMMAND OPENS A CONNECTION BETWEENT THE MCU AND THE COMPUTER.
- //THAT ENABLES US TO SEE THE VALUES FROM THE ANALOG INPUT [0]. COMMUNICATION SPEED OF GENUINO IS SET TO 9600 BITS/S
-
- for(int pinNumber = 2; pinNumber < 5; pinNumber++) {
- pinMode(pinNumber, OUTPUT);
- digitalWrite(pinNumber, LOW);
- }
- }
-
- void loop() {
- int sensorVal = analogRead(sensorPin); //sensorVal IS A LOCAL VARIABLE THAT STORES THE READING FROM THE SENSOR
- Serial.print("Sensor Value: ");
- Serial.print(sensorVal);
-
- //CONVERT THE ADC READING TO VOLTAGE:
- float voltage = (sensorVal/1024.0)*5.0;
- Serial.print(", Volts: ");
- Serial.print(voltage);
- Serial.print(", degrees C: ");
-
- //CONVERT THE VOLTAGE TO TEMPERATURE:
- float temperature = (voltage-0.5)*100;
- Serial.println(temperature);
-
- if(temperature < baselineTemp+2){
- 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);
- //IF TEMPERATURE IS INCREASED BY 2 DEGREES RELATIVE TO THE BASELINE TEMPERATURE, 1 LED TURNS ON.
- }
- else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
- digitalWrite(2, HIGH);
- digitalWrite(3, HIGH);
- digitalWrite(4, LOW);
- //IF TEMPERATURE IS INCREASED BY 4 DEGREES RELATIVE TO THE BASELINE TEMPERATURE, 2 LED TURNS ON.
- }
- else if(temperature >= baselineTemp+6){
- digitalWrite(2, HIGH);
- digitalWrite(3, HIGH);
- digitalWrite(4, HIGH);
- //IF TEMPERATURE IS INCREASED BY 6 DEGREES RELATIVE TO THE BASELINE TEMPERATURE, 3 LED TURNS ON.
- }
- else if(temperature >= baselineTemp+6 && temperature < baselineTemp+8){
- digitalWrite(2, HIGH);
- digitalWrite(3, HIGH);
- digitalWrite(4, HIGH);
- }
- delay(1000);
- //MEASURE PR. 1 SECOND
- }
Indlæg senest redigeret d. 12.06.2016 01:14 af Bruger #20986