-7

I want to create a list who contains a number undefined of reference composed of 8 numbers:

Example:

28546455
95685615
55925555
59295525
etc.......

These number can be entered via a keypad.

Each reference contains 2 parameters:

  1. speed
  2. steps

Example:

28546455 --> speed : 500
--> steps : 10000

95685615 --> speed : 300
-->steps : 20000

etc..........

I made this program, but it doesn't seem to work? I can't get pass the first variable entry which is the reference number.
Any help?

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

char keypad_referenceNumber[9];
char keypad_SPEED[4];
char keypad_STEPS[6];

int counter = 0;

const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
    { '1', '2', '3' },
    { '4', '5', '6' },
    { '7', '8', '9' },
    { '*', '0', '#' }
};

byte rowPins[ROWS] = { 8, 7, 6, 5 };
byte colPins[COLS] = { 4, 3, 2 };

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

int data_referenceNumber;
int data_SPEED;
int data_STEPS;

List* myList;

void setup()
{
    // put your setup code here, to run once:

    Serial.begin(9600);

    // Initialize the first element of the list
    List* myList = initialisation();
}

void loop()
{
    // put your main code here, to run repeatedly:

    // get from the keypad the reference number
    getreferenceNumber();
    // get from the keypad the speed
    getSPEED();
    // get from the keypad the steps
    getSTEPS();
    // Add a new reference to the list
    addNewReference(myList, data_referenceNumber, data_SPEED, data_STEPS);
    // Display the list of references
    displayList(myList);

} // END LOOP
// function to get the reference number
void getreferenceNumber()
{

    char key = keypad.getKey();

    if (key != NO_KEY) {

        keypad_referenceNumber[counter++] = key;
        keypad_referenceNumber[counter] = '\0';

        Serial.print(key);

        if (key == '#') {

            data_referenceNumber = atoi(keypad_referenceNumber);

            Serial.println(data_referenceNumber);

            counter = 0;
        }
    }
}
//function to get speed value from keypad
void getSPEED()
{

    char key = keypad.getKey();
    if (key != NO_KEY) {
        keypad_SPEED[counter++] = key;
        keypad_SPEED[counter] = '\0';

        if (key == '#') {
            data_SPEED = atoi(keypad_SPEED);
            Serial.println(data_SPEED);
            counter = 0;
        }
    }
}
// function to get the steps value from keypad
void getSTEPS()
{
    char key = keypad.getKey();
    if (key != NO_KEY) {
        keypad_STEPS[counter++] = key;
        keypad_STEPS[counter] = '\0';

        if (key == '#') {
            data_STEPS = atoi(keypad_STEPS);
            Serial.println(data_STEPS);
            counter = 0;
        }
    }
}

//function to initialize the first element of the list
List* initialisation()
{
    List* list;
    References* references;

    if (list == NULL || references == NULL) {
        exit(EXIT_FAILURE);
    }

    references->referenceNumber = 0;
    references->SPEED = 0;
    references->STEPS = 0;

    references->next = NULL;
    list->firstElement = references;

    return list;
};
// Function to add a new reference
// x : reference number
// y : speed
// z : steps
void addNewReference(List* list, int x, int y, int z)
{
    References* newReference;

    if (list == NULL || newReference == NULL) {
        exit(EXIT_FAILURE);
    }
    newReference->referenceNumber = x;
    newReference->SPEED = y;
    newReference->STEPS = z;

    newReference->next = list->firstElement;

    list->firstElement = newReference;
}
// function to display to list of references
void displayList(List* list)
{
    if (list == NULL) {
        exit(EXIT_FAILURE);
    }
    References* current = list->firstElement;

    while (current != NULL) {
        Serial.println(current->referenceNumber);
        Serial.println(current->SPEED);
        Serial.println(current->STEPS);

        current = current->next;
    }
    Serial.println("NULL");
}

This is the Reference.h

#ifndef _REFERENCE_H_
#define _REFERENCE_H_

typedef struct References References;
struct References { 
    int referenceNumber;
    int SPEED;
    int STEPS;  
    References *next;    
};

typedef struct List List;
struct List {
    References *firstElement; 
};

#endif
Community
  • 1
  • 1
Sef13
  • 1
  • 2
  • 2
    That code is *very* difficult to read as the tabbing is basically random. Furthermore, you say it doesn't work, but how doesn't it work? Please describe your problem more fully otherwise people will just skip over the rather lengthy code sample; in addition, try getting rid of unnecessary code to create a [MCVE]. – Ken Y-N Jun 11 '18 at 08:28
  • A quick look through tells me you never allocate any memory, so you're not going to be able to build a linked list. Second, you need to compile your code with more warnings enabled, such as `-Wall`, as you use uninitialised variables, for instance. – Ken Y-N Jun 11 '18 at 08:34
  • i created the program to get values from the keypad, when i use it alone it works but when i added to the program to get more values from the keypad it doesn't pass the first variable. what's -Wall? and how do i allocate memory? – Sef13 Jun 11 '18 at 08:48
  • @KenY-N Arduino is an embedded microcontroller system, so you don't use allocated memory at all. Linked lists on such systems use static storage duration variables. – Lundin Jun 11 '18 at 09:23

1 Answers1

-1

From what I understand you want memory that won’t be erased every time the Arduino is off. Do some research into EEPROM.

In terms of debugging, the code you have there is a bit difficult to read. Try inserting statements which print the value of a certain variable as the program progresses and see where it goes wrong.

Best of luck! Tell us what happens.

GoopieGoogpie
  • 71
  • 1
  • 9