如何获取值并保存它们以便以后在Arduino上使用?

时间:2018-06-11 08:21:37

标签: c++ arduino keypad

我想创建一个列表,其中包含由8个数字组成的未定义引用的数字:

示例:

  

28546455
     95685615个
     55925555个
     59295525个
     等等......

可以通过键盘输入这些号码。

每个参考包含2个参数:

  1. 速度
  2. 步骤
  3. 示例:

      

    28546455 - >速度:500
       - >步骤:10000

         

    95685615 - >速度:300
       - >步骤:20000

         

    等..........

    我做了这个程序,但它似乎没有用?我无法通过第一个变量条目,即参考号 有什么帮助吗?

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

    这是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
    

1 个答案:

答案 0 :(得分:-1)

根据我的理解,你想要每次Arduino关闭时都不会删除的内存。对 EEPROM 进行一些研究。

在调试方面,您所拥有的代码有点难以阅读。尝试插入在程序进行时打印某个变量值的语句,并查看出错的位置。

祝你好运!告诉我们会发生什么。