How to set variable after key press

Hello i make arduino controlled pellet burner and try to set variable on keypress but dont know how to make.

I want to set “int pelleton” with 4 keypad

EXAMPLE: if press 1 int pelleton = 32000, if press 2 int pelleton = 38000, if press 3 int pelleton = 45000- something like that

this is my code:

/-----( Import needed libraries )-----/
#include “cactus_io_DS18B20.h”

/-----( Declare Constants )-----/
#define RELAY_ON 0
#define RELAY_OFF 1
/-----( Declare objects )-----/
/-----( Declare Variables )-----/
#define Relay_1 2 // Arduino Digital I/O pin number
#define Relay_2 3
#define Relay_3 4
#define Relay_4 5

void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
Serial.println("— Start Serial Monitor SEND_RCVE —");
//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);

//—( THEN set pins as outputs )----
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
delay(4000); //Check that all relays are inactive at Reset

//==============IGNITION MODE=========================

//—( Turn Relay_2_ON-PELLET RELLAY-Load first portion pellets)—
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
delay(1000);
Serial.println(“relay2 ON”);
delay(40000); // wait for a 40 second
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
delay(1000);
Serial.println(“relay2 OFF”); //—pokazva che rele 2 e OFF
delay(1000);

//—( Turn Relay_1_ON-IGNITION)—
digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
delay(1000);
Serial.println(“relay1 ON”);
delay(130000); // wait for a 1 minute
digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
delay(1000);
Serial.println(“relay1 OFF and wait fan”); //—pokazva che rele 1 e OFF
delay(10000); // wait for a second

//—( Turn Relay_3_ON-FAN rellay ON)—
digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
delay(1000);
Serial.println(“relay3 ON”);
delay(60000);

digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
Serial.println(“relay3 OFF”);
delay(7000); // wait for a 7 second

digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
delay(1000);
Serial.println(“relay3 ON”);
delay(60000); // wait for a 60 second

digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
delay(1000);
Serial.println(“relay3 OFF”);
delay(7000); // wait for a 7 second

digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
Serial.println(“relay3 ON”);
delay(60000);
}//–(end setup )—

int pelleton = 32000;

void loop()

{
static int sleeptime = 320; //40=160 min, 80=320min, 160=640min, 320=1280min
if (sleeptime <= 0)
{
Serial.println(“exit”);
delay(300000);
digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
exit(0);
}
Serial.println(“loop”);
sleeptime = sleeptime - 1;

//===============TEMP SENSOR======================

int DS18S20_Pin = 7; //DS18S20 Signal pin on digital 7
DS18B20 ds(DS18S20_Pin); // on digital pin 7

ds.readSensor();
Serial.print(ds.getTemperature_C()); Serial.print(" *C\t");
delay(200);

if (ds.getTemperature_C() <= 50)
{
pelleton = pelleton + 5000;
}

if (ds.getTemperature_C() <= 70)
{
pelleton = pelleton - 4000;
}

if (ds.getTemperature_C() >= 90)
{
pelleton = pelleton - 7000;
}

//==============BURN MODE=========================

//—( Turn Relay_2_ON-PELLET in cycle mode)—
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
delay(1000);
Serial.println(“relay2 ON cycle”);
delay(pelleton); // wait for a 30 second
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
Serial.println(“relay2 OFF cycle”);
delay(220000); // wait for a 220 seconds

}//–(end main loop )—

//( THE END )**

There is a library named "Keypad" that will help you interface with a keypad matrix. Install it with Sketch->Include Library->Manage Libraries... Then look in File->Examples->Keypad for example sketches.

ds.readSensor();
Serial.print(ds.getTemperature_C()); Serial.print(" *C\t");
delay(200);

if (ds.getTemperature_C() <= 50)
 {
   pelleton = pelleton + 5000;
 }
 
if (ds.getTemperature_C() <= 70)
 {
   pelleton = pelleton - 4000;
 }

if (ds.getTemperature_C() >= 90)

Is it really necessary to call the getTemperature_C() function 4 rimes?

Hello again thanks for your answers. I write something fast - is that will working what do you think.

Do you know how to put waiting function to wait me to press key before start and where to put it this function.

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’#’,‘0’,’*’}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

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

void loop()
{
char key = keypad.getKey();

if (key == ‘1’)
delay(1000);
Serial.println(“set pelleton 32000”);
{
int pelleton = 32000;
}

if (key == ‘2’)
delay(1000);
Serial.println(“set pelleton 39000”);
{
int pelleton = 39000;
}

if (key == ‘3’)
delay(1000);
Serial.println(“set pelleton 45000”);
{
int pelleton = 45000;
}
if (key == ‘4’)
delay(1000);
Serial.println(“set pelleton 50000”);
{
int pelleton = 50000;
}
}

You have a problem with your if statements curly braces. This:

 if (key == '1')
    delay(1000);
    Serial.println("set pelleton 32000");
    {
     int pelleton = 32000;                  
    }

Actually says if key is 1, delay(1000). The other stuff is always executed as you have no doubt observed on the serial monitor. Also, you are declaring pelleton as a local variable between those braces that surround it. Get rid of the int and declare it as a global. Wen you do, declare it as unsigned int or long. Your later uses of larger values (e.g. 39000) won't fit in an int.

Improved:

 if (key == '1')
    {
    delay(1000);
    Serial.println("set pelleton 32000");
    pelleton = 32000;                  
    }

If you find yourself comparing an integer variable to a bunch of integer constants in a series of 'if' statements you can make the code more readable by using switch/case instead:

switch (key) {
case '1':
  delay(1000);
  Serial.println("set pelleton 32000");
  pelleton = 32000;
  break

case '2':
  delay(1000);
  Serial.println("set pelleton 39000");
  pelleton = 39000;
  break;

case '3':
  delay(1000);
  Serial.println("set pelleton 45000");
  pelleton = 45000;
  break;

case '4':
  delay(1000);
  Serial.println("set pelleton 50000");
  pelleton = 50000;
  break;
}

Yes but the program print- "set pelleton 32000" that is case '1' one time than do nothing.

I want to print case '2' , case '3' etc....

Yes but the program print- "set pelleton 32000" that is case '1' one time than do nothing.

Then post the COMPLETE program again - NO MORE SNIPPETS!

Now the keyboard is working but ask me on every loop to set pelleton i want to set pelleton at the begining of the code and then the the pelleton to be apply to the end of the program.

Thanks

/-----( Import needed libraries )-----/
//#include “cactus_io_DS18B20.h”
//#include “avr\wdt.h”
#include “Keypad.h”

/-----( Declare Constants )-----/
#define RELAY_ON 0
#define RELAY_OFF 1
/-----( Declare objects )-----/
/-----( Declare Variables )-----/
#define Relay_1 2 // Arduino Digital I/O pin number
#define Relay_2 3
#define Relay_3 4
#define Relay_4 5

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’#’,‘0’,’*’}
};
byte rowPins[ROWS] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
Serial.println("— Start Serial Monitor SEND_RCVE —");
// wdt_enable(WDTO_500MS);
Serial.println(“WDT start”);
//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);

//—( THEN set pins as outputs )----
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
delay(4000); //Check that all relays are inactive at Reset

//int DS18S20_Pin = 7; //DS18S20 Signal pin on digital 2
// DS18B20 ds(DS18S20_Pin); // on digital pin 2

//==============IGNITION MODE=========================

//—( Turn Relay_2_ON-PELLET RELLAY-Load first portion pellets)—
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
delay(1000);
Serial.println(“relay2 ON”);
delay(40000); // wait for a 40 second
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
delay(1000);
Serial.println(“relay2 OFF”); //—pokazva che rele 2 e OFF
delay(1000);

//—( Turn Relay_1_ON-IGNITION)—
// if (ds.getTemperature_C() <= 25)
// ds.readSensor();
// Serial.print(ds.getTemperature_C()); Serial.print(" *C\t");
// delay(200);

{
digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
delay(1000);
Serial.println(“relay1 ON”);
delay(130000); // wait for a 1 minute
digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
delay(1000);
Serial.println(“relay1 OFF and wait fan”); //—pokazva che rele 1 e OFF
delay(10000); // wait for a second
}

//—( Turn Relay_3_ON-FAN rellay ON)—
digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
delay(1000);
Serial.println(“relay3 ON”);
delay(60000);

digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
Serial.println(“relay3 OFF”);
delay(7000); // wait for a 7 second

digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
delay(1000);
Serial.println(“relay3 ON”);
delay(60000); // wait for a 60 second

digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
delay(1000);
Serial.println(“relay3 OFF”);
delay(10000); // wait for a 10 second

digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
delay(1000);
Serial.println(“relay3 OFF”);
delay(7000); // wait for a 7 second

digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
Serial.println(“relay3 ON”);
delay(60000);
}//–(end setup )—

unsigned long pelleton = 32000;
void loop()
{
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key) {
case ‘1’:
delay(1000);
Serial.println(“set pelleton 32000”);
pelleton = 32000;
break;

case ‘2’:
delay(1000);
Serial.println(“set pelleton 39000”);
pelleton = 39000;
break;

case ‘3’:
delay(1000);
Serial.println(“set pelleton 45000”);
pelleton = 45000;
break;

case ‘4’:
delay(1000);
Serial.println(“set pelleton 50000”);
pelleton = 50000;
break;

default:
Serial.println(key);
}

{
// wdt_reset();
static int sleeptime = 320; //40=160 min, 80=320min, 160=640min, 320=1280min
if (sleeptime <= 0)
{
Serial.println(“exit”);
delay(300000);
digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
exit(0);
}
Serial.println(“loop”);
sleeptime = sleeptime - 1;

//===============TEMP SENSOR======================

int DS18S20_Pin = 7; //DS18S20 Signal pin on digital 7
//DS18B20 ds(DS18S20_Pin); // on digital pin 7

//ds.readSensor();
//Serial.print(ds.getTemperature_C()); Serial.print(" *C\t");
delay(200);

//if (ds.getTemperature_C() <= 50)
{
pelleton = pelleton + 5000;
}

//if (ds.getTemperature_C() <= 70)
{
pelleton = pelleton - 4000;
}

//if (ds.getTemperature_C() >= 90)
{
pelleton = pelleton - 7000;
}

//==============BURN MODE=========================

//—( Turn Relay_2_ON-PELLET in cycle mode)—
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
delay(1000);
Serial.println(“relay2 ON cycle”);
delay(pelleton); // wait for a 30 second
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
Serial.println(“relay2 OFF cycle”);
delay(220000); // wait for a 220 seconds

}
}
}//–(end main loop )—

//( THE END )**

//if (ds.getTemperature_C() <= 50)
 {
   pelleton = pelleton + 5000;
 }
 
//if (ds.getTemperature_C() <= 70)
 {
   pelleton = pelleton - 4000;
 }

//if (ds.getTemperature_C() >= 90)
 {
   pelleton = pelleton - 7000;
 }

Commenting out the line with the ‘if’ DOES NOT COMMENT OUT THE ENTIRE STATEMENT. Those comments change this part of the sketch to act like this:

   pelleton = pelleton + 5000;
   pelleton = pelleton - 4000;
   pelleton = pelleton - 7000;

Every time through loop() you subtract 6000 from pelleton. That will make your relay code act very strangely.

To comment out those ‘if’ statements you have to comment out every line:

//if (ds.getTemperature_C() <= 50)
// {
//   pelleton = pelleton + 5000;
// }
 
//if (ds.getTemperature_C() <= 70)
// {
//   pelleton = pelleton - 4000;
// }

//if (ds.getTemperature_C() >= 90)
// {
//   pelleton = pelleton - 7000;
// }

You can comment out a large block of sketch using a ‘block comment’

/*  (<- Beginning of comment)
if (ds.getTemperature_C() <= 50)
 {
   pelleton = pelleton + 5000;
 }
 
if (ds.getTemperature_C() <= 70)
 {
   pelleton = pelleton - 4000;
 }

if (ds.getTemperature_C() >= 90)
 {
   pelleton = pelleton - 7000;
 }
(End of comment  ->) */

Thanks for the reply but at the moment i don't have a problem with temp sensor.

The problem is with the keyboard. How to set pelleton at the begining of the program using key press

sasko12345:
[How to set pelleton at the begining of the program using key press[/quote]
That's easy. Call this function when you want to set pelleton using key press:

void setPelletonUsingKeyPress() {

pelleton = 0;
  while (pelleton == 0) {  // Repeat until the value is set
      char key = kpd.getKey();

switch (key) {
      case '1':
          pelleton = 32000;
          break;

case '2':
          pelleton = 39000;
          break;

case '3':
          pelleton = 45000;
          break;

case '4':
          pelleton = 50000;
          break;

case 0:
        break;  // No key pressed

default:
        Serial.println(key);  // Show keys that aren't active
    }         
  }  // End of 'while'
  Serial.print(F("Setting pelleton to "));
  Serial.println(pelleton);
}




](http://"http://forum.arduino.cc/index.php?msg=3245766")