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.
Diff: main.cpp
- Revision:
- 2:2ae29a54a3de
- Parent:
- 0:912303e63cbd
diff -r d6a8c89250fa -r 2ae29a54a3de main.cpp
--- a/main.cpp Wed Jun 07 13:46:53 2017 +0000
+++ b/main.cpp Thu Dec 06 13:49:38 2018 +0000
@@ -1,23 +1,81 @@
#include "mbed.h"
-
-InterruptIn mybutton(USER_BUTTON);
-DigitalOut myled(LED1);
-
-float delay = 1.0; // 1 sec
-
-void pressed()
+/*
+Pinlayout of encoder
+Pin_encoder Pin_nucleo Color Assesment
+1 5V white GND
+2 GND brown UB
+3 A0 green A+
+4 - yellow A-
+5 A1 grey B+
+6 - pink B-
+7 A2 blue R+
+8 - red R-
+
+*/
+/************************
+ CONNECTED PINS
+************************/
+InterruptIn encoderA(A0);
+//InterruptIn encoderB(A1);
+InterruptIn encoderR(A2);
+DigitalIn encoderB_state(A1);
+
+//Serial connection via USB.
+Serial serial(SERIAL_TX, SERIAL_RX);
+
+/************************
+ GLOBAL VARIABLES
+************************/
+float delay = 0.02; // 50Hz the encoder values are sent.
+int pos = 0; // Position of the encoder.
+bool refference_puls = 0; // When the refference pulse is reached, a different message is sent to the UDO NEO FULL
+
+/************************
+ FUNCTIONS
+************************/
+//Encoder interrupt A
+void encAInterrupt()
{
- if (delay == 1.0)
- delay = 0.2; // 200 ms
- else
- delay = 1.0; // 1 sec
+ if (encoderB_state) pos++; //myled = true; }
+ else pos--; //myled = false; }
+}
+
+//Encoder interrupt B
+//This will later be implemented
+void encBInterrupt()
+{
+ //To be implemented.
}
-
+
+//Encoder refference interrupt R
+void encRInterrupt()
+{
+ pos=0;
+ refference_puls = 1;
+}
+
+/************************
+ MAIN PROGRAM
+************************/
int main()
{
- mybutton.fall(&pressed);
+ //Encoder interrupts
+ encoderA.fall(&encAInterrupt);
+ //encoderB.fall(&encBInterrupt);
+ encoderR.fall(&encRInterrupt);
+
+ //Serial parameters (baudrate=9600, 8-bits, no parity, 1 stopbit)
+ serial.baud(9600);
+ serial.format(8,Serial::None,1);
+
while (1) {
- myled = !myled;
- wait(delay);
+ //When the refference puls is detected.
+ if (refference_puls) {
+ serial.printf("%d:",pos); //position with ':' instead of ';' means the calibration point is met.
+ refference_puls = false;
+ } else {
+ serial.printf("%d;",pos);
+ }
+ wait(delay); //Wait, because the UDO NEO FULL is not as fast as this board.
}
}