Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
769 views
in Technique[技术] by (71.8m points)

arduino - C: variables retain the value from previous operation instead of resetting

I am fairly new to C and have been trying my hand with some arduino projects on Proteus. I recently tried implementing a keypad and LCD interface with Peter Fleury's libraries, so far the characters I input are displayed fine, but I run into a problem when trying to print to the serial port. It's like the value of the keys keeps on being concatenated with every iteration so the ouput has extra characters like this:

The value before the comma is from the 'key' variable, the value after it the 'buf' variable:

Sample iterations

151 (The 5 I input in the second iteration was added to the 1 from the first iteration and then put into the variable I print)

I figure it may be due to my lack/incorrect use of pointers, heres is my code:

#include <avr/io.h>
#include <util/delay.h>

#include <stdlib.h>
#include <stdio.h>

#include "lcd.h"
#include "mat_kbrd.h"
#include "funciones.h"
#include "menu.h"

char buf[256];
char* coma = ","; 

int main(void)
{
    pin_init();
    serial_begin();
    lcd_init(LCD_DISP_ON);
    kbrd_init();
   
    bienvenida();
   
    while (1) {
        int i = 0;
        char key = 0;
      
        //char *peso;
        //int pesoSize = 1;
        char peso[100];
        //peso = calloc(pesoSize,sizeof(char));  
        int salida = 0;
         
        lcd_clrscr();
        desechos();
      
        key = kbrd_read();
     
        if (key != 0) {
            lcd_gotoxy(0,3);
            lcd_putc(key);

            _delay_ms(2000);
            lcd_clrscr();
            cantidad();

            while (salida != 1) {
                char keypeso = 0;
                keypeso = kbrd_read();
                //pesoSize = i;
                //peso = realloc(peso,pesoSize*sizeof(char));  

                if (keypeso != 0) {
                    if (keypeso == '+') {
                        salida = 1;
                        keypeso = *("");
                        lcd_clrscr();
                        calcularTotal(key,peso);
                        _delay_ms(2000);
                    } else {
                        lcd_gotoxy(i,1);
                        lcd_putc(keypeso);
                        snprintf(peso, sizeof peso, "%s%s",peso, &keypeso);
                        //strcat(peso,&keypeso);
                  
                        i++;
                        _delay_ms(2000);
                    }
                }
            }
        
            snprintf(buf, sizeof buf, "%s%s%s", &key,coma,peso);
            serial_println_str(buf); 
        }
    }
}
question from:https://stackoverflow.com/questions/65873675/c-variables-retain-the-value-from-previous-operation-instead-of-resetting

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

&key and &keypeso point to a single char, but you are using the %s format specifier, so trying to read a string into a single char. Use %c rather then %s for single characters, and pass the char not the address-of-char..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...