How to make Botton increase number
การเขียนโปรแกรม การทำปุ่มเพิ่มตัวเลข
วีดิโอสาธิตปุ่มกดเพิ่มตัวเลข
ผังวงจรปุ่มกดเพิ่มตัวเลข
//กำหนดตัวแปรตัวนับ digit
#define a 2
#define b 3
#define c 4
#define d 5
#define e 6
#define f 7
#define g 8
#define push_plus 9
#define push_minus 10
int digit_sum = 0;
int bottonstate1=0;
int bottonstate0=0;
int lastbottonstate1=0;
int lastbottonstate0=0;
void setup() //โปรดกดติดตาม กดแจ้งเตือน Facebook @เรียนคอมบาทเดียว , Youtube Chanel corecomcenter th
{
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(push_plus, INPUT_PULLUP);
pinMode(push_minus, INPUT_PULLUP);
Serial.begin(9600);
digit_count(digit_sum);
Serial.println(digit_sum);
}
//coding by corecomcenter th
void loop()
{
bottonstate1=digitalRead(push_plus);
bottonstate0=digitalRead(push_minus);
if(bottonstate1 != lastbottonstate1)
{
if(bottonstate1==HIGH)
{
if(digit_sum !=9)
{
digit_sum++;
digit_count(digit_sum);
Serial.println(digit_sum);
}else
{
digit_sum=digit_sum;
}
}else
{
digit_sum=digit_sum;
}
delay(200);
}
lastbottonstate1=bottonstate1;
if(bottonstate0 != lastbottonstate0)
{
if(bottonstate0 ==HIGH)
{
if(digit_sum !=0)
{
digit_sum--;
digit_count(digit_sum);
Serial.println(digit_sum);
}else
{
digit_sum=digit_sum;
}
}else//coding by corecomcenter th
{
digit_sum=digit_sum;
}
delay(200);
}
lastbottonstate0=bottonstate0;
}
void digit_count(int digit_led)
{
if(digit_led >=0 && digit_led <=9)
{
switch(digit_led)
{
case 9:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;//coding by corecomcenter th
case 8:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;
case 7://coding by corecomcenter th
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);//coding by corecomcenter th
digitalWrite(g, LOW);
break;
case 6:
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;
case 5:
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;
case 4:
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;
case 3:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
break;//coding by corecomcenter th
case 2:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
break;
case 1:
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
case 0:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
break;
}
}
else
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
}//coding by corecomcenter th