Hello everyone, I am relatively new to Arduino and programming in general. I am trying to program a temperature controller but I am pretty sure I am getting my assignments wrong in terms of variables and I just need some help. Here is my code:
*
* More flow control tools:
* - for loops
* - while loops
* - switch case
*
* Writing functions
*/
#define RED_LED 6
#define YELLOW_LED 7
#define GREEN_LED 8
#define TEMP_HIGH
#define TEMP_MEDIUM
#define TEMP_LOW
int analogRead();
int temp_to_output(float temperature);
float ain_value_to_temp(int analog_value);
void setup() {
// put your setup code here, to run once:
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
}
void loop() {
if analogWrite(GREEN_LED, temp_to_output >> 255){
int ain_value_to_temp = analogRead(TEMP_LOW);
return (HIGH);
// 100% output
}
else if analogWrite(YELLOW_LED, temp_to_output >> 127 || 255){
int ain_value_to_temp = analogRead(TEMP_MEDIUM);
return (MEDIUM);
// not 100% output
}
else analogWrite(RED_LED, temp_to_output >> 0 || 127) {
int aint_value_to_temp = analogRead(TEMP_HIGH)return (LOW);
//shutdown
}
}
Ok, so two things: one, those values I am not sure about. I forget the range of values that we had in lab. There isn't a dedicated lab sheet for this, so I am trying to figure that out.
Two, since looking at it again I am not sure where I got that from, and I am using the < operator instead. I know it doesn't compile, I was more concerned about the syntax and variable assignments. The idea of the controller is to have values within a certain range to either keep the device at a steady state, turn down the power usage, or turn it off completely.
I'm sorry if I am making simple mistakes, I am rather new to coding in general. I'm more of an applied engineering kind of guy, and even though this may be simple to everyone else I'm still confused.
Remove the return statements; you are not using them correctly. Your code lives only inside of loop(), there's nowhere to return to. After executing all the code in loop() control jumps back to the first statement in loop() and the process repeats ad infinitum.
if analogWrite(GREEN_LED, temp_to_output >> 255){
int ain_value_to_temp = analogRead(TEMP_LOW);
return (HIGH);
// 100% output
}
Spend some time reading the Arduino reference to learn how things like if(), analogWrite(), analogRead(), and int actually work.
For starters, things being compared with if() are necessarily enclosed in parentheses.