Type the code on keypad to select product

Hello everyone !
Can anyone help me, i have a problem with my coding. The coding should be able to print out the name and the price of the product, but the thing is the keypad won’t allow me to type the code properly and thus it won’t print the price and the product on serial monitor. Can anyone point out my mistakes or error in this coding ?

I don’t know how to upload the coding properly so i just copy it to here.
Thank you in advanced :slight_smile:

`
#include <Keypad.h>

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

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

const String bebidas = {“A35”, “A36”, “A37”, “A38”, “A39”, “A40”};

String input_codigo;
float precio;
String refresco = " ";

void setup() {
Serial.begin(9600);
input_codigo.reserve(32); // maximum input characters is 33, change if needed
}

void loop() {
productos(&precio,&refresco);
Serial.print(“Has elegido:”);
Serial.println(refresco);
Serial.print(precio);
Serial.println(“€”);
delay(1000);
}
// elegir el producto
void productos (float *p, String *r)
{
char key = keypad.getKey();
if (key) {
Serial.print(key);
input_codigo += key;
if (input_codigo.length() == 3) {
if (input_codigo == bebidas[0]) {
*p = 1.5;
*r = “un refreco de cola”;
/Serial.println(" ");
Serial.println(“Has elegido:”);
Serial.print(“precio:”);
Serial.print(precio);
Serial.println(“€”);
/
}
else if (input_codigo == bebidas[1]) {
*p = 1.5;
*r = “un refreco de naranja”;
/Serial.println(" ");
Serial.println(“Has elegido un refresco de naranja”);
Serial.print(“precio:”);
Serial.print(precio);
Serial.println(“€”);
/
}
else if (input_codigo == bebidas[2]) {
*p = 1.5;
*r = “un refreco de limón”;
/Serial.println(" ");
Serial.println(“Has elegido un refresco de limón”);
Serial.print(“precio:”);
Serial.print(precio);
Serial.println(“€”);
/
}
else if (input_codigo == bebidas[3]) {
p = 2.5;
r = “un zumo”;
/
Serial.println(" ");
Serial.println(“Has elegido un zumo”);
Serial.print(“precio:”);
Serial.print(precio);
Serial.println(“€”);
/
}
else if (input_codigo == bebidas[4]) {
*p = 1.25;
*r = “un agua con gas”;
/Serial.println(" ");
Serial.println(“Has elegido un agua con gas”);
Serial.print(“precio:”);
Serial.print(precio);
Serial.println(“€”);
/
}
else if (input_codigo == bebidas[5]) {
*p = 1.0;
*r = “un agua mineral”;
/Serial.println(" ");
Serial.println(“Has elegido un agua mineral”);
Serial.print(“precio:”);
Serial.print(precio);
Serial.println(“€”);
/
}
else {
Serial.println(" ");
Serial.println(“no disponible, intentalo otra vez”);
}

  input_codigo = ""; // clear input password
}

}

}`

Just use your global variables you have already declared.

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the kpd

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

String bebidas[]  = {"A35", "A36", "A37", "A38", "A39", "A40"};

String input_codigo;
float precio;
String refresco = " ";

void setup() {
  Serial.begin(9600);
  input_codigo.reserve(32); // maximum input characters is 33, change if needed
}

void loop() {
  productos();
  Serial.print("Has elegido: ");
  Serial.println(refresco);
  Serial.print(precio);
  Serial.println("€");
  delay(1000);
}
// elegir el producto
void productos ()
{
  char key = keypad.getKey();
  if (key) {
    Serial.print(key);
    input_codigo += key;
    if (input_codigo.length() == 3) {
      bool isValid = true;
      if (input_codigo == bebidas[0]) {
        precio = 1.5;
        refresco = "un refreco de cola";
      }
      else if (input_codigo == bebidas[1]) {
        precio = 1.5;
        refresco = "un refreco de naranja";
      }
      else if (input_codigo == bebidas[2]) {
        precio = 1.5;
        refresco = "un refreco de limón";
      }
      else if (input_codigo == bebidas[3]) {
        precio = 2.5;
        refresco = "un zumo";
      }
      else if (input_codigo == bebidas[4]) {
        precio = 1.25;
        refresco = "un agua con gas";
      }
      else if (input_codigo == bebidas[5]) {
        precio = 1.0;
        refresco = "un agua mineral";
      }
      else {
        isValid = false;
      }
      if ( isValid == true ) {
        Serial.println("Has elegido ");
        Serial.println(refresco);
        Serial.print("precio: ");
        Serial.print(precio);
        Serial.println("€");
      }
      input_codigo = ""; // clear input password
    }
  }
}

You can also eliminate a lot of duplicate Serial.print() calls

okay thanks, i got it. A question, if i’m using void for the type of funtion, can i use the value of precio and refresco inside the loop ? Because this is kind of a sequence program that will use the value of precio for the next function. Or i need to modify something ?

I would appreciate if anyone can answer me :grinning:
Thnks a lot !

If you are asking if a local variable can be passed to a function, yes it can.

Google pass by reference c++ arduino

1 Like

if the function type is ‘void’ that means there is no value returned by the function. It has nothing to do with accessing global variables inside that function. You can always access global variables (those declared outside a function) hence the name ‘global’ :slight_smile:

1 Like

Thanks for the replies and help, i really appreciate it :smile:

I have another doubts.I would like to program a vending machine. I tried to compiled and call all the functions together in the loop. But when it came to the function of productos and print the value of precio, it returned ovf. How can i avoid this overflow value ? I’ll attach the coding in case anyone would like to point out my error. I know i kind of messed up for the first two functions inside the loop, but i think i can just ignore it for the moment. Thanks again !

#include "MapFloat.h"
#include <Keypad.h>

const int LED_ERROR = 10;
const int LED_REFRIGERACION = A4;
const int BUTTON_IMPORTE = A5;
const int UPS = A0;
const int SENSOR_TEMPERATURA = A1;

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

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

String bebidas[]  = {"A35", "A36", "A37", "A38", "A39", "A40"};
String input_codigo;
unsigned long previousMillis = 0;
float importe = 0.00;


void setup() {
 // put your setup code here, to run once:
 pinMode(LED_ERROR, OUTPUT);
 pinMode(LED_REFRIGERACION, OUTPUT);
 pinMode(BUTTON_IMPORTE, INPUT_PULLUP);
 pinMode(UPS, INPUT);
 pinMode(SENSOR_TEMPERATURA, INPUT);
 Serial.begin(9600);
 input_codigo.reserve(32);
}

void loop() {
  // put your main code here, to run repeatedly:
  float tempObj = 15;
  float tempHys = 0.75;
  float tempValue = 0;

  monitorizar_temperatura (tempValue, tempHys, tempObj);

  float tension_limitada = 10;
  float voltage = 0;

  ups(voltage, tension_limitada);

  if ( monitorizar_temperatura (tempValue, tempHys, tempObj) && ups(voltage, 
tension_limitada))
  {
    Serial.println(" ");
    Serial.println("Introducir el importe");
    importe_introducido(&importe);
    Serial.println(" ");
    Serial.println("importe");
    Serial.print(importe);
    Serial.println("€");
    delay(1000);

    if (importe > 0) {
          float p;
          String r = " ";

      productos(p, r);
      Serial.println(" ");
      Serial.print("Has elegido ");
      Serial.println(r);
      Serial.print("precio: ");
      Serial.print(p);
      Serial.println("€");
      Serial.println(" ");
    }

  }

}

// para monitorizar la temperatura
bool monitorizar_temperatura (float x, float y, float z) {

  float sensorValue = 0;
  sensorValue = analogRead(SENSOR_TEMPERATURA); // read the input on analog pin 0
  x = mapFloat(sensorValue, 0, 1023, -10, 40); // from  0-1023 convert to -10- 40
  bool tempOK;

  float tempMax = z + y;
  float tempMin = z - y;
  float tempError = 10.0;
  float ledRef = digitalRead(LED_REFRIGERACION);

  Serial.println("tempvalue");
  Serial.println(x);

  if (x < tempMin ) { // if temperature below 14.25 turn off refrigeracion
    if (ledRef == HIGH) {
      Serial.println(" apaga la luz azul ");
      digitalWrite(LED_REFRIGERACION, LOW); // turn the LED de refrigeracion high
      digitalWrite(LED_ERROR, LOW);
      tempOK = true;
    }
    previousMillis = millis();
  }
  if (x >= tempMax && x < z + tempError) { // if temperature above 15.75 turn on 
refrigeracion
    if (ledRef == LOW) {
      Serial.println(" enciende la luz de refrigeracion ");
      digitalWrite(LED_REFRIGERACION, HIGH); // turn the LED on (HIGH is the voltage 
level)
      digitalWrite(LED_ERROR, LOW);
      tempOK = true;
    }
    previousMillis = millis();
  }

  if (x >= z + tempError) {
    if (millis() - previousMillis >= 6000) {
      Serial.println(" maquina está en temperatura alta ");
      digitalWrite(LED_REFRIGERACION, LOW);
      digitalWrite(LED_ERROR, HIGH);
      delay (1000);
      digitalWrite(LED_ERROR, LOW);
      delay (1000);
      tempOK = false;
    }
  }
  return tempOK;
}


// UPS
bool ups( float a, float b ) {

  float valorTension = 0;
  valorTension = analogRead (UPS);
  a = mapFloat (valorTension, 0, 1023, 0, 12);
  bool tensionOK;

  Serial.println("voltage");
  Serial.println(a);

  if (a <= b) {
    if (millis() - previousMillis >= 500) {
      Serial.println(" para la maquina ");
      digitalWrite (LED_ERROR, HIGH);
      delay (1000);
      digitalWrite (LED_ERROR, LOW);
      delay (5000);
      tensionOK = false;
    }
  }

  if (a > b) {
    Serial.println(" introducir el importe ");
    previousMillis = millis();
    tensionOK = true;
  }
  return tensionOK;
}

// importe introducido
void importe_introducido(float * c) {
  static unsigned long previousMillis = 0;
  if (digitalRead(BUTTON_IMPORTE) == LOW) {
    if (millis() - previousMillis >= 500)
    {
      *c += 0.05; // 5cents every 500ms button pushed
      previousMillis = millis();
    }
  }
}

// elegir el producto
void productos (float &precio, String &refresco)
{
  char key = keypad.getKey();
  if (key) {
    Serial.print(key);
    input_codigo += key;
    if (input_codigo.length() == 3) {
      bool isValid = true;
      if (input_codigo == bebidas[0]) {
        precio = 1.5;
        refresco = "un refreco de cola";
      }
      else if (input_codigo == bebidas[1]) {
        precio = 1.5;
        refresco = "un refreco de naranja";
      }
      else if (input_codigo == bebidas[2]) {
        precio = 1.5;
        refresco = "un refreco de limón";
      }
      else if (input_codigo == bebidas[3]) {
        precio = 2.5;
        refresco = "un zumo";
      }
      else if (input_codigo == bebidas[4]) {
        precio = 1.25;
        refresco = "un agua con gas";
      }
      else if (input_codigo == bebidas[5]) {
        precio = 1.0;
        refresco = "un agua mineral";
      }
      else {
        isValid = false;
        precio = 0.0;
        refresco = ("el codigo no es valido");
      }

      if ( isValid == true ) {
      }
      input_codigo = ""; // clear input password
    }
  }
}

I know to run all of this code is a little bit complicated and time consuming :joy:. Perhaps anyone can tell me why the value ‘ovf’ appear and how can I eliminate it. Thanks !