Question

In: Computer Science

this is for those who did Arduino projects this code is supposed to work this way,...

this is for those who did Arduino projects

this code is supposed to work this way, there is a temperature sensor in the Arduino Uno along with a motor. the motor should turn on only when the temperature in the sensor exceeds 23 degrees Celcius. Here is the code

float AnalogToVolts(int reading);

void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);


}

void loop()
{
int reading;
float volts;
float temperature_in_celsius;
reading = analogRead(A1);

volts = AnalogToVolts(reading); //Function call

if ( temperature_in_celsius > 23)
{
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
  

Serial.println(temperature_in_celsius);
Serial.println(volts);
}

// Function Definition
float AnalogToVolts(int reading)
{
float temperature_in_celsius;
float volts;
volts = reading/1023.0 * 5.0; //Perform conversion

temperature_in_celsius = volts * 100 - 50;

  

return temperature_in_celsius;
return volts; //Return result
}

but it does not work. it properly shows the temperature and all, but the motor do not respond correctly with the temperature. Please fix this. Thank you

Solutions

Expert Solution

Hey below are some errors that I noticed:

  1. A function in a C program cannot have more than one return statement.
  2. The variables float volts; float temperature_in_celsius; inside the void loop( ) are the local variables to this method only.

No other function has access to them unless you pass them those variables.

  1. The variables float temperature_in_celsius; float volts; declared inside function AnalogToVolts( int reading ) are local to this function.

It is not like , if we update temperature_in_celsius inside function AnalogToVolts , it doesn't mean that the temperature_in_celsius inside void loop( ) gets updated.

______________________________________________________________________________________________________________________________________________________________________________

float AnalogToVolts(int reading);

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

// setting A1 analog pin to read input from the sensor.

pinMode(A1, INPUT);
pinMode(13, OUTPUT);
}

void loop()
{
int reading;
float volts;
float temperature_in_celsius;
reading = analogRead(A1);

volts = AnalogToVolts(reading); //Function call

// updating temperature_in_celsius
temperature_in_celsius = volts*100 ;

if ( temperature_in_celsius > 23)
{
    digitalWrite(13, HIGH);
}
else
{
    digitalWrite(13, LOW);
}

Serial.println(temperature_in_celsius);
Serial.println(volts);
}

// Function Definition
float AnalogToVolts(int reading)
{

// the temperature_in_celsius variable , we don't need to declare one here

// float temperature_in_celsius;

float volts;

// MODIFICATION 1
// I also found that the conversion has some small error,
// Most of the standards say that.

// volts = 5*reading/1023
// multiplying the above with 100 gives the temperature in celsius

// volts = reading/1023.0 * 5.0; //Perform conversion

// temperature_in_celsius = volts * 100 - 50;

volts = 5.0*reading/1023.0;

// No need to update this local variable here
// return temperature_in_celsius;

// only returning volts
// there in the loop() we update temperature_in_celsius value

return volts; //Return result
}

____________________________________________________________________________________________________

THANK YOU!!


Related Solutions

Are those who are working economically affected in any way by those who are unemployed ?...
Are those who are working economically affected in any way by those who are unemployed ? Explain .
How did the work of slave women differ between those who lived in the Chesapeake region...
How did the work of slave women differ between those who lived in the Chesapeake region and those who lived in the Lower South (South Carolina and Georgia)?
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work...
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work ?
Exposed persons Those who ate suspected foods Unexposed persons Those who did not eat suspected foods...
Exposed persons Those who ate suspected foods Unexposed persons Those who did not eat suspected foods Suspected foods Ill Well Total Attack Rate % Ill Well Total Attack Rate % Ham 36 5 2 11 Potato Salad 40 4 9 6 Peas 16 15 10 13 An investigation of an outbreak of staphylococcal food poisoning occurred during the second week of January 2017 on campus. The case definition produced by the investigators was an acute onset of nausea, cramps, and...
In th eory, how is the gold standard supposed to work? How did practice differ from...
In th eory, how is the gold standard supposed to work? How did practice differ from theory during the height of the gold standard (1870–1914)? Discuss the main difficulties policymakers faced when they tried to restore the gold standard after the end of World War I.
Using Arduino to blink an LED in morse code. Morse code is used as one of...
Using Arduino to blink an LED in morse code. Morse code is used as one of the methods for communications. It consists of a series of “dit” and “dah” symbols. (i) Develop an Arduino program to produce a Morse code to express the phrase “We are students” using Pin #13 (connect an LED that is in series with a 220-Ω resistor to Pins #13 to view the information sent via the Morse code). Express one “dit” with LED on for...
arduino c code only write a code that counts down a timer on serial monitor and...
arduino c code only write a code that counts down a timer on serial monitor and if A1 is typed into serial monitor prints the timer counting down and writes at 1 second hello and at 5 secs goodbye and repeats every 5 secs A2 is typed as above at 2 seconds writes hello and 3 seconds writes goodbye A3 same as above but at 3 seconds says hello and 2 seconds goodbye This continues until c is pressed to...
Create a code showing Arduino IMU data being plotted by your Python code.  
Create a code showing Arduino IMU data being plotted by your Python code.  
A company applies an aptitude test to all those who apply for work as programmers. The...
A company applies an aptitude test to all those who apply for work as programmers. The results of these tests are analyzed according to their precedence (general advertising, advertisements for specialized magazines, employment agencies, personal recommendations or people who come spontaneously). With a significance level of 0.025, determine if there is a difference between the results of these tests according to their precedence. Your work should have detailed procedure, in this exercise you can use the tool offered by Excel...
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must...
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must read two digital input signals and turn on two LEDS as needed to show the sum of the inputs. e.g. 0 + 1 = 01.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT