Assignments
- Design, build and connect wired or wireless node(s) with network or bus addresses
Communication between PC and my output board¶
As part of our machine design project, we need to make a communication between two microcontrollers: - a board with a keyboard (board 1) - a board do drive CC motors and LEDs (board 2)
When the good code is typed on the keyboard the microcontroller of the board 1 sends a message to the board 2 to turn on the blink and motors. To do we used serial communication between the boards.
Process¶
To test the program and the operation of my board I tried a first program. The aim was to start the motors and turn on the leds when I typed “1” in char (=49 decimal) on the keyboard of my computer. To do I used the board I made week 12 to drive motors. As I need to blink the LEDs and start and stop the motors during the blink I used millis() function.
For this code it’s important to use ATTiny core from Spence Konde, like this you don’t need to use the SoftwareSerial. To know how to use it with the ATTiny44
#define in1 2 // IN1 A4953 H-bridge #define in2 3 // IN2 A4953 H-bridge #define ledL 10 // left led #define ledR 7 // right led const unsigned long BLINK_INTERVAL_L = 400; // blink interval for left led const unsigned long BLINK_INTERVAL_R = 200; // blink interval for right led const unsigned long startmotor = 2000; //delay before to start motors const unsigned long stopmotor = 7000; //delay before to stop motors unsigned long startMillis = 0; void task_ledL(); void task_ledR(); void startEye(); int command = 0; void setup() { Serial.begin(9600); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(ledL, OUTPUT); pinMode(ledR, OUTPUT); } void loop() { //check if data are received and write it in "command" if (Serial.available() > 0) { command = Serial.read(); Serial.println(command); } if (command==49){ startEye(); command=0; } } void startEye() { startMillis = millis(); while (command == 49) { //If the time elapsed since entering the loop is between startmotor and stopmotor we start the engine and turn on the blink if ((millis() - startMillis >= startmotor) && (millis() - startMillis <= stopmotor)) { digitalWrite(in1, LOW); analogWrite(in2, 50); task_ledL(); task_ledR(); Serial.println("start motor"); } //If not, if the time elapsed since entering the loop is inferior to startmotor only we turn on the blink else if (millis() - startMillis < startmotor) { task_ledL(); task_ledR(); Serial.println("no motor"); } //If the time elapsed since entering the loop is superior to stopmotor we stop the motors and turn off the blink else { Serial.println("finish"); command = 0; digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(ledL, LOW); digitalWrite(ledR, LOW); } } } void task_ledL() { static unsigned long previousMillisLedL = 0; static byte etatBrocheLedL = LOW; // If BLINK_INTERVAL_L or more millisecondes have elapsed if (millis() - previousMillisLedL >= BLINK_INTERVAL_L) { // memorize the current value of millis() previousMillisLedL = millis(); // Reverse the state of the left LED etatBrocheLedL = !etatBrocheLedL; digitalWrite(ledL, etatBrocheLedL); } } void task_ledR() { static unsigned long previousMillisLedR = 0; static byte etatBrocheLedR = LOW; // If BLINK_INTERVAL_R or more millisecondes have elapsed if (millis() - previousMillisLedR >= BLINK_INTERVAL_R) { // memorize the current value of millis() previousMillisLedR = millis(); // Reverse the state of the right LED etatBrocheLedR = !etatBrocheLedR; digitalWrite(ledR, etatBrocheLedR); } }
Result¶
Communication between two ATTiny44 boards¶
For my final project, I need to communicate boards. One will receive the information from the sensors (INPUT_BOARDS) and the other will light LEDs to indicate to the user the composting parameters.
LED_BOARD design and production¶
I based on the Eagle schematic made for my INPUT_BOARDS to make this new PCB. I deleted the crystal to release pins for the RGB leds. I added 3 resistors of 1kohms for each LED. The positioning of the LEDs corresponds to the spacing that will be between each of them once the PCB installed on the vermicomposter’s lid. I added 3 Rx,Tx pairs to allow the serial communication from the INPUT_BOARDS to 3 other boards ( OUTPUT_BOARD, LED_BOARD and Huzzah ).
Eagle schematic | Eagle board | Fusion board | Board milled |
---|---|---|---|
The through hole LEDs have been placed on the back of the card to be visible on the top of the lid.
Fix¶
As explained above, I based on the Eagle schematic of my INPUT_BOARDS made during week 11. It was an error !! On this card I used the PA1 pins for the FTDI Rx and PA0 for the FTDI Tx. This required the use of the SoftwareSerial library which allows you to choose which pins to use for the Rx and Tx. However, using ATTiny core, the use of SoftwareSerial is no longer mandatory because directly implemented. On the other hand, in this case, it is necessary to use the pins PA1 and PA2 for FTDI Rx and FTDI Tx.
Not having made this modification I had to modify my card by the addition of jumpers.
Modifying board and schematic with EAGLE¶
To fix this mistake I modified the schematic of my board with EAGLE.
To do:
- I copied the original schematic and renamed it
- I connected PA2 pin to the FTDI Tx and PA0 to one of the resistor’s LED
- I copied the original board file and renamed it with the same name of the new schematic file
Here I met this message:
To solve the problem I needed to:
- Export the NetScript from my Schematic
- Execute this script from the board file
Useful informations in this video:
Modified board¶
Software¶
To send¶
To send the value of the sensor plugged on the INPUT_BOARD I used this function:
void sendByte(int message1, int message2) { sendBuffer[0] = 'b'; // Byte that begin the communication sendBuffer[1] = message1 >> 8; sendBuffer[2] = message1 & 255; sendBuffer[3] = message2 >> 8; sendBuffer[4] = message2 & 255; sendBuffer[5] = '\n'; // Byte that end the communication for (int i = 0; i <= 5; i++) { Serial.write(sendBuffer[i]); } }
To read¶
To read the value send by the INPUT_BOARD on the LED_BOARD I used this function:
void readSerial() { if (Serial.available() > 0) //Get the number of bytes (characters) available for reading from the serial port { Serial.readBytesUntil('\n',messageR, 6); //reads characters from the serial buffer into an array if ( messageR[0] == byte('b')) { message1 = messageR[1] << 8 | messageR[2]; message2 = messageR[3] << 8 | messageR[4]; } else { message1 = -255; message2 = -255; } } }
Connexion between boards¶
- Rx(FTDI) INPUT_BORD -> Tx(FTDI) LED_BORD
- VCC INPUT_BORD -> VCC LED_BORD -> External 5V
- GND INPUT_BORD -> GND LED_BORD -> External GND
- Rx(FTDI) LED_BORD -> Rx of the FTDI friend (to read the received message on the LED_BOARD)