Code 1: Calculate Temperature in Celsius from Analog Reading
int readTemperature(int analogPin) {
int reading = analogRead(analogPin);
float voltage = reading * (5.0 / 1023.0); // Convert analog reading to voltage
int temperatureC = (voltage - 0.5) * 100; // Assuming TMP36 sensor
return temperatureC;
}
void setup() {
Serial.begin(9600);
}
void loop() {
int temp = readTemperature(A0); // Call the function and store return value
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" C");
delay(1000);
}
Code 2: Add Two Numbers and Use Result to Blink LED
int addNumbers(int a, int b) {
return a + b; // Return the sum
}
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
int delayTime = addNumbers(200, 300); // Get delay time from function
digitalWrite(13, HIGH);
delay(delayTime);
digitalWrite(13, LOW);
delay(delayTime);
}