/**************************
File: sketch.ino
Title: "Ejemplo de Codigo BASICO en lenguaje C (CLUB DE ROBOTICA)"
Author: Alfredo Chacon
Date: 25/09/2024
Notes:
Ejemplo del curso de programacion para el CLUB DE ROBOTICA.
En este ejemplo podemos ver como esta compuesto un programa tipico en lenguaje C.
Incluye: Librerias, Constantes, Variables Globales y locales, Ciclos o bucles y condicionantes.
***************************/
/* DEFINION DE CONSTANTES*/
#define PI 3.1416
#define OPCION0 // <--CAMBIA LA OPCION PARA SELECCIONAR EL CODIGO A COMPILAR.
/* DECLARACION Y/O DEFINICION DE VARIABLES GLOBALES */
int x,y;
char a;
float b = 5.2;
int incomingByte = 0; // for incoming serial data
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
/* DECLARACION Y/O DEFINICION DE VARIABLES LOCALES */
float c = PI;
int i,n;
/* CICLO FOR */
for(i=0;i<5;i++){
/* CONDICION IF */
if(i<3){
Serial.println("Hello, ESP32!");
}
else{
Serial.print("El valor de b es:");
Serial.println(b);
Serial.print("El valor de c es:");
Serial.println(c);
}
}
Serial.println("Introduce el texto -->\"Hola mundo\" y da enter");
}
void loop() {
// put your main code here, to run repeatedly:
int i,n;
#ifdef OPCION0
// reply only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read(); // Se lee un byte del serial y se guarda en incomingByte
// say what you got:
Serial.print("Byte recibido: ");
Serial.println(incomingByte, DEC); //Se imprime la variable incomingByte en formato Decimal.
}
#endif
#ifdef OPCION1
// reply only when you receive data:
if (Serial.available() > 0) {
// read the incoming string:
String cadena = Serial.readString(); //Se declara la variable cadena y se guarda el string leido.
// say what you got:
Serial.println(cadena); //Imprime lo que contenga cadena.
}
#endif
#ifdef OPCION2
while(Serial.available()){
size_t byteslength = Serial.available(); //retorna la cantidad de bytes
Serial.print("Numero de bytes disponibles en serial:");
Serial.println(byteslength); //imprime el numero de bytes disponibles en el serial
uint8_t buffer[byteslength]; //crea un arreglo de esa cantidad como buffer, Nota: no es recomendable.
int leidos= Serial.read(buffer, byteslength); // Lee hasta un maximo de byteslength, los pasa al buffer y retorna la cuenta de bytes leidos.
Serial.print("Y ahora numero de bytes leidos:");
Serial.println(leidos); //imprime el numero de bytes leidos
Serial.write(buffer,byteslength); //escribe(imprime) todos esos bytes del buffer.
for(i=0;i<leidos;i++){
Serial.println(buffer[i],HEX); //Imprime los bytes recibidos en hexadecimal.
}
}
#endif
#ifdef OPCION3
if (Serial.available() > 0) {
String entrada = Serial.readStringUntil('\n');
if(entrada == "1"){
Serial.println("Mandaste un uno ");
} else if (entrada == "0") {
Serial.println("Mandaste un cero");
} else {
Serial.print("Cadena de caracteres leida: ");
Serial.println(entrada);
}
}
#endif
delay(10); // this speeds up the simulation
}