If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.
程式設計可以改變您的未來(Programming can change your future)。 雲林SONG 全名為雲林軟體工程(SOftware eNGineering),目標致力於軟體人才的培養並推廣開源軟體落實於資訊教育。程式設計的觀念是軟體產品的基礎,程式碼就像沙子一樣,要紮實,所建立出來的高塔才會穩固。本站也提供資訊教育相關的教學資源。 YunlinSONG stands for Yunlin SOftware eNGineering, offering tutorial for computer programming and promoting open-source software. Teaching resources in information technology education are provided here.
Solution to Karel Coding – Hour of Code
MakeCode Microbit 遙控機器人 (Microbit Remote-Controlled Robot)
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.
本篇文章將講解如何製作一個簡易的遙控機器人,此篇文章所使用的機器人為【麥昆智能小車 (Maqueen)】。This post will build a simple remote car with Maqueen which is a micro:bit based robot platform for education.一、功能如下(Game Design):
訊號說明(The communication way):
遙控器(The controller)
- 按下 A + B 按鈕時,送出停止(stop)訊號。Sending a stop signal when button A and B are pressed.
- Microbit 板子向上傾斜時,送出前進f(forward)訊號。Sending a forward signal when tilting the microbit front.
- Microbit 板子向下傾斜時,送出後退b(backward)訊號。Sending a backward signal when tilting the microbit back.
- Microbit 板子向左傾斜時,送出左轉l(left)訊號。Sending a left signal when tilting the microbit to the left.
- Microbit 板子向右傾斜時,送出右轉r(right)訊號。Sending a right signal when tilting the microbit to the right.
- 收到停止(stop)訊號時,車子停止。On receiving a stop signal, the car will stop.
- 收到前進f(forward)訊號時,車子前進。On receiving a forward signal, the car will move forward.
- 收到後退f(backward)訊號時,車子後退。On receiving a backward signal, the car will move backward.
- 收到左轉l(left)訊號時,車子左轉。On receiving a left signal, the car will turn left .
- 收到右轉r(right)訊號時,車子右轉。On receiving a right signal, the car will turn right .
遙控器(The controller)
- 按下 A + B 按鈕時,送出停止(stop)訊號。Sending a stop signal when button A and B are pressed.
- Microbit 板子向上傾斜時,送出前進f(forward)訊號。Sending a forward signal when tilting the microbit front.
- Microbit 板子向下傾斜時,送出後退b(backward)訊號。Sending a backward signal when tilting the microbit back.
- Microbit 板子向左傾斜時,送出左轉l(left)訊號。Sending a left signal when tilting the microbit to the left.
- Microbit 板子向右傾斜時,送出右轉r(right)訊號。Sending a right signal when tilting the microbit to the right.
- 收到停止(stop)訊號時,車子停止。On receiving a stop signal, the car will stop.
- 收到前進f(forward)訊號時,車子前進。On receiving a forward signal, the car will move forward.
- 收到後退f(backward)訊號時,車子後退。On receiving a backward signal, the car will move backward.
- 收到左轉l(left)訊號時,車子左轉。On receiving a left signal, the car will turn left .
- 收到右轉r(right)訊號時,車子右轉。On receiving a right signal, the car will turn right .
影片(Demo Video):
bDesigner 與 Arduino 互動
bDesigner 簡短介紹
bDesigner 是由桃園市八德國小蔡佳倫老師自行研發之Scratch擴充積木軟體以及blockly程式。在他的 Youtube上也有很多的教學影片。除了可以使用 Scratch 3 以外,還可與多種硬體(如Arduino UNO、Nano、ESP32、Microbit等)互動,使用這一套就可以省去了一直安裝不同軟體的時間,非常適合接觸多種硬體與blockly程式教學的老師們。
MakeCode Microbit 遊戲設計:貓追老鼠遊戲(Microbit Game: Cat Chasing Mouse Game)
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.
一、遊戲功能如下(Game Design):
- 遊戲時間一分鐘。One minute game.
- 遊戲開始時,貓在畫面正中央。The cat is at the screen center when the game on start.
- 老鼠不停閃爍,老鼠跑到隨機位置。The mouse is blinking and at random position on the screen.
- 板子往上傾斜時,貓往上移動。The cat move up when tilting the microbit front.
- 板子往下傾斜時,貓往下移動。The cat move down when tilting the microbit back.
- 板子往右傾斜時,貓往右移動。The cat move to right when tilting the microbit to the right.
- 板子往左傾斜時,貓往左移動。The cat move to left when tilting the microbit to the left.
- 貓捉到老鼠時,得一分,老鼠跑到隨機位置。 When the cat catches the mouse, the mouse runs to random position. The player scores 1 point.
遊戲開始時,貓在畫面正中央。The cat is at the screen center when the game on start.
老鼠不停閃爍,老鼠跑到隨機位置。The mouse is blinking and at random position on the screen.
C語言練習題:陣列(C language exercise: Array)
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.
練習一:整數陣列加總使用一個大小為10的整數陣列來讀取使用者所輸入的10個整數,並算出此十個整數的總和。
練習一參考解法:
Exercise 1 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> #include <stdlib.h> int main() { int numbers[10], sum = 0; for(int i = 0; i < 10; i++) { printf("Enter an integer for numbers[%d]:", i); scanf("%d", &numbers[i]); sum = sum + numbers[i]; } printf("\nSum:%d", sum); return 0; } |
練習二:整數陣列中最大的整數
使用亂數函數 rand() 產生 10 整數,並找出最大值。
Using rand() function to generate 10 integers and find the biggest integer.
練習二參考解法:
Exercise 2 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> #include <stdlib.h> int main() { int numbers[10], big = 0; printf("The numbers:"); for(int i = 0; i < 10; i++) { numbers[i] = rand(); printf("%d,", numbers[i]); if(big < numbers[i]) big = numbers[i]; } printf("\nThe biggest integer:%d.\n", big); return 0; } |
練習三:Greeting message
Get the user's name from keyboard and print it with greeting message.
請使用者輸入自己的姓名,程式輸出【姓名】,【打招呼訊息】。
練習三參考解法:
Exercise 3 solution:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> #include <stdlib.h> int main() { char name[50]; printf("Input your name:"); scanf("%s", name); // name is a string that don't need'&' operator printf("%s, nice to meet you.\n", name); return 0; } |
練習四:奇數或偶數
使用亂數函數 rand() 產生 10 整數,分別對奇數的數加總以及偶數的數加總。
練習四參考解法:
Exercise 4 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <stdio.h> #include <stdlib.h> int main() { int numbers[10], evenSum = 0, oddSum = 0; printf("The numbers:"); for(int i = 0; i < 10; i++) { numbers[i] = rand() % 10; printf("%d,", numbers[i]); if(numbers[i] % 2) { oddSum += numbers[i]; } else { evenSum += numbers[i]; } } printf("\nThe sum of even numbers:%d\n", evenSum); printf("The sum of odd numbers:%d\n", oddSum); return 0; } |
練習五:二維陣列
練習五參考解法:
Exercise 5 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> #include <stdlib.h> int main() { int matrix[3][3]; float avg = 0.0f; printf("The matrix:\n"); for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { matrix[i][j] = rand() % 100; avg += matrix[i][j]; printf("matrix[%d][%d] = %2d, ", i, j, matrix[i][j]); } printf("\n"); } avg = avg / 9.0; printf("Average:%f\n", avg); return 0; } |
練習六:矩陣相加
Using rand() function to write a program for two 2D array of size 3x3. And output the addition of the two matrix.
練習六參考解法:
Exercise 6 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <stdio.h> #include <stdlib.h> void prtMatrix(int m[3][3]) { for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { printf("[%d][%d] = %2d, ", i, j, m[i][j]); } printf("\n"); } } int main() { int a[3][3]; int b[3][3]; int c[3][3]; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { a[i][j] = rand() % 100; b[i][j] = rand() % 100; c[i][j] = a[i][j] + b[i][j]; } } printf("a:\n"); prtMatrix(a); printf("\n"); printf("b:\n"); prtMatrix(b); printf("\n"); printf("The addition of matrix a and matrix b:\n"); prtMatrix(c); return 0; } |
使用亂數函數 rand()來建立兩個大小為 3 x 3 的二維陣列。並輸出此兩矩陣相減後的結果。
Using rand() function to write a program for two 2D array of size 3x3. And output the subtraction of the two matrix.
練習七參考解法:
Exercise 7 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <stdio.h> #include <stdlib.h> void prtMatrix(int m[3][3]) { for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { printf("[%d][%d] = %2d, ", i, j, m[i][j]); } printf("\n"); } } int main() { int a[3][3]; int b[3][3]; int c[3][3]; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { a[i][j] = rand() % 100; b[i][j] = rand() % 100; c[i][j] = a[i][j] - b[i][j]; } } printf("a:\n"); prtMatrix(a); printf("\n"); printf("b:\n"); prtMatrix(b); printf("\n"); printf("The subtraction of matrix a and matrix b:\n"); prtMatrix(c); return 0; } |
使用亂數函數 rand()來建立兩個大小為 3 x 3 的方陣 A 與 B。並輸出此兩方陣A乘B後的結果。
Using rand() function to write a program for two 2D array of size 3x3. And output the subtraction of the two square matrices.
練習八參考解法:
Exercise 8 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <stdio.h> #include <stdlib.h> #include <time.h> void prtMatrix(int m[3][3]) { for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { printf("[%d][%d] = %3d, ", i, j, m[i][j]); } printf("\n"); } } int main() { int A[3][3]; int B[3][3]; int C[3][3]; srand((unsigned) time(0)); // Matrices initialization for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { A[i][j] = rand() % 10; B[i][j] = rand() % 10; C[i][j] = 0; } } // C = A x B int sum = 0; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { sum = 0; for(int k = 0; k < 3; k++) { sum = sum + A[i][k] * B[k][j]; } C[i][j] = sum; } } printf("A:\n"); prtMatrix(A); printf("\n"); printf("B:\n"); prtMatrix(B); printf("\n"); printf("The A X B:\n"); prtMatrix(C); return 0; } |
Using rand() function to generate 10 integers. Then find a specific integer and replace it with -1.
練習九參考解法:
Exercise 9 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int numbers[10], target = -1; srand(time(0)); printf("Enter an integer between [0 - 9]:"); scanf("%d", &target); printf("The numbers:\n"); for(int i = 0; i < 10; i++) { numbers[i] = rand() % 10; printf("%2d,", numbers[i]); // Find the target and replace it with -1 if(numbers[i] == target) numbers[i] = -1; } printf("\nAfter replacing, the numbers:\n"); for(int i = 0; i < 10; i++) { printf("%2d,", numbers[i]); } printf("\n"); return 0; } |
輸入一個正整數 N,使用陣列的方式來產生前 N 個的費氏數列。
練習十參考解法:
Exercise 10 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <stdio.h> #include <stdlib.h> int main() { int fibo[1000]; int N; int t; printf("Enter a positive integer [1-999]:"); scanf("%d", &N); fibo[0] = 0; fibo[1] = 1; for(int i = 2; i < N; i++) { fibo[i] = fibo[i-1] + fibo[i-2]; } for(int i = 0; i < N; i++) printf("%2d,", fibo[i]); printf("\n"); return 0; } |