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:
- 6:f7028034aabb
- Parent:
- 5:4e5c644d5cc3
- Child:
- 7:b8de1529c7fc
--- a/main.cpp Thu Nov 15 08:43:32 2018 +0000
+++ b/main.cpp Thu Nov 15 15:57:10 2018 +0000
@@ -25,6 +25,8 @@
DigitalOut Phase3 (p23);
DigitalOut Phase4 (p24);
+AnalogOut Aout(p18);
+
DigitalIn Button1 (p11);
DigitalIn Button2 (p12);
@@ -35,6 +37,9 @@
int StateA = 0;
int StateB = 0;
+//int StateC = 0;
+int AdjCW = 2;
+int AdjACW = 5;
int TimePerClick = 0;
int TimePerRev = 0;
int RPS = 0;
@@ -52,19 +57,27 @@
Initialisation();
wait(0.1);
t.start();
-
+
while(wheel.getRevolutions()==0)
- {
+ {
switch(StateA)
{
- case 0:Ph1();pc.printf("Ph1= %i\n\r", wheel.getPulses());break;
- case 1:Ph12();pc.printf("Ph12= %i\n\r", wheel.getPulses());break;
- case 2:Ph2();pc.printf("Ph2= %i\n\r", wheel.getPulses());break;
- case 3:Ph23();pc.printf("Ph23= %i\n\r", wheel.getPulses());break;
- case 4:Ph3();pc.printf("Ph3= %i\n\r", wheel.getPulses());break;
- case 5:Ph34();pc.printf("Ph34= %i\n\r", wheel.getPulses());break;
- case 6:Ph4();pc.printf("Ph4= %i\n\r", wheel.getPulses());break;
- case 7:Ph41();pc.printf("Ph41= %i\n\r", wheel.getPulses());break;
+ case 0:Ph1();break;
+ case 1:Ph1();break;
+ case 2:Ph12();break;
+ case 3:Ph12();break;
+ case 4:Ph2();break;
+ case 5:Ph2();break;
+ case 6:Ph23();break;
+ case 7:Ph23();break;
+ case 8:Ph3();break;
+ case 9:Ph3();break;
+ case 10:Ph34();break;
+ case 11:Ph34();break;
+ case 12:Ph4();break;
+ case 13:Ph4();break;
+ case 14:Ph41();break;
+ case 15:Ph41();break;
default:break;
}
@@ -72,7 +85,7 @@
{
StateA++;
wheel.ResetYay();
- if (StateA>7)
+ if (StateA>15)
{
StateA=0;
}
@@ -83,40 +96,41 @@
{
while((led1 == 0) && (led2 == 0))
{
- GetChar();
- StateB = (wheel.getPulses())%16;
Phase1 = 0;
Phase2 = 0;
Phase3 = 0;
Phase4 = 0;
- pc.printf("StateA= %i\r", StateA);
+ GetChar();
+ //StateB = wheel.getPulses()%16;
+ //StateC = (800+wheel.getPulses()+StateA+AdjCW)%16;
+ //pc.printf("StateA= %i, StateB= %i, StateC= %i, Pulses = %i\n\r", StateA, StateB, StateC, wheel.getPulses());
//pc.printf("0 StateB= %i, Pulses= %i, Revs= %i\r", StateB,wheel.getPulses(),wheel.getRevolutions());
}
while((wheel.getRevolutions()>0) && (wheel.getPulses()>0) && (led1==1))
{
GetChar();
- StateB = (wheel.getPulses())%16;
- pc.printf("StateA= %i\r", StateA);
+ StateB = (wheel.getPulses()+StateA+AdjCW)%16;
+ //pc.printf("StateB= %i\n\r", StateB);
//pc.printf("1 StateB= %i, Pulses= %i, Revs= %i\r", StateB,wheel.getPulses(),wheel.getRevolutions());
switch(StateB)
{
- case 0:Ph3();break;
- case 1:Ph3();break;
- case 2:Ph34();break;
- case 3:Ph34();break;
- case 4:Ph4();break;
- case 5:Ph4();break;
- case 6:Ph41();break;
- case 7:Ph41();break;
- case 8:Ph1();break;
- case 9:Ph1();break;
- case 10:Ph12();break;
- case 11:Ph12();break;
- case 12:Ph2();break;
- case 13:Ph2();break;
- case 14:Ph23();break;
- case 15:Ph23();break;
+ case 0:Ph1();break;
+ case 1:Ph1();break;
+ case 2:Ph12();break;
+ case 3:Ph12();break;
+ case 4:Ph2();break;
+ case 5:Ph2();break;
+ case 6:Ph23();break;
+ case 7:Ph23();break;
+ case 8:Ph3();break;
+ case 9:Ph3();break;
+ case 10:Ph34();break;
+ case 11:Ph34();break;
+ case 12:Ph4();break;
+ case 13:Ph4();break;
+ case 14:Ph41();break;
+ case 15:Ph41();break;
default:break;
}
@@ -129,28 +143,28 @@
while(wheel.getRevolutions()>0 && wheel.getPulses()<1 && (led1==1))
{
GetChar();
- StateB = (800+wheel.getPulses())%16;
- pc.printf("StateA= %i\r", StateA);
+ StateB = (800+wheel.getPulses()+StateA+AdjCW)%16;
+ //pc.printf("StateA= %i\r", StateA);
//pc.printf("2 StateB= %i, Pulses= %i, Revs= %i\r", StateB,wheel.getPulses(),wheel.getRevolutions());
switch(StateB)
{
- case 0:Ph3();break;
- case 1:Ph3();break;
- case 2:Ph34();break;
- case 3:Ph34();break;
- case 4:Ph4();break;
- case 5:Ph4();break;
- case 6:Ph41();break;
- case 7:Ph41();break;
- case 8:Ph1();break;
- case 9:Ph1();break;
- case 10:Ph12();break;
- case 11:Ph12();break;
- case 12:Ph2();break;
- case 13:Ph2();break;
- case 14:Ph23();break;
- case 15:Ph23();break;
+ case 0:Ph1();break;
+ case 1:Ph1();break;
+ case 2:Ph12();break;
+ case 3:Ph12();break;
+ case 4:Ph2();break;
+ case 5:Ph2();break;
+ case 6:Ph23();break;
+ case 7:Ph23();break;
+ case 8:Ph3();break;
+ case 9:Ph3();break;
+ case 10:Ph34();break;
+ case 11:Ph34();break;
+ case 12:Ph4();break;
+ case 13:Ph4();break;
+ case 14:Ph41();break;
+ case 15:Ph41();break;
default:break;
}
@@ -162,34 +176,31 @@
while((wheel.getRevolutions()>0) && (wheel.getPulses()>0) && (led2==1))
{
GetChar();
- StateB = (800+wheel.getPulses())%16;
- pc.printf("StateA= %i\r", StateA);
- //pc.printf("StateA= %i\n\r", StateA);
+ //StateB = (800+wheel.getPulses())%16;
+ StateB = (800+wheel.getPulses()+StateA+AdjACW)%16;
+ //pc.printf("StateA= %i\r", StateA);
//pc.printf("3 StateB= %i, Pulses= %i, Revs= %i \r", StateB,wheel.getPulses(),wheel.getRevolutions());
switch(StateB)
{
- case 15:Ph34();break;
- case 14:Ph3();break;
- case 13:Ph3();break;
- case 12:Ph23();break;
- case 11:Ph23();break;
- case 10:Ph2();break;
- case 9:Ph2();break;
- case 8:Ph12();break;
- case 7:Ph12();break;
- case 6:Ph1();break;
- case 5:Ph1();break;
- case 4:Ph41();break;
- case 3:Ph41();break;
- case 2:Ph4();break;
- case 1:Ph4();break;
- case 0:Ph34();break;
+ case 15:Ph41();break;
+ case 14:Ph41();break;
+ case 13:Ph4();break;
+ case 12:Ph4();break;
+ case 11:Ph34();break;
+ case 10:Ph34();break;
+ case 9:Ph3();break;
+ case 8:Ph3();break;
+ case 7:Ph23();break;
+ case 6:Ph23();break;
+ case 5:Ph2();break;
+ case 4:Ph2();break;
+ case 3:Ph12();break;
+ case 2:Ph12();break;
+ case 1:Ph1();break;
+ case 0:Ph1();break;
default:break;
}
- if (StateB<0)
- {
- StateB = 800+wheel.getPulses();
- }
+
if(wheel.getYay()==1)
{
RPM();
@@ -198,34 +209,30 @@
while((wheel.getRevolutions()>0) && (wheel.getPulses()<1) && (led2==1))
{
GetChar();
- StateB = (800+wheel.getPulses())%16;
- pc.printf("StateA= %i\r", StateA);
+ StateB = (800+wheel.getPulses()+StateA+AdjACW)%16;
+ //pc.printf("StateA= %i\r", StateA);
//pc.printf("4 StateB= %i, Pulses= %i, Revs= %i \r", StateB,wheel.getPulses(),wheel.getRevolutions());
switch(StateB)
{
- case 15:Ph34();break;
- case 14:Ph3();break;
- case 13:Ph3();break;
- case 12:Ph23();break;
- case 11:Ph23();break;
- case 10:Ph2();break;
- case 9:Ph2();break;
- case 8:Ph12();break;
- case 7:Ph12();break;
- case 6:Ph1();break;
- case 5:Ph1();break;
- case 4:Ph41();break;
- case 3:Ph41();break;
- case 2:Ph4();break;
- case 1:Ph4();break;
- case 0:Ph34();break;
+ case 15:Ph41();break;
+ case 14:Ph41();break;
+ case 13:Ph4();break;
+ case 12:Ph4();break;
+ case 11:Ph34();break;
+ case 10:Ph34();break;
+ case 9:Ph3();break;
+ case 8:Ph3();break;
+ case 7:Ph23();break;
+ case 6:Ph23();break;
+ case 5:Ph2();break;
+ case 4:Ph2();break;
+ case 3:Ph12();break;
+ case 2:Ph12();break;
+ case 1:Ph1();break;
+ case 0:Ph1();break;
default:break;
}
- if (StateB<0)
- {
- StateB = 800+wheel.getPulses();
- }
- if(wheel.getYay()==1)
+ if(wheel.getYay()==1)
{
RPM();
}
@@ -346,9 +353,11 @@
wheel.ResetYay();
TimePerClick = (t.read_us());
t.reset();
- TimePerRev = TimePerClick * 400;
+ TimePerRev = TimePerClick * 800;
TimePerRev = TimePerRev / 1000;
RPS = 10000000 / TimePerRev;
rpm = (RPS * 60)/10000;
+ Aout=((0.30303*rpm)/1000); // for 500 rpm (0.3030*500/1000)*3.3V = 0.500V
//pc.printf("rpm = %d\n\r", rpm);
+ //pc.printf("StateA= %i, StateB= %i, StateC= %i, Pulses = %i\n\r", StateA, StateB, StateC, wheel.getPulses());
}
\ No newline at end of file