Programme de la première cellule du RC, celle uniquement connectée en BT avec l'autre ObCP. Le code reçoit des infos et renvoi le temps relevé.

Dependencies:   mbed SimpleBLE X_NUCLEO_IDB0XA1 LIS3DH_spi

Committer:
MaxenceGalopin
Date:
Sun Jan 26 11:01:52 2020 +0000
Revision:
12:797136ee9f42
Parent:
11:72c976e0d889
Version final du code d'envoi. Ajout commentaires et explications

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MaxenceGalopin 12:797136ee9f42 1 /*
MaxenceGalopin 12:797136ee9f42 2 Programme pour l'ObCP connecté uniquement à l'autre ObCP.
MaxenceGalopin 12:797136ee9f42 3 C'est la cellule qui fait la porte de départ en mode solo
MaxenceGalopin 12:797136ee9f42 4 La sortie de la photodiode est reliée à la broche A0
MaxenceGalopin 12:797136ee9f42 5 Le laser est alimenté en 3V3 (suffisant) ou en 5V.
MaxenceGalopin 12:797136ee9f42 6 Les pattes RX et TX dumodule HC-06 sont reliées aux broches D0 et D1 (Attention à relier RX à TX et Tx à RX)
MaxenceGalopin 12:797136ee9f42 7 Le shield est fonctionnel (sauf BLE) mais il faut inverser TX et RX pour le HC-05/HC06
MaxenceGalopin 12:797136ee9f42 8 Il comporte aussi 2 leds, 2 boutons, et un transistor pour controler le laser.
MaxenceGalopin 12:797136ee9f42 9 */
MaxenceGalopin 12:797136ee9f42 10 /*
MaxenceGalopin 12:797136ee9f42 11 Penser à mettre à jour les librairies
MaxenceGalopin 12:797136ee9f42 12 */
MaxenceGalopin 12:797136ee9f42 13
jimbaud 6:1670244c4eb4 14 //Includes
jimbaud 6:1670244c4eb4 15
janjongboom 0:ba1c49874d3c 16 #include "mbed.h"
janjongboom 0:ba1c49874d3c 17 #include "SimpleBLE.h"
jimbaud 6:1670244c4eb4 18 #include "LIS3DH.h"
Nthnthj 9:3d83d0b410b8 19 #include "stdlib.h"
janjongboom 0:ba1c49874d3c 20
jimbaud 6:1670244c4eb4 21
Nthnthj 9:3d83d0b410b8 22 //Bluetooth hc05-6
Nthnthj 9:3d83d0b410b8 23
MaxenceGalopin 12:797136ee9f42 24 #define TX D0 //On utilise USBTX et USBRX
Nthnthj 10:a15e07c7ad61 25 #define RX D1
Nthnthj 9:3d83d0b410b8 26
jimbaud 6:1670244c4eb4 27
jimbaud 6:1670244c4eb4 28
jimbaud 6:1670244c4eb4 29
jimbaud 6:1670244c4eb4 30 // GPIO set
jimbaud 6:1670244c4eb4 31
jimbaud 6:1670244c4eb4 32 //Interrupt input
jimbaud 6:1670244c4eb4 33
jimbaud 6:1670244c4eb4 34 InterruptIn user1(PC_13); //User1
Nthnthj 9:3d83d0b410b8 35 InterruptIn boutton1(D3);
Nthnthj 9:3d83d0b410b8 36 InterruptIn boutton2(D4);
Nthnthj 9:3d83d0b410b8 37 InterruptIn event(A0);
Nthnthj 9:3d83d0b410b8 38
MaxenceGalopin 12:797136ee9f42 39 //creation timer
MaxenceGalopin 12:797136ee9f42 40
Nthnthj 9:3d83d0b410b8 41 Timer timer;
Nthnthj 9:3d83d0b410b8 42
MaxenceGalopin 12:797136ee9f42 43 //Sorties numériques
Nthnthj 9:3d83d0b410b8 44 DigitalOut led1(D14);
Nthnthj 9:3d83d0b410b8 45 DigitalOut transistor(D6);
jimbaud 6:1670244c4eb4 46
jimbaud 6:1670244c4eb4 47 //PWM output
jimbaud 6:1670244c4eb4 48
jimbaud 6:1670244c4eb4 49 PwmOut PWMoutput(PB_1); //Main PWM output
jimbaud 6:1670244c4eb4 50 PwmOut Green(PC_8); //PWM Red LED
jimbaud 6:1670244c4eb4 51 PwmOut Red(PC_6); //PWM Green LED
jimbaud 6:1670244c4eb4 52 PwmOut Blue(PC_9); //PWM Blue LED
jimbaud 6:1670244c4eb4 53
MaxenceGalopin 12:797136ee9f42 54 //Création de la liaison série(BT HC-05)
Nthnthj 10:a15e07c7ad61 55 Serial BT(USBTX,USBRX);
Nthnthj 10:a15e07c7ad61 56 //Serial pc(USBTX, USBRX);
Nthnthj 9:3d83d0b410b8 57
Nthnthj 9:3d83d0b410b8 58 int compteur;
Nthnthj 9:3d83d0b410b8 59 bool flag = false;
Nthnthj 9:3d83d0b410b8 60 float end,begin;
Nthnthj 10:a15e07c7ad61 61 int message = 0;
Nthnthj 10:a15e07c7ad61 62 int temps1 = 0;
Nthnthj 10:a15e07c7ad61 63 string course;
Nthnthj 10:a15e07c7ad61 64 char Buffer[10];
Nthnthj 10:a15e07c7ad61 65 int i = 0;
Nthnthj 10:a15e07c7ad61 66 int begin2 = 0;
Nthnthj 10:a15e07c7ad61 67
MaxenceGalopin 12:797136ee9f42 68 //envoi d'un message à l'autre cellule
MaxenceGalopin 12:797136ee9f42 69
Nthnthj 10:a15e07c7ad61 70 void envoi(int message){
Nthnthj 10:a15e07c7ad61 71 BT.printf("%i\n", message);
Nthnthj 10:a15e07c7ad61 72 }
Nthnthj 10:a15e07c7ad61 73
MaxenceGalopin 12:797136ee9f42 74 // fonction appelée lors du déclanchement de la diode
Nthnthj 9:3d83d0b410b8 75 void pressed(){
Nthnthj 10:a15e07c7ad61 76 led1 = !led1;
Nthnthj 10:a15e07c7ad61 77 temps1 = timer.read_ms();
Nthnthj 10:a15e07c7ad61 78 message = temps1;
Nthnthj 10:a15e07c7ad61 79 envoi(message);
Nthnthj 9:3d83d0b410b8 80 }
Nthnthj 9:3d83d0b410b8 81
MaxenceGalopin 12:797136ee9f42 82 /*
Nthnthj 9:3d83d0b410b8 83 void skater_d()
Nthnthj 9:3d83d0b410b8 84 {
Nthnthj 9:3d83d0b410b8 85 if(flag==false) {
Nthnthj 10:a15e07c7ad61 86 // pc.printf("Ligne de depart coupee solo \n");
Nthnthj 9:3d83d0b410b8 87 if( flag == false) {
Nthnthj 9:3d83d0b410b8 88 //printf("Depart skate \n");
Nthnthj 9:3d83d0b410b8 89 begin = timer.read_ms();
Nthnthj 9:3d83d0b410b8 90 compteur=100;
Nthnthj 9:3d83d0b410b8 91 //pc.printf(" skater lance %.0f \n", begin);
Nthnthj 9:3d83d0b410b8 92 flag = true;
Nthnthj 9:3d83d0b410b8 93 } else if(flag == true) {
Nthnthj 9:3d83d0b410b8 94 //printf("erreur \n");
Nthnthj 9:3d83d0b410b8 95 //pc.printf(" Temps du skater : %.0f \n", end-begin);
Nthnthj 9:3d83d0b410b8 96 flag = false;
Nthnthj 9:3d83d0b410b8 97 }
Nthnthj 9:3d83d0b410b8 98 }
Nthnthj 9:3d83d0b410b8 99 else if(flag==true) {
Nthnthj 9:3d83d0b410b8 100 //printf("Ligne d'arrivee coupee \n");
Nthnthj 9:3d83d0b410b8 101 if( flag == false ) {
Nthnthj 9:3d83d0b410b8 102 //printf("arrivee coupe sans depart\n");
Nthnthj 9:3d83d0b410b8 103 flag = true;
Nthnthj 9:3d83d0b410b8 104 } else if(flag == true ) {
Nthnthj 9:3d83d0b410b8 105 //printf("Arrivee skate \n");
Nthnthj 9:3d83d0b410b8 106 end = timer.read_ms();
Nthnthj 9:3d83d0b410b8 107 //Temps = end-begin;
Nthnthj 9:3d83d0b410b8 108 compteur=200;
Nthnthj 9:3d83d0b410b8 109 //wait(1);
Nthnthj 9:3d83d0b410b8 110 //pc.printf(" Temps du skater : %.0f \n", end-begin);
Nthnthj 9:3d83d0b410b8 111 flag = false;
Nthnthj 9:3d83d0b410b8 112 }
Nthnthj 9:3d83d0b410b8 113 }
Nthnthj 9:3d83d0b410b8 114 }
MaxenceGalopin 12:797136ee9f42 115 */
jimbaud 6:1670244c4eb4 116
MaxenceGalopin 12:797136ee9f42 117
MaxenceGalopin 12:797136ee9f42 118 //fonction non utilisée
Nthnthj 10:a15e07c7ad61 119 void test_mode(char Buffer[10]){
Nthnthj 10:a15e07c7ad61 120 if(Buffer[0] == 'S'){
Nthnthj 10:a15e07c7ad61 121 // pc.printf("mode solo \n");
Nthnthj 10:a15e07c7ad61 122 timer.reset();
Nthnthj 10:a15e07c7ad61 123 flag = false;
Nthnthj 10:a15e07c7ad61 124 }else if(Buffer[0]=='D'){
Nthnthj 10:a15e07c7ad61 125 // pc.printf("mode duo \n");
Nthnthj 10:a15e07c7ad61 126 flag=true;
Nthnthj 10:a15e07c7ad61 127 timer.reset();
Nthnthj 10:a15e07c7ad61 128 }
Nthnthj 10:a15e07c7ad61 129 else if (Buffer[0] == 'R'){
Nthnthj 10:a15e07c7ad61 130 // pc.printf("Reset timer \n ");
Nthnthj 10:a15e07c7ad61 131 timer.reset();
Nthnthj 10:a15e07c7ad61 132 //wait(1);
Nthnthj 10:a15e07c7ad61 133 //timer.start();
Nthnthj 10:a15e07c7ad61 134 }
Nthnthj 10:a15e07c7ad61 135 // pc.printf(" Test \n");
Nthnthj 10:a15e07c7ad61 136 }
Nthnthj 10:a15e07c7ad61 137
MaxenceGalopin 12:797136ee9f42 138 // reception d'un message
Nthnthj 10:a15e07c7ad61 139 void RXevent (){
Nthnthj 10:a15e07c7ad61 140 if(BT.readable()){
Nthnthj 10:a15e07c7ad61 141 BT.scanf("%s", &Buffer);
Nthnthj 10:a15e07c7ad61 142 wait(1);
Nthnthj 10:a15e07c7ad61 143 }
Nthnthj 10:a15e07c7ad61 144
Nthnthj 10:a15e07c7ad61 145 if (Buffer[0] == 'R'){
Nthnthj 10:a15e07c7ad61 146 timer.reset();
Nthnthj 10:a15e07c7ad61 147 }
Nthnthj 10:a15e07c7ad61 148 return;
Nthnthj 10:a15e07c7ad61 149 }
Nthnthj 10:a15e07c7ad61 150
Nthnthj 10:a15e07c7ad61 151
Nthnthj 10:a15e07c7ad61 152
jimbaud 6:1670244c4eb4 153 //Main program
jimbaud 6:1670244c4eb4 154
jimbaud 6:1670244c4eb4 155 int main(int, char**)
jimbaud 6:1670244c4eb4 156 {
Nthnthj 9:3d83d0b410b8 157 transistor=1;
Nthnthj 9:3d83d0b410b8 158 timer.start();
Nthnthj 10:a15e07c7ad61 159 BT.attach(&RXevent);
Nthnthj 9:3d83d0b410b8 160 user1.fall(&pressed);
Nthnthj 10:a15e07c7ad61 161 event.fall(&pressed);
MaxenceGalopin 11:72c976e0d889 162
jimbaud 6:1670244c4eb4 163 while (1) {
Nthnthj 9:3d83d0b410b8 164
Nthnthj 10:a15e07c7ad61 165
Nthnthj 10:a15e07c7ad61 166 Green = 0.001;
Nthnthj 10:a15e07c7ad61 167 wait(1);
Nthnthj 10:a15e07c7ad61 168 Green = 0;
Nthnthj 10:a15e07c7ad61 169 wait(1);
Nthnthj 9:3d83d0b410b8 170 //}
jimbaud 6:1670244c4eb4 171
janjongboom 0:ba1c49874d3c 172 }
janjongboom 0:ba1c49874d3c 173 }
Nthnthj 9:3d83d0b410b8 174