Hello,
I am going to use serial port and read different numbers, but this code only works for numbers below 10 (for example: '5')
void setup() {
// initialize serial communication:
Serial.begin(9600);
int inByte = 0;
}
void loop() {
if (Serial.available()>0) {
int inByte = Serial.read();
if (inByte == 30) {
Serial.print("Hello");
}
}
}
Do you have a question?
Please remember to use code tags when posting code
Hints:
A character are things like A, 1, and 3 and can be stored in ONE byte.
Words are like 10 and APPLE which CANNOT be stored in one byte.
To make a word you'd want to combine all your characters.
There is some serial basics write ups on this site, use search, that you'd want to read up on.
serial basics <<< a link!! clicky clicky. A link to serial basics thingy.
smhmehrdad20:
void setup()
{
// initialize serial communication:
Serial.begin(9600);
int inByte = 0; ////// This does nothing, because...
}
void loop()
{
if (Serial.available() > 0)
{
int inByte = Serial.read(); ///// ...this is a separate variable.
if (inByte == 30) ///// Say '0' instead of 30
{
Serial.print("Hello");
}
}
}
The name 'inByte' seems to imply that the variable should be a 'byte' instead of an 'int'. I would make it a 'char' instead, since that is the natural type for a variable that holds a character.
void setup()
{
// initialize serial communication:
Serial.begin(9600);
Serial.println();
Serial.println("Started.");
}
void loop()
{
if (Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == '0')
{
Serial.println("Hello");
}
}
}
Yes, the code doesn't work, I need to send "30" but this code works only for numbers below 9.
smhmehrdad20:
Thank you very much.
But I need to send other numbers, 10, 11 and so on
Thank you very much.
But I need to send other numbers, 10, 11 and so on
Won't look at the how to do the serial thingy. <<<< That's the easy button.
Here is a function that receives bytes from the serial and forms it into words.
void fReceiveSerial_LIDAR( void * parameters )
{
bool BeginSentence = false;
char OneChar;
char *str;
str = (char *)ps_calloc(300, sizeof(char) ); // put str buffer into PSRAM
// log_i("Free PSRAM before String: %d", ESP.getFreePsram());
for ( ;; )
{
EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
if ( LIDARSerial.available() >= 1 )
{
while ( LIDARSerial.available() )
{
OneChar = LIDARSerial.read();
if ( BeginSentence )
{
if ( OneChar == '>')
{
if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
{
xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &str );
xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
//
}
BeginSentence = false;
break;
}
strncat( str, &OneChar, 1 );
}
else
{
if ( OneChar == '<' )
{
strcpy( str, ""); // clear string buffer
BeginSentence = true; // found beginning of sentence
}
}
} // while ( LIDARSerial.available() )
} //if ( LIDARSerial.available() >= 1 )
xSemaphoreGive( sema_ReceiveSerial_LIDAR );
// log_i( "fReceiveSerial_LIDAR " );
// log_i(uxTaskGetStackHighWaterMark( NULL ));
}
free(str);
vTaskDelete( NULL );
} //void fParseSerial( void * parameters )
"Serial" has a method ".parseInt()" for when you are expecting a multi-digit number:
void loop()
{
if (Serial.available()) // means "if Serial.available() is not equal to zero"
{
int inNumber = Serial.parseInt();
if (inNumber == 30)
{
Serial.print("Hello");
}
}
}
system
Closed
January 19, 2022, 10:32pm
10
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.