Hello,
I am trying to print some text on serial monitor using while loop but when the statement is true it print ("system on") the same text for infinite and while the text is false it prints ("system off") but i want it if the while (true) print system on only once same goes for system off as well.
I did try to look for the same scenario in the forum but i could not find one can anyone tell me what should i do. here is code i m working on :
// These constants won't change:
const int sensorPin = A2; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
const int buttonPin = 2; // pin that the button is attached to
void setup() {
// set the LED pins as outputs and the switch pin as input:
pinMode(indicatorLedPin, OUTPUT);
pinMode (ledPin, OUTPUT);
pinMode (buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
// while the button is pressed, take calibration readings:
while (digitalRead(buttonPin) == HIGH) {
Serial.println("System ON!!!!");
tempreading();
}
// signal the end of the calibration period
digitalWrite(indicatorLedPin, LOW);
Serial.println("System OFFFFF!!!!");
}
void tempreading() {
// turn on the indicator LED to indicate that Heating process is happening:
digitalWrite(indicatorLedPin, HIGH);
Serial.println("ON_on_Demand!!!!");
int reading =analogRead(tempSen);
if(reading < 920)
{
}
else
{
Serial.print("Temperature is more then 40*C");
}
If i move the while into setup() in while true condition wont allow me to access the tempreadin() which is to determine the temperature using temperature sensor.
// These constants won't change:
const int sensorPin = 2; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
const int buttonPin = 2; // pin that the button is attached to
void setup() {
// set the LED pins as outputs and the switch pin as input:
pinMode(indicatorLedPin, OUTPUT);
pinMode (ledPin, OUTPUT);
pinMode (buttonPin, INPUT);
Serial.begin(9600);
// while the button is pressed, take calibration readings:
while (digitalRead(buttonPin) == HIGH) {
Serial.println("System ON!!!!");
tempreading();
}
// signal the end of the calibration period
digitalWrite(indicatorLedPin, LOW);
Serial.println("System OFFFFF!!!!");
}
void loop() {
}
void tempreading() {
// turn on the indicator LED to indicate that Heating process is happening:
digitalWrite(indicatorLedPin, HIGH);
Serial.println("ON_on_Demand!!!!");
int reading =analogRead(sensorPin);
if(reading < 920)
{
}
else
{
Serial.print("Temperature is more then 40*C");
}
}
this will only works for one loop. for example if the starting state is on and if user press the switch if will shut ! and if the system is already off (this case system never will go on)
This is a different requirement than you said at first. So do you want it to always calibrate when the user presses the button?
if so try this:-
// These constants won't change:
const int sensorPin = 2; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
const int buttonPin = 2; // pin that the button is attached to
boolean calabrated = false // have we calabrated the sensor
void setup() {
// set the LED pins as outputs and the switch pin as input:
pinMode(indicatorLedPin, OUTPUT);
pinMode (ledPin, OUTPUT);
pinMode (buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
if(!calabrated){
// while the button is pressed, take calibration readings:
while (digitalRead(buttonPin) == HIGH) {
Serial.println("System ON!!!!");
tempreading();
}
calabrated= true;
// signal the end of the calibration period
digitalWrite(indicatorLedPin, LOW);
Serial.println("System OFFFFF!!!!");
}
if(digitalRead(buttonPin) == HIGH) calabrated = false;
// do other suff here
}
void tempreading() {
// turn on the indicator LED to indicate that Heating process is happening:
digitalWrite(indicatorLedPin, HIGH);
Serial.println("ON_on_Demand!!!!");
int reading =analogRead(sensorPin);
if(reading < 920)
{
}
else
{
Serial.print("Temperature is more then 40*C");
}
}
Getting error on that code! error:
Project_code_while:-1: error: expected unqualified-id before numeric constant
Project_code_while:11: error: expected ',' or ';' before 'void'