win10專業版或以上才有遠端桌面連線的功能。
2020年2月15日 星期六
2020年2月14日 星期五
秀出左2順/逆時針 右2秀出計數器
char seg7Table1[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
char lineTable2[] = {0x81, 0x00, 0x00, 0x00, 0x00, 0x88, 0x90, 0xa0};char lineTable3[] = {0x00, 0x81, 0x82, 0x84, 0x88, 0x00, 0x00, 0x00};
char seg7Pin[] = {2, 3, 4, 5, 6, 7, 8};
char comPin[] = {9, 10, 11, 12};
char sw1 = 14;
char sw2 = 15;
int number1;
int number2;
int counter1 = 0;
int counter2 = 0;
int add1 = 1;
int add2 = 1;
unsigned long time = 0;
int i;
int j;
void setup()
{
int i;
for (i = 0; i < 7; i++)
{
pinMode(seg7Pin[i], OUTPUT);
// digitalWrite(seg7Pin[i], LOW);
}
for (i = 0; i < 4; i++)
{
pinMode(comPin[i], OUTPUT);
digitalWrite(comPin[i], LOW);
}
pinMode(sw1, INPUT);
pinMode(sw2, INPUT);
}
void loop()
{
char val1 = digitalRead(sw1);
if (val1 == 0)
{
delay(20);
while (digitalRead(sw1) == LOW);
add1++;
}
//========================================
char val2 = digitalRead(sw2);
if (val2 == 0)
{
delay(20);
while (digitalRead(sw2) == LOW);
add2++;
}
//========================================
number1 = counter1;
for (j = 0; j < 7; j++)//先秀出個位數
{
if (bitRead(~seg7Table1[number1 % 10], j))
digitalWrite(seg7Pin[j], HIGH);
else
digitalWrite(seg7Pin[j], LOW);
}
digitalWrite(comPin[0], HIGH); delay(5);
digitalWrite(comPin[0], LOW);
number1 = number1 / 10;//除10秀出十位數
for (j = 0; j < 7; j++)
{
if (bitRead(~seg7Table1[number1 % 10], j))
digitalWrite(seg7Pin[j], HIGH);
else
digitalWrite(seg7Pin[j], LOW);
}
digitalWrite(comPin[1], HIGH); delay(5);
digitalWrite(comPin[1], LOW);
if (millis() - time >= 1000)
{
time = millis();
if (add1 % 2 == 1 )
{
counter1++;
if (counter1 > 99)counter1 = 0;
}
else
{
counter1--;
if (counter1 < 0)counter1 = 99;
}
}
//=================================================
number2 = counter2;
for (j = 0; j < 7; j++)
{
if (bitRead(lineTable2[number2 % 8], j))
digitalWrite(seg7Pin[j], HIGH);
else
digitalWrite(seg7Pin[j], LOW);
}
digitalWrite(comPin[3], HIGH); delay(5);
digitalWrite(comPin[3], LOW);
for (j = 0; j < 7; j++)
{
if (bitRead(lineTable3[number2 % 8], j))
digitalWrite(seg7Pin[j], HIGH);
else
digitalWrite(seg7Pin[j], LOW);
}
digitalWrite(comPin[2], HIGH); delay(5);
digitalWrite(comPin[2], LOW);
if (millis() - time >= 1000)
{
time = millis();
if (add2 % 2 == 1 )
{
counter2++;
if (counter2 > 7)counter2 = 0;
}
else
{
counter2--;
if (counter2 < 0)counter2 = 7;
}
}
}
2020年2月13日 星期四
pili燈+sw1 sw2 sw3 sw4
char piliTable1[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
char piliTable2[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
char piliTable3[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40};
char piliTable4[] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
char piliPin[] = {2, 3, 4, 5, 6, 7, 8, 9};
char sw1 = 10;
char sw2 = 11;
char sw3 = 12;
char sw4 = 13;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int number1;
int number2;
int number3;
int number4;
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned long time3 = 0;
unsigned long time4 = 0;
int state1 = 0;
int state2 = 0;
int state3 = 0;
int state4 = 0;
void setup()
{
int i;
for (i = 0; i < 8; i++)
{
pinMode(piliPin[i], OUTPUT);
digitalWrite(piliPin[i], LOW);
}
digitalWrite(sw1, INPUT);
digitalWrite(sw2, INPUT);
digitalWrite(sw3, INPUT);
digitalWrite(sw4, INPUT);
}
void loop()
{
int val1;
int val2;
int val3;
int val4;
int i;
val1 = digitalRead(sw1);
{
if (val1 == 0)
{
delay(20);
while (digitalRead(sw1) == LOW);
state1 = 1;
state2 = 0;
state3 = 0;
state4 = 0;
}
}
//=========================================
val2 = digitalRead(sw2);
{
if (val2 == 0)
{
delay(20);
while (digitalRead(sw2) == LOW);
state1 = 0;
state2 = 1;
state3 = 0;
state4 = 0;
}
}
//============================================
val3 = digitalRead(sw3);
{
if (val3 == 0)
{
delay(20);
while (digitalRead(sw3) == LOW);
state1 = 0;
state2 = 0;
state3 = 1;
state4 = 0;
}
}
//============================================
val4 = digitalRead(sw4);
{
if (val4 == 0)
{
delay(20);
while (digitalRead(sw4) == LOW);
state1 = 0;
state2 = 0;
state3 = 0;
state4 = 1;
}
}
//===============================================
if (state1 == 1 && state2 == 0 && state3 == 0 && state4 == 0)
{
number1 = counter1;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable1[number1 % 8], i))
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time1 >= 500)
{
time1 = millis();
counter1++;
if (counter1 > 7)counter1 = 0;
}
}
//============================================
if (state1 == 0 && state2 == 1 && state3 == 0 && state4 == 0)
{
number2 = counter2;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable2[number2 % 8], i))
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time2 >= 500)
{
time2 = millis();
counter2++;
if (counter2 > 7)counter2 = 0;
}
}
//============================================
if (state1 == 0 && state2 == 0 && state3 == 1 && state4 == 0)
{
number3 = counter3;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable3[number3 % 13], i))//有13個字型,所以要取餘數13,否則會停在第一格
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time3 >= 500)
{
time3 = millis();
counter3++;
if (counter3 > 13)counter3 = 0;
}
}
//================================================
if (state1 == 0 && state2 == 0 && state3 == 0 && state4 == 1)
{
number4 = counter4;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable4[number4 % 8], i))//pili燈表4
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time4 >= 500)
{
time4 = millis();
counter4++;
if (counter4 > 7)counter4 = 0;
}
}
}
char piliTable2[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
char piliTable3[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40};
char piliTable4[] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
char piliPin[] = {2, 3, 4, 5, 6, 7, 8, 9};
char sw1 = 10;
char sw2 = 11;
char sw3 = 12;
char sw4 = 13;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int number1;
int number2;
int number3;
int number4;
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned long time3 = 0;
unsigned long time4 = 0;
int state1 = 0;
int state2 = 0;
int state3 = 0;
int state4 = 0;
void setup()
{
int i;
for (i = 0; i < 8; i++)
{
pinMode(piliPin[i], OUTPUT);
digitalWrite(piliPin[i], LOW);
}
digitalWrite(sw1, INPUT);
digitalWrite(sw2, INPUT);
digitalWrite(sw3, INPUT);
digitalWrite(sw4, INPUT);
}
void loop()
{
int val1;
int val2;
int val3;
int val4;
int i;
val1 = digitalRead(sw1);
{
if (val1 == 0)
{
delay(20);
while (digitalRead(sw1) == LOW);
state1 = 1;
state2 = 0;
state3 = 0;
state4 = 0;
}
}
//=========================================
val2 = digitalRead(sw2);
{
if (val2 == 0)
{
delay(20);
while (digitalRead(sw2) == LOW);
state1 = 0;
state2 = 1;
state3 = 0;
state4 = 0;
}
}
//============================================
val3 = digitalRead(sw3);
{
if (val3 == 0)
{
delay(20);
while (digitalRead(sw3) == LOW);
state1 = 0;
state2 = 0;
state3 = 1;
state4 = 0;
}
}
//============================================
val4 = digitalRead(sw4);
{
if (val4 == 0)
{
delay(20);
while (digitalRead(sw4) == LOW);
state1 = 0;
state2 = 0;
state3 = 0;
state4 = 1;
}
}
//===============================================
if (state1 == 1 && state2 == 0 && state3 == 0 && state4 == 0)
{
number1 = counter1;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable1[number1 % 8], i))
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time1 >= 500)
{
time1 = millis();
counter1++;
if (counter1 > 7)counter1 = 0;
}
}
//============================================
if (state1 == 0 && state2 == 1 && state3 == 0 && state4 == 0)
{
number2 = counter2;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable2[number2 % 8], i))
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time2 >= 500)
{
time2 = millis();
counter2++;
if (counter2 > 7)counter2 = 0;
}
}
//============================================
if (state1 == 0 && state2 == 0 && state3 == 1 && state4 == 0)
{
number3 = counter3;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable3[number3 % 13], i))//有13個字型,所以要取餘數13,否則會停在第一格
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time3 >= 500)
{
time3 = millis();
counter3++;
if (counter3 > 13)counter3 = 0;
}
}
//================================================
if (state1 == 0 && state2 == 0 && state3 == 0 && state4 == 1)
{
number4 = counter4;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable4[number4 % 8], i))//pili燈表4
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time4 >= 500)
{
time4 = millis();
counter4++;
if (counter4 > 7)counter4 = 0;
}
}
}
2020年2月12日 星期三
一顆七段顯示器上下數計器練習
//用一顆7seg接Vcc
byte seg7_table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
byte seg7pin[] = {2, 3, 4, 5, 6, 7, 8};
byte sw = 10;
byte count = 0;
byte number;
byte val=0;
byte add=0;
unsigned long time = 0;
void setup()
{
int i;
for (i = 0; i < 7; i++)
{
pinMode(seg7pin[i], OUTPUT);
}
pinMode(sw, INPUT);
}
void loop()
{
byte i;
val = digitalRead(sw);
if (val == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for (i = 0; i < 8; i++)
{
if (bitRead(~seg7_table[number % 10], i))
digitalWrite(seg7pin[i], HIGH);
else
digitalWrite(seg7pin[i], LOW);
}
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 0)
{
count++;
if (count > 9)count = 0;
}
else
{
count--;
if (count < 0)count = 9;
}
}
}
=====================================
//用七段共陰極來寫
byte seg7_table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
byte seg7pin[] = {2, 3, 4, 5, 6, 7, 8};
byte sw = 10;
int count = 0;//不可以設為byte, 否則計數有問題
int number;
byte val=0;
byte add=0;
byte com[1]={0};
unsigned long time = 0;
void setup()
{
int i;
for (i = 0; i < 7; i++)
{
pinMode(seg7pin[i], OUTPUT);
}
pinMode(sw, INPUT);
pinMode(com[0],OUTPUT);
}
void loop()
{
byte i,com_scan;
val = digitalRead(sw);
if (val == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for(com_scan=0;com_scan<1;com_scan++)//掃描的觀念寫
{
for (i = 0; i < 8; i++)
{
if (bitRead(~seg7_table[number % 10], i))
digitalWrite(seg7pin[i], HIGH);
else
digitalWrite(seg7pin[i], LOW);
}
}
digitalWrite(com[0],HIGH);delay(5);
digitalWrite(com[0],LOW);
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 0)
{
count++;
if (count > 9)count = 0;
}
else
{
count--;
if (count < 0)count = 9;
}
}
}
byte seg7_table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
byte seg7pin[] = {2, 3, 4, 5, 6, 7, 8};
byte sw = 10;
byte count = 0;
byte number;
byte val=0;
byte add=0;
unsigned long time = 0;
void setup()
{
int i;
for (i = 0; i < 7; i++)
{
pinMode(seg7pin[i], OUTPUT);
}
pinMode(sw, INPUT);
}
void loop()
{
byte i;
val = digitalRead(sw);
if (val == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for (i = 0; i < 8; i++)
{
if (bitRead(~seg7_table[number % 10], i))
digitalWrite(seg7pin[i], HIGH);
else
digitalWrite(seg7pin[i], LOW);
}
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 0)
{
count++;
if (count > 9)count = 0;
}
else
{
count--;
if (count < 0)count = 9;
}
}
}
=====================================
//用七段共陰極來寫
byte seg7_table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
byte seg7pin[] = {2, 3, 4, 5, 6, 7, 8};
byte sw = 10;
int count = 0;//不可以設為byte, 否則計數有問題
int number;
byte val=0;
byte add=0;
byte com[1]={0};
unsigned long time = 0;
void setup()
{
int i;
for (i = 0; i < 7; i++)
{
pinMode(seg7pin[i], OUTPUT);
}
pinMode(sw, INPUT);
pinMode(com[0],OUTPUT);
}
void loop()
{
byte i,com_scan;
val = digitalRead(sw);
if (val == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for(com_scan=0;com_scan<1;com_scan++)//掃描的觀念寫
{
for (i = 0; i < 8; i++)
{
if (bitRead(~seg7_table[number % 10], i))
digitalWrite(seg7pin[i], HIGH);
else
digitalWrite(seg7pin[i], LOW);
}
}
digitalWrite(com[0],HIGH);delay(5);
digitalWrite(com[0],LOW);
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 0)
{
count++;
if (count > 9)count = 0;
}
else
{
count--;
if (count < 0)count = 9;
}
}
}
pwm
byte led_pwm=3;
void setup()
{
// put your setup code here, to run once:
pinMode(led_pwm,OUTPUT);
randomSeed(analogRead(0));
Serial.begin(9600);
}
void loop()
{
int val;
analogWrite(led_pwm,val=random(135)+120);
Serial.println(val);
delay(random(100));
// put your main code here, to run repeatedly:
}
void setup()
{
// put your setup code here, to run once:
pinMode(led_pwm,OUTPUT);
randomSeed(analogRead(0));
Serial.begin(9600);
}
void loop()
{
int val;
analogWrite(led_pwm,val=random(135)+120);
Serial.println(val);
delay(random(100));
// put your main code here, to run repeatedly:
}
2020年2月11日 星期二
九宮格
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(0));
int table99[3][3]={{1,2,3},{4,5,6},{7,8,9}
};
int i,j,k=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
int rnd = rand()%(9-k)+k;
int m=rnd/3;
int n=rnd%3;
int temp =table99[i][j];
table99[i][j]=table99[m][n];
table99[m][n]=temp;
k++;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",table99[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(0));
int table99[3][3]={{1,2,3},{4,5,6},{7,8,9}
};
int i,j,k=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
int rnd = rand()%(9-k)+k;
int m=rnd/3;
int n=rnd%3;
int temp =table99[i][j];
table99[i][j]=table99[m][n];
table99[m][n]=temp;
k++;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",table99[i][j]);
}
printf("\n");
}
return 0;
}
bubble sort
#include <stdio.h>
int main()
{
int num[5],i,j;
for(i=0;i<5;i++)
{
scanf("%d",&num[i]);
}
for(i=0;i<4;i++)//總共要做4輪
{//每多做一輪就少做一次,不用做到那麼tail
for(j=0;j<4-i;j++)//每一輸比較四次
{
if(num[j]>num[j+1])
{
int temp =num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
for(i=0;i<5;i++)
{
printf("%d",num[i] );
}
return 0;
}
int main()
{
int num[5],i,j;
for(i=0;i<5;i++)
{
scanf("%d",&num[i]);
}
for(i=0;i<4;i++)//總共要做4輪
{//每多做一輪就少做一次,不用做到那麼tail
for(j=0;j<4-i;j++)//每一輸比較四次
{
if(num[j]>num[j+1])
{
int temp =num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
for(i=0;i<5;i++)
{
printf("%d",num[i] );
}
return 0;
}
swap
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int count =9;
int *countaddr = & count;
*countaddr =10;
printf("%d\n",count);
printf("%d\n",*countaddr+1);
return 0;
}
=========================
#include <stdio.h>
#include <stdlib.h>
void pass(int *a)
{
*a=*a+1;
}
int main(int argc, char *argv[])
{
int a=3;
pass(&a);
printf("%d",a);
return 0;
}
==========================
#include <stdio.h>
#include <stdlib.h>
void pass(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
int main(int argc, char *argv[])
{
int a=1,b=2;
pass(&a,&b);
printf("%d %d",a,b);
return 0;
}
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int count =9;
int *countaddr = & count;
*countaddr =10;
printf("%d\n",count);
printf("%d\n",*countaddr+1);
return 0;
}
=========================
#include <stdio.h>
#include <stdlib.h>
void pass(int *a)
{
*a=*a+1;
}
int main(int argc, char *argv[])
{
int a=3;
pass(&a);
printf("%d",a);
return 0;
}
==========================
#include <stdio.h>
#include <stdlib.h>
void pass(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
int main(int argc, char *argv[])
{
int a=1,b=2;
pass(&a,&b);
printf("%d %d",a,b);
return 0;
}
2020年2月10日 星期一
str_read
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void str_read(char str[])
{
int i=0;
while(1)
{
char ch;
scanf("%c", &str[i]) ;
if(str[i]=='\n') break;
i++;
}
str[i]='\0';
}
int main(int argc, char *argv[])
{
char str[15];
str_read(str);
printf("%s\n",str);
return 0;
}
#include <stdlib.h>
#include <string.h>
void str_read(char str[])
{
int i=0;
while(1)
{
char ch;
scanf("%c", &str[i]) ;
if(str[i]=='\n') break;
i++;
}
str[i]='\0';
}
int main(int argc, char *argv[])
{
char str[15];
str_read(str);
printf("%s\n",str);
return 0;
}
char str
#include <stdio.h>
#include <stdlib.h>
void pass(char k[])
{
int i;
for(i=0;i<5;i++)
{
printf("%c",k[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
char str[]={'H','e','l','l','o'};
pass(str);
return 0;
}
=================================
#include <stdio.h>
#include <stdlib.h>
void pass(char k[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%c",k[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
char str[]={'H','e','l','l','o'};
pass(str,5);
return 0;
}
===============================
#include <stdio.h>
#include <stdlib.h>
void pass(char k[])
{
int i;
for(i=0;k[i] !='\0';i++)
{
printf("%c",k[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
char str[]={'H','e','l','l','o','\0'};
pass(str);
return 0;
}
==============================
#include <stdio.h>
#include <stdlib.h>
void pass(char k[])
{
int i;
for(i=0;k[i] !='\0';i++)
{
printf("%c",k[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
char str[]="Hello";//進行初始化,會自動加上'\0'
pass(str);
return 0;
}
===============================
#include <stdio.h>
#include <stdlib.h>
void pass(char k[])
{
printf("%s",k);
}
int main(int argc, char *argv[])
{
char str[]="Hello";//進行初始化,會自動加上'\0'
pass(str);
return 0;
}
#include <stdlib.h>
void pass(char k[])
{
int i;
for(i=0;i<5;i++)
{
printf("%c",k[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
char str[]={'H','e','l','l','o'};
pass(str);
return 0;
}
=================================
#include <stdio.h>
#include <stdlib.h>
void pass(char k[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%c",k[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
char str[]={'H','e','l','l','o'};
pass(str,5);
return 0;
}
===============================
#include <stdio.h>
#include <stdlib.h>
void pass(char k[])
{
int i;
for(i=0;k[i] !='\0';i++)
{
printf("%c",k[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
char str[]={'H','e','l','l','o','\0'};
pass(str);
return 0;
}
==============================
#include <stdio.h>
#include <stdlib.h>
void pass(char k[])
{
int i;
for(i=0;k[i] !='\0';i++)
{
printf("%c",k[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
char str[]="Hello";//進行初始化,會自動加上'\0'
pass(str);
return 0;
}
===============================
#include <stdio.h>
#include <stdlib.h>
void pass(char k[])
{
printf("%s",k);
}
int main(int argc, char *argv[])
{
char str[]="Hello";//進行初始化,會自動加上'\0'
pass(str);
return 0;
}
Query,記得跳出視窗,要關閉才能compiler
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i,n[5];//i是陣列索引編號, index是0~4
for(i=0;i<5;i++) // 陣列5, 所以i<5 比較直覺
{
printf("%d: ",i+1);//加1代表第1個
scanf("%d",&n[i]);//連續輸入5個數值
}
while(1)//用while查詢很多次
{
printf("Query: ");//印出查詢
scanf("%d", &i);//輸入第幾個
if(i==0) break;//如果輸入0就直接跳出
printf("%d\n",n[i-1]);//你輸入1,其實index是0
}
return 0;
}
dice
例1:one dice
/*
sum-1即可找到對應的編號
count[0]=存放1共出現幾次--->count[1-1]
count[1]=存放2共出現幾次--->count[2-1]
count[2]=存放3共出現幾次--->count[3-1]
count[3]=存放4共出現幾次--->count[4-1]
count[4]=存放5共出現幾次--->count[5-1]
count[5]=存放6共出現幾次--->count[6-1]
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
srand( time(0));
int counter[6]={0};//要統計dice用
int i;
for(i=0;i<=100000;i++)//i代表次數
{
int dice1 =rand()%6+1;
int sum=dice1;
counter[sum-1]++;//最小是2,最大是12 ,共有11個數字
}//counter=counter[sum-2]+1
for(i=1;i<=6;i++)//i代表點數和
{
printf("%d: %d\n",i,counter[i-1]);
//印出2~12,存在一維陣列count[i-2]=對應的值仙出來
}
return 0;
}
例2:two dice
/*
sum-2即可找到對應的編號
count[0]=存放2共出現幾次--->count[2-2]
count[1]=存放3共出現幾次--->count[3-2]
count[2]=存放4共出現幾次--->count[4-2]
count[3]=存放5共出現幾次--->count[5-2]
count[4]=存放6共出現幾次--->count[6-2]
count[5]=存放7共出現幾次--->count[7-2]
count[6]=存放8共出現幾次--->count[8-2]
count[7]=存放9共出現幾次--->count[9-2]
count[8]=存放10共出現幾次-->count[10-2]
count[9]=存放11共出現幾次-->count[11-2]
count[10]=存放12共出現幾次->count[12-2]
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
srand( time(0));
int counter[11]={0};
int i;
for(i=0;i<=100000;i++)//i代表次數
{
int dice1 =rand()%6+1;
int dice2 =rand()%6+1;
int sum=dice1+dice2;
counter[sum-2]++;//最小是2,最大是12 ,共有11個數字
}//counter=counter[sum-2]+1
for(i=2;i<=12;i++)//i代表點數和
{
printf("%d: %d\n",i,counter[i-2]);
//印出2~12,存在一維陣列count[i-2]=對應的值仙出來
}
return 0;
}
/*
sum-1即可找到對應的編號
count[0]=存放1共出現幾次--->count[1-1]
count[1]=存放2共出現幾次--->count[2-1]
count[2]=存放3共出現幾次--->count[3-1]
count[3]=存放4共出現幾次--->count[4-1]
count[4]=存放5共出現幾次--->count[5-1]
count[5]=存放6共出現幾次--->count[6-1]
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
srand( time(0));
int counter[6]={0};//要統計dice用
int i;
for(i=0;i<=100000;i++)//i代表次數
{
int dice1 =rand()%6+1;
int sum=dice1;
counter[sum-1]++;//最小是2,最大是12 ,共有11個數字
}//counter=counter[sum-2]+1
for(i=1;i<=6;i++)//i代表點數和
{
printf("%d: %d\n",i,counter[i-1]);
//印出2~12,存在一維陣列count[i-2]=對應的值仙出來
}
return 0;
}
例2:two dice
/*
sum-2即可找到對應的編號
count[0]=存放2共出現幾次--->count[2-2]
count[1]=存放3共出現幾次--->count[3-2]
count[2]=存放4共出現幾次--->count[4-2]
count[3]=存放5共出現幾次--->count[5-2]
count[4]=存放6共出現幾次--->count[6-2]
count[5]=存放7共出現幾次--->count[7-2]
count[6]=存放8共出現幾次--->count[8-2]
count[7]=存放9共出現幾次--->count[9-2]
count[8]=存放10共出現幾次-->count[10-2]
count[9]=存放11共出現幾次-->count[11-2]
count[10]=存放12共出現幾次->count[12-2]
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
srand( time(0));
int counter[11]={0};
int i;
for(i=0;i<=100000;i++)//i代表次數
{
int dice1 =rand()%6+1;
int dice2 =rand()%6+1;
int sum=dice1+dice2;
counter[sum-2]++;//最小是2,最大是12 ,共有11個數字
}//counter=counter[sum-2]+1
for(i=2;i<=12;i++)//i代表點數和
{
printf("%d: %d\n",i,counter[i-2]);
//印出2~12,存在一維陣列count[i-2]=對應的值仙出來
}
return 0;
}
2020年2月9日 星期日
判斷誰大 , 練習fcn傳陣列
//判斷誰大 , 練習fcn傳陣列
#include <stdio.h>
#include <stdlib.h>
int maxv(int v[],int n)
{
int max=v[0],i;
for(i=1;i<n;i++)//第一個最大,所以從index=1開始比
{
if(v[i]>max)//如果比原的大就change
max=v[i];
}
return max;
}
int main(int argc, char *argv[])
{
int a[3]={11,22,3};
printf("%d\n",maxv(a,3));
int b[]={1,2,3,4,5,6,7,8,9};
printf("%d\n",maxv(b,9));
int c[]={11,21,33,43,52,62,71,18,19};
printf("%d\n",maxv(c,9));
return 0;
}
//參考此網站https://www.youtube.com/watch?v=Y68Gnrvw1k4&list=PLY_qIufNHc293YnIjVeEwNDuqGo8y2Emx&index=145
#include <stdio.h>
#include <stdlib.h>
int maxv(int v[],int n)
{
int max=v[0],i;
for(i=1;i<n;i++)//第一個最大,所以從index=1開始比
{
if(v[i]>max)//如果比原的大就change
max=v[i];
}
return max;
}
int main(int argc, char *argv[])
{
int a[3]={11,22,3};
printf("%d\n",maxv(a,3));
int b[]={1,2,3,4,5,6,7,8,9};
printf("%d\n",maxv(b,9));
int c[]={11,21,33,43,52,62,71,18,19};
printf("%d\n",maxv(c,9));
return 0;
}
//參考此網站https://www.youtube.com/watch?v=Y68Gnrvw1k4&list=PLY_qIufNHc293YnIjVeEwNDuqGo8y2Emx&index=145
判断是平年,还是润年
#include<iostream>
#include<iomanip>
using namespace std;
//1900年1月1号是星期一
int nyear(int year)//判断是平年,还是润年
{
if(year%400==0||(year%4==0&&year%100!=0))
return 366;
else
return 365;
}
int nmonth(int m,int y) //根据年份和月份,判断是那一个月的天数
{
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
return 31;
else if(nyear(y)==366&&m==2)
return 29;
else if(nyear(y)==365&&m==2)
return 28;
else
return 30;
}
int getDays(int year, int month)//输入年份和月份确定到1900年1月1日总共多少天
{
int i;
int sum=0;
if(year>1900)
for(i=1900;i<year;i++)
sum+=nyear(i);
if(month>1)
for(i=1;i<month;i++)
sum+=nmonth(i,year);
return sum; //返回总共多少天;
}
int main()
{
int i,j,year,month,day,sum,daycount,cases=1;
while(cin>>year>>month)
{
sum=getDays(year,month);
day=sum%7; //这里算出来这个月的一号前面需要几个空格;等于0的话,前面就不需要空格
daycount=nmonth(month,year);//算出这个月的天数
cout<<"Case "<<cases++<<": "<<year<<"年"<<month<<"月"<<endl;
cout<<"Mon Tue Wed Thu Fri Sat Sun"<<endl;
for(i=0;i<day%7;i++)
cout<<" ";//
for(i=1,j=day+1;i<=daycount;i++,j++)
{
cout<<setw(4)<<setiosflags(ios::left)<<i;
if(j%7==0) cout<<endl;
}
cout<<endl;
}
return 0;
}
//原出處https://blog.csdn.net/rain_qingtian/article/details/16845111
#include<iomanip>
using namespace std;
//1900年1月1号是星期一
int nyear(int year)//判断是平年,还是润年
{
if(year%400==0||(year%4==0&&year%100!=0))
return 366;
else
return 365;
}
int nmonth(int m,int y) //根据年份和月份,判断是那一个月的天数
{
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
return 31;
else if(nyear(y)==366&&m==2)
return 29;
else if(nyear(y)==365&&m==2)
return 28;
else
return 30;
}
int getDays(int year, int month)//输入年份和月份确定到1900年1月1日总共多少天
{
int i;
int sum=0;
if(year>1900)
for(i=1900;i<year;i++)
sum+=nyear(i);
if(month>1)
for(i=1;i<month;i++)
sum+=nmonth(i,year);
return sum; //返回总共多少天;
}
int main()
{
int i,j,year,month,day,sum,daycount,cases=1;
while(cin>>year>>month)
{
sum=getDays(year,month);
day=sum%7; //这里算出来这个月的一号前面需要几个空格;等于0的话,前面就不需要空格
daycount=nmonth(month,year);//算出这个月的天数
cout<<"Case "<<cases++<<": "<<year<<"年"<<month<<"月"<<endl;
cout<<"Mon Tue Wed Thu Fri Sat Sun"<<endl;
for(i=0;i<day%7;i++)
cout<<" ";//
for(i=1,j=day+1;i<=daycount;i++,j++)
{
cout<<setw(4)<<setiosflags(ios::left)<<i;
if(j%7==0) cout<<endl;
}
cout<<endl;
}
return 0;
}
//原出處https://blog.csdn.net/rain_qingtian/article/details/16845111
2020年2月8日 星期六
左2順/逆時針
int seg7[] = {2, 3, 4, 5, 6, 7, 8};
int table7segR[] = {0xFF, 0xFE, 0xFD, 0xFB, 0xF7, 0xFF, 0xFF, 0xFF};
int table7segL[] = {0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xF7, 0xEF, 0xDF};
int com[] = {9, 10, 11, 12};
int i, j;
int sw = 13;
unsigned long time = 0;
int count ;
void setup()
{
for (i = 0; i < 8; i++)
{
pinMode(seg7[i], OUTPUT);
}
for (i = 0; i < 4; i++)
{
pinMode(com[i], OUTPUT);
}
pinMode(sw, INPUT);
}
void loop()
{
int add;
int number;
if (digitalRead(sw) == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for (i = 1; i >= 0; i--)
{
for (j = 0; j < 6; j++) //一個七段index 從0跑到5
{
if (bitRead(~table7segL[number], j))
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(com[1], HIGH);
delay(5);
digitalWrite(com[1], LOW);
//============================================
for (j = 0; j < 6; j++) //一個七段index 從0跑到5
{
if (bitRead(~table7segR[number], j))
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(com[0], HIGH);
delay(5);
digitalWrite(com[0], LOW);
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 0)
{
count++;
if (count > 7)count = 0;//超過7要歸7
}
else
{
count--;
if (count < 0)count = 7;//小於0要歸7
}
}
}
}
一個七段abcdefg,順時逆時針
int seg7[] = {2, 3, 4, 5, 6, 7, 8};
int table7seg[] = {0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xFF};
int com[] = {9, 10, 11, 12};
int i, j;
int sw = 13;
unsigned long time = 0;
int count ;
void setup()
{
for (i = 0; i < 8; i++)
{
pinMode(seg7[i], OUTPUT);
}
for (i = 0; i < 4; i++)
{
pinMode(com[i], OUTPUT);
}
pinMode(sw, INPUT);
}
void loop()
{
int add;
int number;
if (digitalRead(sw) == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for (i = 0; i < 1; i++)
{
for(j=0;j<6;j++)//一個七段index 從0跑到5
{
if (bitRead(~table7seg[number], j))
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(com[i], HIGH);
delay(5);
digitalWrite(com[i], LOW);
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 0)
{
count++;
if (count > 5)count = 0;//超過5要歸0
}
else
{
count--;
if (count < 0)count = 5;//小於0要歸5
}
}
}
}
int table7seg[] = {0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xFF};
int com[] = {9, 10, 11, 12};
int i, j;
int sw = 13;
unsigned long time = 0;
int count ;
void setup()
{
for (i = 0; i < 8; i++)
{
pinMode(seg7[i], OUTPUT);
}
for (i = 0; i < 4; i++)
{
pinMode(com[i], OUTPUT);
}
pinMode(sw, INPUT);
}
void loop()
{
int add;
int number;
if (digitalRead(sw) == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for (i = 0; i < 1; i++)
{
for(j=0;j<6;j++)//一個七段index 從0跑到5
{
if (bitRead(~table7seg[number], j))
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(com[i], HIGH);
delay(5);
digitalWrite(com[i], LOW);
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 0)
{
count++;
if (count > 5)count = 0;//超過5要歸0
}
else
{
count--;
if (count < 0)count = 5;//小於0要歸5
}
}
}
}
左2-上下數
int seg7[] = {2, 3, 4, 5, 6, 7, 8};
int table7seg[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
int com[] = {9, 10, 11, 12};
int i, j;
int sw = 13;
unsigned long time = 0;
int count ;
void disp2020()
{
int i, j;
for (i = 0; i < 50; i++)
{
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[0], j))//0
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
delay(1); digitalWrite(9, LOW);//關閉鬼影
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[2], j))//2
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, LOW );
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
delay(1); digitalWrite(10, LOW);
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[0], j))//0
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, LOW);
digitalWrite(10, LOW );
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
delay(1); digitalWrite(11, LOW);
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[2], j))//2
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW );
digitalWrite(12, HIGH);
delay(1); digitalWrite(12, LOW);
}
}
void setup()
{
for (i = 0; i < 8; i++)
{
pinMode(seg7[i], OUTPUT);
}
for (i = 0; i < 4; i++)
{
pinMode(com[i], OUTPUT);
}
pinMode(sw, INPUT);
for (j = 0; j < 3; j++)//跑閃爍三次2020
{
disp2020();
for (i = 9; i < 13; i++)
{
digitalWrite(com[i], LOW);
}
delay(500);
}
delay(2000);
}
void loop()
{
int add;
int number;
if (digitalRead(sw) == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[number % 10], j))
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(com[i], HIGH);
delay(5);
digitalWrite(com[i], LOW);
number = number / 10;
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 0)
{
count++;
if (count > 99)count = 0;
}
else
{
count--;
if (count < 0)count = 99;
}
}
}
}
int table7seg[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
int com[] = {9, 10, 11, 12};
int i, j;
int sw = 13;
unsigned long time = 0;
int count ;
void disp2020()
{
int i, j;
for (i = 0; i < 50; i++)
{
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[0], j))//0
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
delay(1); digitalWrite(9, LOW);//關閉鬼影
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[2], j))//2
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, LOW );
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
delay(1); digitalWrite(10, LOW);
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[0], j))//0
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, LOW);
digitalWrite(10, LOW );
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
delay(1); digitalWrite(11, LOW);
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[2], j))//2
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW );
digitalWrite(12, HIGH);
delay(1); digitalWrite(12, LOW);
}
}
void setup()
{
for (i = 0; i < 8; i++)
{
pinMode(seg7[i], OUTPUT);
}
for (i = 0; i < 4; i++)
{
pinMode(com[i], OUTPUT);
}
pinMode(sw, INPUT);
for (j = 0; j < 3; j++)//跑閃爍三次2020
{
disp2020();
for (i = 9; i < 13; i++)
{
digitalWrite(com[i], LOW);
}
delay(500);
}
delay(2000);
}
void loop()
{
int add;
int number;
if (digitalRead(sw) == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[number % 10], j))
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(com[i], HIGH);
delay(5);
digitalWrite(com[i], LOW);
number = number / 10;
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 0)
{
count++;
if (count > 99)count = 0;
}
else
{
count--;
if (count < 0)count = 99;
}
}
}
}
開機爍2020三下,停2秒再上數,壓sw下數
int seg7[] = {2, 3, 4, 5, 6, 7, 8};
int table7seg[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
int com[] = {9, 10, 11, 12};
int i, j;
int sw = 13;
unsigned long time = 0;
int count ;
void disp2020()
{
int i, j;
for (i = 0; i < 50; i++)
{
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[0], j))//0
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
delay(1); digitalWrite(9, LOW);//關閉鬼影
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[2], j))//2
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, LOW );
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
delay(1); digitalWrite(10, LOW);
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[0], j))//0
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, LOW);
digitalWrite(10, LOW );
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
delay(1); digitalWrite(11, LOW);
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[2], j))//2
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW );
digitalWrite(12, HIGH);
delay(1); digitalWrite(12, LOW);
}
}
void setup()
{
for (i = 0; i < 8; i++)
{
pinMode(seg7[i], OUTPUT);
}
for (i = 0; i < 4; i++)
{
pinMode(com[i], OUTPUT);
}
pinMode(sw, INPUT);
for (j = 0; j < 3; j++)//跑閃爍三次2020
{
disp2020();
for (i = 9; i < 13; i++)
{
digitalWrite(com[i], LOW);
}
delay(500);
}
delay(2000);
}
void loop()
{
int add;
int number;
if (digitalRead(sw) == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 7; j++)
{
if (bitRead(~table7seg[number % 10], j))
{
digitalWrite(seg7[j], HIGH);
}
else
{
digitalWrite(seg7[j], LOW);
}
}
digitalWrite(com[i], HIGH);
delay(5);
digitalWrite(com[i], LOW);
number = number / 10;
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 1)//調開機閃三下之後是上數或下數
{
count++;
if (count > 9999)count = 0;
}
else
{
count--;
if (count < 0)count = 9999;
}
}
}
}
2020年2月7日 星期五
C++ 猜數字
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main(int argc, char *argv[])
{
int guess=1000;
srand(time(NULL));
int Ans = rand() % 100+1;
int i=0;
while (guess!=Ans)
{
//cout << "輸入一個猜測值:" << Ans << endl;
cout << "輸入一個猜測值:" << endl;
cin >> guess ;
i++;
if (guess>Ans)
cout <<"太大了,請猜小一點的數值!" << endl;
else if (guess <Ans)
cout <<"太小了,請猜大一點的數值!"<< endl;
else if (guess == Ans)
cout << "答對了..." << "總共猜了"<< i <<"次" ;
}
system("PAUSE");
return 0;
}
Printable ASCII
#include <iostream>
using namespace std;
int main()
{
cout << "Printable ASCII:\n";
for (char i = 32; i < 127; ++i)
{
cout << i << ' ';
if (i % 4 == 3)
cout << '\n';
}
system("PAUSE");
return 0;
}
利用輸入ctrl-z按enter來跳出while回圈
//利用輸入ctrl-z按enter來跳出while回圈
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int lower_case_count=0;//counter
char ch;
while(cin >> ch)
{
if(ch >='a' && ch <= 'z')
lower_case_count++;
}
cout << "lower case count:" << lower_case_count << endl;
system("PAUSE");
return 0;
}
srand(time(NULL))
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int rand(void);
int i;
int main()
{
srand(time(NULL));
for(i=0;i<10;i++)
{
cout << rand() % 10 << ' ' ;
}
cout << endl;
system("PAUSE");
return 0;
}
輸入:4567 輸出:4 5 6 7
/*
1.輸入:4567 輸出:4 5 6 7
2.輸入: 321 輸出:0 3 2 1
*/
#include <iostream>
using namespace std;
int main()
{
int number=0;
int val1;
int val2;
int val3;
int val4; //val1~val4千百十個
cout << "請輸入0000~9999:";
cin >> number;
val1=(number/1000)%10;
val2=(number/100)%10;
val3=(number/10)%10;
val4=number%10;
cout << val1 << ' '<< val2 << ' '<< val3 << ' '<< val4 << endl;
system("PAUSE");
return 0;
}
潤年就是該年可被400整除或可被4整除且不被100整除
//潤年就是該年可被400整除或可被4整除且不被100整除
#include <iostream>
using namespace std;
int main()
{
int year=0;
cout << "請輸入西元年數:";
cin >> year;
if((year % 4 == 0) && ((year % 100 != 0) || (year %400 == 0)))
cout << "該年為潤年" << endl;
else
cout << "該年不為潤年" << endl;
system("PAUSE");
return 0;
}
2020年2月6日 星期四
9999+sw上數/下數
int i;
int j;
int count=0;
int number;
int KeyData;
int numKeys=0;
const int debounceDelay=20;
unsigned long time=0;
int table7seg[10]={//寫的是共陽極0亮,而共陰極1亮
0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
const int seg[]={2,3,4,5,6,7,8}; //abcdefg
const int com[]={9,10,11,12}; //com0-com3
const int sw=13;
void setup()
{
int KeyData;
int numKeys=0;
const int debounceDelay=20;
for(i=0;i<7;i++)
pinMode(seg[i],OUTPUT);
for(i=0;i<4;i++)
{
pinMode(com[i],OUTPUT);
digitalWrite(com[i],HIGH);
}
}
void loop()
{
KeyData=digitalRead(sw);//讀取按鍵
if(KeyData==LOW) //按下按鍵
{
delay(debounceDelay); //消除彈跳
while(digitalRead(sw)==LOW); //按鍵尚未放開
numKeys++;//記錄按鍵次數
}
number=count;//顯示計數值
for(i=0;i<=3;i++)//掃瞄四位顯示數com
{
for(j=0;j<8;j++)
{
if(bitRead(~table7seg[number%10],j))
digitalWrite(seg[j],HIGH);//共陰極1亮
else
digitalWrite(seg[j],LOW);
}
digitalWrite(com[i],HIGH); //共同參考端0打開
delay(5);
digitalWrite(com[i],LOW);//共同參考端0關閉
number=number/10;//下一位數
if(millis()-time>=1000)
{
if(numKeys%2==0)
{
count++;
if(count>9999)
count=0;
}
else
{
count--;
if(count<0)
count=9999;
}
}
}
}
int j;
int count=0;
int number;
int KeyData;
int numKeys=0;
const int debounceDelay=20;
unsigned long time=0;
int table7seg[10]={//寫的是共陽極0亮,而共陰極1亮
0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
const int seg[]={2,3,4,5,6,7,8}; //abcdefg
const int com[]={9,10,11,12}; //com0-com3
const int sw=13;
void setup()
{
int KeyData;
int numKeys=0;
const int debounceDelay=20;
for(i=0;i<7;i++)
pinMode(seg[i],OUTPUT);
for(i=0;i<4;i++)
{
pinMode(com[i],OUTPUT);
digitalWrite(com[i],HIGH);
}
}
void loop()
{
KeyData=digitalRead(sw);//讀取按鍵
if(KeyData==LOW) //按下按鍵
{
delay(debounceDelay); //消除彈跳
while(digitalRead(sw)==LOW); //按鍵尚未放開
numKeys++;//記錄按鍵次數
}
number=count;//顯示計數值
for(i=0;i<=3;i++)//掃瞄四位顯示數com
{
for(j=0;j<8;j++)
{
if(bitRead(~table7seg[number%10],j))
digitalWrite(seg[j],HIGH);//共陰極1亮
else
digitalWrite(seg[j],LOW);
}
digitalWrite(com[i],HIGH); //共同參考端0打開
delay(5);
digitalWrite(com[i],LOW);//共同參考端0關閉
number=number/10;//下一位數
if(millis()-time>=1000)
{
if(numKeys%2==0)
{
count++;
if(count>9999)
count=0;
}
else
{
count--;
if(count<0)
count=9999;
}
}
}
}
用八個指撥開關顯示十位數、個位數
/* 指撥開關加七段顯示器實驗 */
const int dipsw1[] = { 9, 10, 11, 12};// 四位元指撥開關,對應MSB~LSB
const int dipsw2[] = { 14, 15, 16, 17}; // 四位元指撥開關,對應MSB~LSB
const byte Weight1[] = { 8, 4, 2, 1};// 每一位元對應到的加權值,對應2^3, 2^2, 2^1, 2^0
int seg7[] = {2, 3, 4, 5, 6, 7, 8};// 七段控制腳陣列,分別對應a~g段
char seg7Table[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90,
};// 七節顯示器編碼表
int scan[] = {18, 19};
int i, j, k; // 迴圈用的變數
void OutPort(byte data)// 將指定值顯示在七段顯示器上,最低位元為a,依序為 abcdefg
{
for (j = 0; j < 7; j++) {
if (data % 2 == 1) // 取出 dat 的最低位元
digitalWrite(seg7[j], HIGH); // 若為 1 代表該段要亮, 輸出高準位
else
digitalWrite(seg7[j], LOW); // 若為 0 代表該段要滅, 輸出低準位
data = data / 2; // 除2,進行下一位元的處理
}
}
void setup()// 此 setup 程序只有在微控制器按 reset 時執行一次
{
for (i = 0; i < 7; i++) // 初始化指定的數位腳位為輸出模式
{
pinMode(seg7[i], OUTPUT);
}
for (i = 0; i < 2; i++)
{
pinMode(scan[i], OUTPUT);
}
for (i = 0; i < 4; i++) {
pinMode(dipsw1[i], INPUT);
}
for (i = 0; i < 4; i++) {
pinMode(dipsw2[i], INPUT);
}
}
void loop()// 此 loop 程序會一直重覆執行
{
{//顯示個位數
int num1 = 0;
int reading1;
for (i = 0; i < 4; i++)
{
reading1 = digitalRead(dipsw1[i]); // 讀取指撥開關狀態
if (reading1 == HIGH) num1 = num1 + Weight1[i];// 往上撥時為LOW,往下撥為HIGH,為LOW時要加上加權值
}
OutPort(~seg7Table[num1]); // 取出陣列中對應的數字編碼表
digitalWrite(scan[0], HIGH);
delay(5);
digitalWrite(scan[0], LOW);
}
{//顯示十位數
int num2 = 0;
int reading2;
for (i = 0; i < 4; i++)
{
reading2 = digitalRead(dipsw2[i]); // 讀取指撥開關狀態
if (reading2 == HIGH) num2 = num2 + Weight1[i];// 往上撥時為LOW,往下撥為HIGH,為LOW時要加上加權值
}
OutPort(~seg7Table[num2]); // 取出陣列中對應的數字編碼表
digitalWrite(scan[1], HIGH);
delay(5);
digitalWrite(scan[1], LOW);
}
}
四個dip
/* 指撥開關加七段顯示器實驗 */
const int dipsw[] = { 9, 10, 11, 12};// 四位元指撥開關,對應MSB~LSB
const byte Weight[] = { 8, 4, 2, 1};// 每一位元對應到的加權值,對應2^3, 2^2, 2^1, 2^0
int seg7[] = {2, 3, 4, 5, 6, 7, 8};// 七段控制腳陣列,分別對應a~g段
char seg7Table[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, \
0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E// 七節顯示器編碼表
};
int i, j; // 迴圈用的變數
int num ;
int reading;
void setup()// 此 setup 程序只有在微控制器按 reset 時執行一次
{
for (i = 0; i < 7; i++) // 初始化指定的數位腳位為輸出模式
{
pinMode(seg7[i], OUTPUT);
}
for (i = 0; i < 4; i++) {
pinMode(dipsw[i], INPUT);
}
}
//純C使用前要先宣告、定義為了省時間,
//現在新的complier都可以接受fcn放的地方,可能混合java、C
//1973 1974發明C語言
void OutPort(byte data)// 將指定值顯示在七段顯示器上,最低位元為a,依序為 abcdefg
{
for (j = 0; j < 7; j++) {
if (data % 2 == 1) // 取出 dat 的最低位元
digitalWrite(seg7[j], HIGH); // 若為 1 代表該段要亮, 輸出高準位
else
digitalWrite(seg7[j], LOW); // 若為 0 代表該段要滅, 輸出低準位
data = data / 2; // 除2,進行下一位元的處理
}
}
void loop()// 此 loop 程序會一直重覆執行
{
/*如果沒有在35行附近統計之前先把變數清為0,則之前舊的值會殘留,導致整個程式錯誤」
每次都要清,這樣才能夠每次統計都是正確地址
統計值加=慢慢做,清為0才可以統計,算十個班級,每進入下一個班級之前為清為0,才可以算平均,你沒有清不會是正確的。
用全域變數,操作時第一次統計到,之後可能沒清除,盡量不用全域變數,盡量設在區域變數,因為只有它在使用。
用全域變數是有很多個函數都會用到
*/
num = 0;
for (i = 0; i < 4; i++)
{
reading = digitalRead(dipsw[i]); // 讀取指撥開關狀態
if (reading == HIGH) num = num + Weight[i];// 往上撥時為LOW,往下撥為HIGH,為LOW時要加上加權值
}
OutPort(~seg7Table[num]); // 取出陣列中對應的數字編碼表
}
2020年2月5日 星期三
switch( intexp )小寫變大寫
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// input a char -> upper case
//不得使用 ascII +-*/ > < >= <= ! << >> if is-else , 僅能使用 swict
char C1;
printf( "input a char: " );
scanf( "%c" , &C1 );
switch( C1 )
{
case 'a':
//printf( "A" );//故意寫兩行
//printf( "\r\n" );//按行
//break;
case 'A':
printf( "A\r\n" );
break;
default://不是輸入abc的字
printf( "input a-c\r\n" );
break;
case 'b'://大小寫變大寫B
case 'B':
printf( "B\r\n" );
break;
case 'c': case 'C':
printf( "C\r\n" );
}//[break;]
system( "PAUSE" );
return 0;
}
/*
switch( intexp )
{
case V1:
[grp statements]
[break;]
case V2:
[grp statements]
[break;]
default:
[grp statements]
}
*/
#include <stdlib.h>
int main(int argc, char *argv[])
{
// input a char -> upper case
//不得使用 ascII +-*/ > < >= <= ! << >> if is-else , 僅能使用 swict
char C1;
printf( "input a char: " );
scanf( "%c" , &C1 );
switch( C1 )
{
case 'a':
//printf( "A" );//故意寫兩行
//printf( "\r\n" );//按行
//break;
case 'A':
printf( "A\r\n" );
break;
default://不是輸入abc的字
printf( "input a-c\r\n" );
break;
case 'b'://大小寫變大寫B
case 'B':
printf( "B\r\n" );
break;
case 'c': case 'C':
printf( "C\r\n" );
}//[break;]
system( "PAUSE" );
return 0;
}
/*
switch( intexp )
{
case V1:
[grp statements]
[break;]
case V2:
[grp statements]
[break;]
default:
[grp statements]
}
*/
if( express )
#include <stdio.h> // int printf( const char*, ... )
#include <stdlib.h>
int main(int argc, char *argv[])
{
int score;
printf( "input a num: " );
scanf( "%d" , &score );
// 判斷(印出 score) 及格 T 印出 恭喜你 , >=60 F 印出 你完了 , < 60
if( ( printf( "score=%d\r\n",score ), score>=60 && score<=100) )
{
printf( "恭喜你 \r\n" );
printf( ">=60\r\n" );
}
else
( printf( "你完了 \r\n" ) , printf( "< 60\r\n" ) ) ;
/*{
printf( "你完了 \r\n" );
printf( "< 60\r\n" );
} */
system( "PAUSE" );
return 0;
}
/*
if( express )
statement_true
else
statement_flase
*/
#include <stdlib.h>
int main(int argc, char *argv[])
{
int score;
printf( "input a num: " );
scanf( "%d" , &score );
// 判斷(印出 score) 及格 T 印出 恭喜你 , >=60 F 印出 你完了 , < 60
if( ( printf( "score=%d\r\n",score ), score>=60 && score<=100) )
{
printf( "恭喜你 \r\n" );
printf( ">=60\r\n" );
}
else
( printf( "你完了 \r\n" ) , printf( "< 60\r\n" ) ) ;
/*{
printf( "你完了 \r\n" );
printf( "< 60\r\n" );
} */
system( "PAUSE" );
return 0;
}
/*
if( express )
statement_true
else
statement_flase
*/
2020年2月4日 星期二
sizeof(type)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a,b;
scanf( "%d %d", &a, &b );
printf( "MAX -> %d \n" , (a>b?a:b) );
printf( "sizeof(a)=%d, sizeof(int) =%d, sizeof(float)=%d \n sizeof(double)=%d \n" ,\
sizeof(a),sizeof(int), sizeof(float), sizeof(double) );
//印出a的大小 印出int大小
//程式碼太長換行加\
system( "PAUSE" );
return 0;
}
byte(x) 將任何型態之 x 轉成 byte 型態
Arduino 內建六個函式來轉換不同資料型態如下表 :
要注意, 記憶體長度大的轉成較小時會有 roll-over 問題, 例如 :
void setup() {
Serial.begin(9600);
int i=257;
byte b=byte(i); //輸出 1, byte 類型最大值 255, 256 時反折為 0
Serial.println(b);
}
void loop() {}
參考http://yhhuang1966.blogspot.com/2015/09/arduino_14.html
型態轉換函式 | 說明 |
char(x) | 將任何型態之 x 轉成 char 型態 |
byte(x) | 將任何型態之 x 轉成 byte 型態 |
int(x) | 將任何型態之 x 轉成 int 型態 |
word(x), word(h,l) | 將任何型態之 x 轉成 word 型態, x 可拆成高位元組 h 與低位元組 l |
long(x) | 將任何型態之 x 轉成 long 型態 |
float(x) | 將任何型態之 x 轉成 float 型態 |
要注意, 記憶體長度大的轉成較小時會有 roll-over 問題, 例如 :
void setup() {
Serial.begin(9600);
int i=257;
byte b=byte(i); //輸出 1, byte 類型最大值 255, 256 時反折為 0
Serial.println(b);
}
void loop() {}
參考http://yhhuang1966.blogspot.com/2015/09/arduino_14.html
P21跳脫字元
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a,b;
char c1;
printf( "Please input two Int. " );
scanf( "%d %d", &a, &b ); // 100 200[enter]
printf( "Please input one Char. " );
scanf( "%c", &c1 );
printf( "c1=%c, a=%d ,b=%X, ,b=%o\r\n", c1, a ,b, b );//%o為八進位
printf( "\101 = A \x41 = A 0101=%c 0x41=%c\r\n", 0101, 0x41 );
system( "PAUSE" );
return 0;
}
P17 常數
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char Str1[] = "Hello CYH"; // Str1[0] = 'H'; ... Str1[5] = ' '
printf("1. ---Str1=%s---\r\n", Str1 );
Str1[5] = '\0';//或用Str1[5] = 0 ;用數值的0;
printf("2. ---Str1=%s---\r\n", Str1 );
if( 1 ) // (1) = (true)1是真(非0即真)
printf("1 is true\n" );
if( 0 ) // (0) = (false)0是假即不成立,即不做(不會被執行)
printf("0 is true\n" ); // X
// ---- 'A' = 65 ---------------------
printf("65 = %c\n", 65 ); // 'A'以字元型態印出來
printf("0x41 = %c\n", 0x41 ); // 'A' 0x41也是字元A
printf("0101 = %c\n", 0101 ); // 'A' 64 8 1用八進位也是A
system( "PAUSE" );
return 0;
}
2020年2月3日 星期一
showPrintf
#include <stdlib.h>
void showPrintf( int p );//函數宣告一定要在前面,vb不太嚴謹(不用宣告函數)
int main(int argc, char *argv[])
{
int printf;
printf = 100;
showPrintf( printf );
system( "PAUSE" );
return 0;
}
void showPrintf( int p ) // p = printf;
{
printf( "Notepad modi -> p=%d \n" , p );
}
例2:
#include <stdlib.h> // int system( const char* cmd );
void showVar( int p );
int main(int argc, char *argv[])
{
int printf; // OK
printf = 123;
showVar( printf );
system( "PAUSE" ); // define in stdlib.h
return 0;
}
#include <stdio.h> // int printf( const char* fmt, ... );//不一定,定義在前面
int gVar2;
void showVar( int p )
{
int c;
//int printf; // error
printf( "p=%d\r\n" , p );
//int b; // error
}
void showPrintf( int p );//函數宣告一定要在前面,vb不太嚴謹(不用宣告函數)
int main(int argc, char *argv[])
{
int printf;
printf = 100;
showPrintf( printf );
system( "PAUSE" );
return 0;
}
void showPrintf( int p ) // p = printf;
{
printf( "Notepad modi -> p=%d \n" , p );
}
例2:
#include <stdlib.h> // int system( const char* cmd );
void showVar( int p );
int main(int argc, char *argv[])
{
int printf; // OK
printf = 123;
showVar( printf );
system( "PAUSE" ); // define in stdlib.h
return 0;
}
#include <stdio.h> // int printf( const char* fmt, ... );//不一定,定義在前面
int gVar2;
void showVar( int p )
{
int c;
//int printf; // error
printf( "p=%d\r\n" , p );
//int b; // error
}
練習角度
angle=angle*10+ch-'0';這段我看不是往懂,我的了解是angle=(angle*10)+(ch-'0')
字母-字母=都沒有了(a-a=0)
1-0(字母1-字母0差一格)
2-0差兩格 …以此類推
括號起來字元就變數值了
*10倍幫忙轉十進位,後面加是個位數即一位一位相加(乘10變十進位,+多少變個位數)
1.先輸入9, angle=angle*10+9-‘0’ ……angle=9
2.再輸入0,angle=9*10+0-‘0’ ……angle=90
3. 再輸入a,跳到else if(ch==’a’),執行servo.write(90);嗎?
最後重設角度angle=0代表沒有任何角度,下次要在讀值又重新開始十位數+個位數
20a、90a代表三個字母,只要是Serial,COM傳永遠是字母
鍵盤都是ascii、電報看的到的字母
字母可以比大小加單引號(ch-‘0’)
PC傳數字(整數數值例長短整數即binary)、字母
int angle = 0;
char ch = '7';
int x = 0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
angle = angle * 10 + (ch - '0');
Serial.println(angle);
}
void loop()
{
// put your main code here, to run repeatedly:
if (x == 0)
{
ch='0';
angle = angle * 10 + (ch - '0');
Serial.println(angle);
x = 1;
}
if(angle==90 && x==1)
{
Serial.println("you got it!!");
x=100;
}
}
訂閱:
文章 (Atom)
algorithm
#include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv) { for(int j=2;j<=100;j++)//j...
-
本文旨在補充《 超圖解Arduino互動設計入門 》第18章「RFID無線識別裝置與問答遊戲製作」單元,書本採用的RFID讀卡機模組是採用9600bps, TTL序列通訊介面,RFID的通訊頻率為125KHz。這種模組的接線和程式都很簡單,每當感測到RFID卡,讀卡機就把...
-
/* 多重按鈕開關 */ //==宣告區================================ #include // 定義8051暫存器之標頭檔,P2-17~19 #include "myio.h" // 自己寫的I/O程式庫 sbit PB...