Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed tsi_sensor
Diff: main.cpp
- Revision:
- 0:256d00799c21
- Child:
- 1:5b92fb2cd769
diff -r 000000000000 -r 256d00799c21 main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed May 22 13:07:54 2019 +0000
@@ -0,0 +1,210 @@
+#include "mbed.h"
+#include "tsi_sensor.h"
+
+/* This defines will be replaced by PinNames soon */
+#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
+#define ELEC0 9
+#define ELEC1 10
+#elif defined (TARGET_KL05Z)
+#define ELEC0 9
+#define ELEC1 8
+#else
+#error TARGET NOT DEFINED
+#endif
+Ticker tiempo;
+
+enum maquina_estado_general {INICIO, CABLE1, CABLE2, CABLE3, CABLE4, WIN, LOOSER};
+maquina_estado_general maquina_general;
+
+PwmOut display(PTA2);
+TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
+AnalogIn noise (PTC1);
+DigitalIn cable_0 (PTB0);
+DigitalIn cable_1 (PTB1);
+DigitalIn cable_2 (PTB2);
+DigitalIn cable_3 (PTC12);
+DigitalOut verde (LED_GREEN);
+DigitalOut rojo (LED_RED);
+
+int vector_secuencia [4];
+int vector_orden_cables [4] = {-1, -1, -1, -1};
+int ccero = 0;
+int cuno = 0;
+int cdos = 0;
+int ctres = 0;
+int sumador = 0;
+
+void secuencia();
+void maquina_general_sec ();
+//void maquina_display ();
+void maquina_orden_cables ();
+void suma ();
+int cablesDesconectados(int n);
+
+int main(void)
+{
+
+ cable_0.mode (PullUp);
+ cable_1.mode (PullUp);
+ cable_2.mode (PullUp);
+ cable_3.mode (PullUp);
+
+
+ srand (int(noise * 10000));
+
+ secuencia ();
+ verde = 1;
+ rojo = 1;
+
+ while (true) {
+ maquina_general_sec ();
+ //maquina_display ();
+ maquina_orden_cables ();
+ printf("%d %d %d %d", vector_orden_cables[0], vector_orden_cables[1], vector_orden_cables[2], vector_orden_cables[3]);
+ }
+}
+
+void suma ()
+{
+ sumador++;
+}
+
+void secuencia ()
+{
+ vector_secuencia [0] = rand () %4;
+ for (int i = 1; i < 4; i++) {
+ int randomm = rand () %4;
+ if (vector_secuencia [i - 1] != randomm)
+ vector_secuencia [i] = randomm;
+ else
+ i--;
+ }
+
+}
+
+
+void maquina_general_sec ()
+{
+ switch (maquina_general) {
+
+ default:
+ maquina_general = INICIO;
+ break;
+
+ case INICIO:
+ if (tsi.readPercentage() > 0.5){
+ maquina_general = CABLE1;
+ verde = 0;
+ }
+ else
+ maquina_general = INICIO;
+ break;
+
+ case CABLE1:
+ verde = 1;
+ if (vector_secuencia [0] == vector_orden_cables [0] && vector_orden_cables [0] != -1) {
+ maquina_general = CABLE2;
+ }
+ else {
+ maquina_general = CABLE1;
+ }
+ if (vector_secuencia [0] != vector_orden_cables [0] && vector_orden_cables [0] != -1) {
+ maquina_general = LOOSER;
+ }
+
+ break;
+
+ case CABLE2:
+ rojo = 0;
+ if (vector_secuencia [1] == vector_orden_cables [1] && vector_orden_cables [1] != -1) {
+ maquina_general = CABLE3;
+ tiempo.attach (&suma, 1);
+ }
+ else {
+ maquina_general = CABLE2;
+ }
+ if (vector_secuencia [1] != vector_orden_cables [1] && vector_orden_cables [1] != -1) {
+ maquina_general = LOOSER;
+ }
+
+ break;
+
+ case CABLE3:
+ rojo = 1;
+ if (vector_secuencia [2] == vector_orden_cables [2] && vector_orden_cables [2] != -1) {
+ maquina_general = CABLE4;
+ tiempo.attach (&suma, 1);
+ }
+ else {
+ maquina_general = CABLE3;
+ }
+ if (vector_secuencia [2] != vector_orden_cables [2] && vector_orden_cables [2] != -1) {
+ maquina_general = LOOSER;
+ }
+ break;
+
+ case CABLE4:
+ rojo = 0;
+ if (vector_secuencia [3] == vector_orden_cables [3] && vector_orden_cables [3] != -1) {
+ maquina_general = WIN;
+ }
+ else {
+ maquina_general = CABLE4;
+ }
+ if (vector_secuencia [3] != vector_orden_cables [3] && vector_orden_cables [3] != -1) {
+ maquina_general = LOOSER;
+ }
+ break;
+
+ case WIN:
+ verde = 0;
+ break;
+ case LOOSER:
+ verde = 0;
+ rojo = 0;
+ break;
+
+ }
+}
+
+void maquina_orden_cables ()
+{
+ static int a = 0;
+
+ if (cable_0 != 0 && cablesDesconectados(0) == 0) {
+ ccero = 0;
+ vector_orden_cables [a] = ccero;
+ a++;
+ printf ("vector orden cables [%d] = 0", &a);
+
+ }
+ if (cable_1 != 0 && cablesDesconectados(1) == 0) {
+ cuno = 1;
+ vector_orden_cables [a] = cuno;
+ a++;
+ printf ("vector orden cables [%d] = 1", &a);
+
+ }
+ if (cable_2 != 0 && cablesDesconectados(2) == 0) {
+ cdos = 2;
+ vector_orden_cables [a] = cdos;
+ a++;
+ printf ("vector orden cables [%d] = 2", &a);
+ }
+ if (cable_3 != 0 && cablesDesconectados(3) != 0) {
+ ctres = 3;
+ vector_orden_cables [a] = ctres;
+ a++;
+ printf ("vector orden cables [%d] = 3", &a);
+ }
+}
+
+int cablesDesconectados(int n)
+{
+
+ for(int j = 0; j < 4; j++) {
+ if(vector_orden_cables[j] == n)
+ return 1;
+ }
+ return 0;
+}