Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to create a list who contains a number undefined of reference composed of 8 numbers :

exemple :

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??
any help??

What I have tried:

<pre>#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);
List *myList = initialisation();
}

void loop() {
  // put your main code here, to run repeatedly:
  
 getreferenceNumber();
 getSPEED();
 getSTEPS(); 
  
 addNewReference(myList,data_referenceNumber,data_SPEED,data_STEPS);
 displayList(myList);

}
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;
    }
  }
}

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;
    }
  } 
}

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;
    }
  }
}

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;
};

// 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;   
}

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");
}



and 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
Posted
Updated 10-Jun-18 23:32pm

1 solution

Avoid using global variables. It is hard to track what values are actually stored in them and are a possible source for errors.

If a program is not working as expected, you have to use a debugger to step through your code and watch the variables. If this is not possible on your Arduino, write the application first on a PC to test and debug it (reading input then from the keyboard).

You have a design / logic error:
Reading input consisting of multiple characters must be performed within a loop but you don't have such in your input routines.

It would be also simpler to write a function that reads a numeric input and returns that for all three values instead of providing three functions which would basically do the same:
C
int get_num_from_keypad()
{
    int count = 0;
    int value = 0;
    char key;
    char buf[9];
    
    key = keypad.getKey();
    while (key != NO_KEY)
    {
        Serial.print(key);
        if (key == '#')
        {
            buf[count] = '\0';
            value = atoi(buf);
            break;
        }
        buf[count++] = key;
        /* EDIT: Forgot this */
        key = keypad.getKey();
    }
    return value;
}

Read about linked lists before trying to use them. They have to use dynamic memory allocation using malloc() or calloc().

If you have a known maximum number of data to be entered, you can even avoid using a linked list and instead use an array of fixed size and a variable holding the number of recordsets:
C++
int ref_count = 0;
References refs[MAX_REFS];

/* ... */

void loop()
{
    if (ref_count < MAX_REFS)
    {
        refs[ref_count].referenceNumber = get_num_from_keypad();
        refs[ref_count].SPEED = get_num_from_keypad();
        refs[ref_count].STEPS = get_num_from_keypad();
        ref_count++;
    }
}
To display this list simply loop up to but excluding ref_count.

Note also that all upper case names should not be used for variables. It is recommended to use those exclusively for preprocessor definitions.

[EDIT]
Quote:
since there is no debugger in arduino can you recommand me one i can use

for the memory allocation, i don't know how to work with the arduino memory so i'm gonna test the array you proposed

As already suggested you can develop your program on a PC first using getchar() for the input and printf() for the output.

You are using C which provides the already mentioned malloc() and calloc() standard library functions. Just use them like on any other platform (besides the fact that the Arduino has not much memory and they will fail when requesting too much memory).
[/EDIT]
 
Share this answer
 
v3
Comments
Member 13867267 11-Jun-18 6:29am    
this is my first time using this platform, i will be more careful
thank you for your answers they're really useful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900