Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <multiprotocolShield.h>
- #include <MCP23008.h>
- #include <Wire.h>
- # define INACTIVO 0
- # define ACTIVO 1
- # define RESET 2
- # define BUFFER_SIZE 750
- int index = 0;
- char buffer[BUFFER_SIZE];
- struct Sistema {
- byte estado;
- char operando;
- int primero;
- int segundo;
- int resultado;
- int contador;
- };
- void operar(struct Sistema &sist){
- switch (sist.operando){
- case '*':
- sist.resultado = sist.primero * sist.segundo;
- break;
- case '/':
- sist.resultado = sist.primero / sist.segundo;
- break;
- case '+':
- sist.resultado = sist.primero + sist.segundo;
- break;
- case '-':
- sist.resultado = sist.primero - sist.segundo;
- break;
- }
- Serial.println(sist.resultado);
- }
- void setup_sistema(struct Sistema &sist){
- sist.estado = INACTIVO;
- sist.contador = 0;
- }
- void loop_sistema(struct Sistema &sist, char &c){
- switch(sist.estado){
- case INACTIVO:
- if(c == '#'){
- sist.estado = ACTIVO;
- Serial.println("Introduce la operacion");
- } else {
- Serial.println("INCORRECTO");
- }
- break;
- case ACTIVO:
- if(sist.contador == 0){
- if(c == '*' || c == '/' || c == '+' || c == '-'){
- sist.operando = c;
- sist.contador++;
- Serial.println("Introduce el primer numero");
- } else {
- Serial.println("INCORRECTO");
- }
- } else if (sist.contador == 1){
- sist.primero = c - '0';
- sist.contador++;
- Serial.println("Introduce el segundo numero");
- } else if(sist.contador == 2){
- sist.segundo = c - '0';
- sist.estado = RESET;
- operar(sist);
- sist.contador = 0;
- sist.estado = INACTIVO;
- }
- break;
- }
- }
- //---------------------------------------------
- Sistema sist;
- void setup() {
- pinMode(13, OUTPUT);
- Serial.begin(115200);
- delay(1000);
- Serial.setTimeout(30);
- socket0.ON();
- delay(100);
- socket0.setMUX();
- delay(100);
- setup_sistema(sist);
- // ----------------------------------------------------------------
- Serial.println("SET BT PAGEMODE 4");
- }
- void loop() {
- char c;
- while(Serial.available()== 0){
- Serial.println("Esperando...");
- delay(5000);
- }
- c = Serial.read();
- loop_sistema(sist, c);
- }
- void printSerialInput(int seconds){
- int counter = seconds * 1000 / 10;
- while( counter > 0 ){
- if( Serial.available() ){
- while(Serial.available() > 0) {
- int incomingByte = Serial.read();
- buffer[index] = (char)incomingByte;
- index++;
- }
- }
- delay(10);
- counter--;
- }
- if( index != 0 ){
- flushToSerialDebug( buffer, index );
- index = 0;
- }
- }
- void flushToSerialDebug(char* buffer, int len){
- socket1.ON();
- delay(100);
- socket1.setMUX();
- Serial.write(buffer, len);
- delay(100);
- socket0.ON();
- delay(100);
- socket0.setMUX();
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement