BASIC4MCU | 아두이노 | 아두이노 | 아두이노 Simple 계산기
페이지 정보
작성자 master 작성일2018-03-17 17:49 조회8,563회 댓글0건본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2018-03-17 오후 5:54:01// by Ok-Hyun Park#define op_Plus 0#define op_Minus 1#define op_Div 2#define op_Mul 3//int SWa=2,SWb=3,SWop=4,SWequ=5;int a=0,b=0,op=0,result=0;//void setup(){pinMode(SWa,INPUT_PULLUP);pinMode(SWb,INPUT_PULLUP);Serial.begin(38400);}//void loop(){if(digitalRead(SWa)==LOW){a++; Serial.print("a="); Serial.println(a);delay(50); while(digitalRead(SWa)==LOW); delay(50); //채터링, 디바운스}if(digitalRead(SWb)==LOW){b++; Serial.print("b="); Serial.println(b);delay(50); while(digitalRead(SWb)==LOW); delay(50); //채터링, 디바운스}if(digitalRead(SWop)==LOW){if(++op>3)op=0; Serial.print("op=");switch(op){case op_Plus : result=a+b; Serial.println("+"); break;case op_Minus: result=a-b; Serial.println("-"); break;case op_Div : result=a*b; Serial.println("*"); break;case op_Mul : result=a/b; Serial.println("/"); break;}delay(50); while(digitalRead(SWop)==LOW); delay(50); //채터링, 디바운스}if(digitalRead(SWequ)==LOW){Serial.print(a);switch(op){case op_Plus : result=a+b; Serial.print("+"); break;case op_Minus: result=a-b; Serial.print("-"); break;case op_Div : result=a*b; Serial.print("*"); break;case op_Mul : result=a/b; Serial.print("/"); break;}//Serial.print(b);Serial.print("=");Serial.println(result);delay(50); while(digitalRead(SWequ)==LOW); delay(50); //채터링, 디바운스}}
나눗셈 경우 정수가 아닌 실수값이 나올 수 있으니
result=0;
이 변수는 float로 선언해도 되겠습니다.
댓글 0
조회수 8,563등록된 댓글이 없습니다.