Commit 2d053d1d authored by Elina Nguyen-Cadoret's avatar Elina Nguyen-Cadoret
Browse files

add the programmation of 2 different microcontrollers in week 9

parent 57ea4158
Pipeline #238752 passed with stage
in 41 seconds
......@@ -69,3 +69,217 @@ For the group assignment we decided to compare several kinds of microcontrollers
| 25 | LOW | SDA0 | SA5 | PCLK | SPI3_CE0_N | TXD2 | SDA6 |
| 26 | LOW | SDA0 | SA5 | PCLK | SPI3_CE0_N | TXD2 | SDA6 |
| 27 | LOW | SDA0 | SA5 | PCLK | SPI3_CE0_N | TXD2 | SDA6 |
## Programming of SAMD11C - Code
For this week I programmed my board with LEDs and a button. First to learn how to program, I learnt on an [Arduino](https://www.arduino.cc/) board, with Serial monitor, to learn how to make function, and fill the paragraph on Arduino software. The programmation of this microcontroller is the first microcontroller programmed for the group assignment.
I will put the files of the most advanced programmed I made, because some codes I worte are the step by step I learnt how to code but doesn't really do much things.
Here is the kind of basic code I wrote.
![](../images/week09/cap1.jpg)
I wrote a code to have a counter print on a serial monitor, to try the tool offer by Arduino. Here I just learnt how to fill the setup and the loop function.
```
void setup (){
// put your setup code here, to run once:
Serial.begin(9600); //speed of communication, 9600 baud, mandatory for the serial monitor to work
delay(1000); //wait a bit at the beginning
Serial.printIn("start"); //check if the serial monitor is working
}
int toto = 0; //the counter, an integer
void loop(){
// put your main code here, to run repeatedly:
delay(1000); //wait 1 sec, to repeat the loop after
Serial.printIn(toto); //print the coundter
toto = toto + 1; //every 1 second, the counter is added of 1
}
```
This is a screenshot of the serial monitor, where the number of the counter is print.
![](../images/week09/cap23.jpg)
[Arduino website](https://www.arduino.cc/reference/en/) has all the references, function we can use to programm.
During the learning of how to code, I make a mistake in my code. I declared a variable 2 times in the code.
![](../images/week09/cap2.jpg)
And when I upload the code, it broked the board. I wasn't able to connect the board to my computer. So I had to reflashed it with the bootloader and the programmer.
![](../images/week09/cap3.jpg)
So now, this is the code I wrote to make the green LED on when I push the button, and switch on the red LED when the button is not pushed. I found the number of the pin on the datasheet, mixed with a picture for the name to put on the code, [bellow on this website it is explained](http://fabacademy.org/2021/labs/agrilab/students/elina-nguyen-cadoret/assignments/week09/#reading-the-datasheet-of-samd11c).
Don't forget to open the serial monitor at the beginning.
![](../images/week09/cap22.jpg)
Here I wrote the code in C++ language, using different functions and variables.
```
int toto = 0; //a integer variable, a counter
int totoref = 0; // a variable that will help to storage the value of my counter and after do comparison
int pinLEDvert = 14; //LED green linked to the pin 14
int pinLEDrouge = 5; //LED red linked to pin 5
int pushButton = 15; //button linked in pin 15
void setup(){
Serial.begin(9600); //the initial beginning for the serial monitor to be able to work.
pinMode(pushButton, INPUT_PULLUP); //setup the button
pinMode(pinLEDvert, OUTPUT); //setup the LED
digitalWrite(pinLEDvert, HIGH); //check if the LED is working and if we go well in the function
delay(1000); //wait 1 sec
digitalWrite(pinLEDvert, LOW); //switch off the LED
pinMode(pinLEDrouge, OUTPUT); //setup the red LED, and do the same as the LED before
digitalWrite(pinLEDrouge, HIGH);
delay(1000);
digitalWrite(pinLEDrouge, LOW);
}
bool greenLEDstatus = false; //create a boolean
void loop(){
int buttonState = digitalRead(pushButton); //a variable that will have the state of the button, and help to do some checking in a if loop
delay(300);
if(buttonState == 0){ //if the button is pushed, then toto will change, 1 will be added on the variable integer
toto = toto+1;
}
if(totoref != toto){ //if the reference of toto is different of toto, means taht the button has been pushed, the green LED will be on
Serial.println(toto); //a check if toto has been changed, on the serial monitor
setLEDvert(true); // check the function below, turn on the green LED
greenLEDstatus = true;
setLEDrouge(false); //turn off the red LED
totoref = toto; //equalize the two variable, to go out from the if
delay(300);
}
else{ //else turn off the green LED, and turn on the red LED, the button has not been pushed
setLEDvert(false);
greenLEDstatus = false;
setLEDrouge(true);
}
}
void setLEDrouge(bool on_offr){ //same as the next function but for the red LED
if (on_offr == 1){
digitalWrite(pinLEDrouge, HIGH);
//Serial.print("LED rouge ");
//Serial.println(on_offr);
}
else{
digitalWrite(pinLEDrouge, LOW);
}
}
void setLEDvert(bool on_offv){ //a function that will turn on or off the LED
if (on_offv == 1){ //if the boolean is true then the LED will turn on otherwise it will turn off the green LED
digitalWrite(pinLEDvert, HIGH);
//Serial.print("LED vert ");
//Serial.println(on_offv);
}
else{
digitalWrite(pinLEDvert, LOW);
}
}
```
The file is available at the end of the page, in [all the files](http://fabacademy.org/2021/labs/agrilab/students/elina-nguyen-cadoret/assignments/week09/#all-the-files).
To upload the code on the board with Arduino, first I selected the right board, and not forget to select the port where the Mattairtech board is plugged. And it is the LEDs turned on and off according to the push button.
<video controls="true" allowfullscrenn="true" width="35%" muted="muted">
<source src="../../images/week09/vid.mp4" type="video/mp4">
</video>
Now because I had issues on one pin on one of my board, I fixed it this week. I change with a wire where the LED is linked on the microcontroller. To check which pin is working, I upload this code in the board, and checked how many volts going out the pin I programmed with the multimeter. I took the basic example on Arduino software and just changed some part to be able to change the number pf the pin quickly.
I won't put the file of this code, it was taken in the example folder of Arduino, a basic blink, I just added 2 variables to be able to do my checks quickly.
```
int pinLEDvert = 4; //create a varible of which pin is linked to the LED, to change it quickly and make a test on each pin of the board
int pinLEDrouge = 5; // the red LED is working, and linked to the pin 5
void setup() {
// put your setup code here, to run once:
pinMode(pinLEDvert, OUTPUT);
pinMode(pinLEDrouge, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(pinLEDvert, HIGH); //turn on both LED
digitalWrite(pinLEDrouge, HIGH);
delay(1000);
digitalWrite(pinLEDvert, LOW); //turn off both LED
digitalWrite(pinLEDrouge, LOW);
delay(1000);
}
```
It shows around 3.3 V on the multimeter so I supposed that the pin is working.
![](../images/week09/1.jpg)
![](../images/week09/2.jpg)
I soldered a wire, and cut the trace where the LED is linked with the microcontroller, and when I plugged the board the LED were blinking both, so it worked.
![](../images/week09/3.jpg)
## Micro:bit - programming microcontroller Nordic nRF51822
During the bootcamp week, I discovered a little bit about [micro:bit](https://microbit.org/), that's why I reused this board for this week. On micro:bit website, there is an interface, [makecode editor](https://classroom.microbit.org/createactivity/makecode/editor) to play with code as blocks. I made the programm with blockly language, that bring to a JavaScript code. The micro:bit board is processed by a MCU [Nordic nRF51822](https://www.generationrobots.com/fr/402708-carte-microbit.html).
The programmation of this microcontroller is the second microcontroller programmed for the group assignment.
<!-- Fabacademy pouet pouet -->
To test the editor, I put the blocks to make the board print "hello" at the end and when we push the A button, it make a heart shape blink two times. The button B clear the LEDs.
![](../images/week09/cap21.jpg)
We can also check the code in JavaScript:
```
input.onButtonPressed(Button.A, function () {
for (let index = 0; index < 2; index++) {
basic.showIcon(IconNames.Heart)
basic.pause(1000)
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
}
})
input.onButtonPressed(Button.B, function () {
basic.clearScreen()
})
basic.showString("Hello!")
basic.forever(function () {
})
```
I did not write the code in JavaScript, but I just checked to know what is going on in with this language and blockly I used.
When the code is done, I just need to download the [file](../files/microbit.hex) and then copy it in the micro:bit board, plugged to my computer.
And here is the result:
<video controls="true" allowfullscrenn="true" width="35%" muted="muted">
<source src="../../images/week09/vid2.mp4" type="video/mp4">
</video>
This way of working is nice to learn quickly some code, and very nice to see, but it is pretty limited, to the micro:bit board.
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment