简体   繁体   中英

Error when asking for numbers on Arduino Uno

my name is Jan, it turns out that when I put a number like 200, it shows me 2 on the console, then a 0 and then another 0

This is my code:

const int ledPin = 3 ;
const int rePin = 2 ; 
const int puPin = 1 ; 
int pulso = 0; 
int input; 

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  Serial.begin(9600) ; 
  pinMode(ledPin, OUTPUT);
  pinMode(rePin, OUTPUT);
  pinMode(puPin, INPUT) ; 
   

}

// the loop function runs over and over again forever
void loop() {


if (Serial.available()) {
    char pulso = Serial.read(); //To introduce the pulse
    Serial.print("Numero de pulsaciones: ");
    Serial.print(pulso);

This is the error: If I put 200

What I want is for an input to appear on the console that requests a certain number of pulses, then when I put them, the number of pulses that I have put appears on the console and then if the pulses are less than 35, for example, give a discharge and a led is activated.

here is the correct code, you just need to configure the serial monitor to send the both NL/CR.

const int ledPin = 3 ;
            const int rePin = 2 ; 
            const int puPin = 1 ; 
            int pulso = 0; 
            int input; 
            bool mensajeRecibido = false;
            String mensajeCompleto;


            void setup() {
              // initialize digital pin LED_BUILTIN as an output.
              Serial.begin(9600) ; 
              pinMode(ledPin, OUTPUT);
              pinMode(rePin, OUTPUT);
              pinMode(puPin, INPUT) ; 
              mensajeCompleto.reserve(50); //Se reciben maximo hasta 50 bytes
              mensajeCompleto = ""; 

            }

            // the loop function runs over and over again forever
            void loop() {


            if (Serial.available()) {
                char byteSerial = Serial.read(); //To introduce the pulse

                if(byteSerial == '\n'){
                
                  int pulso = mensajeCompleto.toInt();
                  Serial.print("Numero de pulsaciones: ");
                  Serial.print(pulso);
                  mensajeCompleto = "";

                  
                }
                else{
                  mensajeCompleto = mensajeCompleto + byteSerial;

                }
                
                
                

            }

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM