4. Embedded Programming¶
4.1 Group Assignment¶
Our group work was to search about {Arduino MKR 1010} and collect these information about it:
4.2 Individual Assignment¶
Easy Mode¶
1- Easy Mode: Blink your led but have your blink delay periods randomized values 1 sec and 5 sec. First I select the board and port and then compile the code twice by changing the delay time:
First this is how I select the Board and the Port:
1sec delay¶
in this code the main thing is the delay part, the blinking of the built-in led will depend on the delay time
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
5sec delay¶
in this code the main thing is the delay part, the blinking of the built-in led will depend on the delay time
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(5000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(5000); // wait for a second
}
After setting the board and port and make sure of the code we upload the code by clicking the arrow
Medium Mode¶
2- Medium Mode: pre code MCU to send a morse code 1 word message:
const int LED_PIN = 13;
// Define the Morse code dictionary
const char *morse_code[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
};
// Function to send a Morse code symbol
void send_morse(char symbol) {
switch(symbol) {
case '.':
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
delay(200);
break;
case '-':
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(200);
break;
case ' ':
delay(500);
break;
default:
break;}}
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);}
void loop() {
// Get input word from user
char word[50];
Serial.println("Enter a word:");
while(!Serial.available()) {}
int i = 0;
while(Serial.available()) {
word[i] = Serial.read();
i++; }
word[i] = '\0';
// Convert the word to Morse code
int length = strlen(word);
for(int i=0; i<length; i++) {
char letter = toupper(word[i]);
if(letter == ' ') {
Serial.print(" "); delay(500);}
else {
const char *morse_symbol = morse_code[letter - 'A'];
int symbol_length = strlen(morse_symbol);
for(int j=0; j<symbol_length; j++) {
char symbol = morse_symbol[j];
Serial.print(symbol);
send_morse(symbol);}
Serial.print(" ");}}
Serial.println();}
And this is the result:
Hard Mode¶
3- Hard Mode: Select the duration of the light being ON and OFF while the program is running by entering it in the serial monitor. Write a code that takes the data from the serial monitor and delays by that amount of time
int onDuration = 0; // duration of light being on
int offDuration = 0; // duration of light being off
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(LED_BUILTIN, OUTPUT);} // set LED pin as output
void loop() {
if (Serial.available()) { // check if data is available on serial port
char input = Serial.read(); // read the input character
switch (input) {
case 'o': // if input is 'o', read on duration
onDuration = readDuration();
break;
case 'f': // if input is 'f', read off duration
offDuration = readDuration();
break;
default:
break;}}
digitalWrite(LED_BUILTIN, HIGH); // turn on LED
delay(onDuration); // wait for on duration
digitalWrite(LED_BUILTIN, LOW); // turn off LED
delay(offDuration);} // wait for off duration
int readDuration() {
String inputString = ""; // initialize input string
while (Serial.available()) {
char inChar = Serial.read(); // read input character
inputString += inChar; } // append input character to input string
return inputString.toInt(); }// convert input string to integer and return
-The Arduino MKR 1010 Light is ON for 3 sec:
Installing MU editor¶
In this week we have to try another language and another editor, so MU editor was required to use python for Blink program:
-First we chose the CircuitPython mode then upload the code in the MCU.
TinkerCad simulation¶
-I also tried TinkerCad simulations for the blinking code
I downloaded the code from TinkerCad:
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
// turn the LED on (HIGH is the voltage level)
digitalWrite(LED_BUILTIN, HIGH);
delay(2000); // Wait for 3000 millisecond(s)
// turn the LED off by making the voltage LOW
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 3000 millisecond(s)
}
Then open it from Arduino IDE software and upload it to the board.
click here to see the download the code