Working reset, flipped logic
Dependencies: SDFileSystem emic2 mbed-rtos mbed
Fork of BAT_senior_design_Nhi by
Revision 19:ceac47be2e64, committed 2017-10-27
- Comitter:
- aismail1997
- Date:
- Fri Oct 27 15:15:00 2017 +0000
- Parent:
- 18:d14bf57f435b
- Child:
- 20:abbc12fca525
- Commit message:
- cleaned up button code, added buttonarray class
Changed in this revision
--- a/button.cpp Fri Oct 27 13:34:49 2017 +0000
+++ b/button.cpp Fri Oct 27 15:15:00 2017 +0000
@@ -3,7 +3,7 @@
// button constructor
button::button(PwmOut servo, DigitalIn pb)
- : servo(servo), pb(pb) {}
+ : servo(servo), pb(pb), press(0), state(0) {}
//button::button(PwmOut servo, DigitalIn pb, AnalogIn lp)
// : servo(servo), pb(pb), linpot(lp), mode(0), state(0) {}
@@ -16,22 +16,28 @@
}
// get servo pin
-/*void button::setState(int mystate)
+void button::setState(int mystate)
{
state = mystate;
}
// get servo pin
-void button::setMode(int mymode)
+/*void button::setMode(int mymode)
{
mode = mymode;
}*/
// get current state of the button
-/*int button::getState()
+int button::getState()
{
return state;
-}*/
+}
+
+int button::getPress()
+{
+ return press;
+}
+
// move servo into the slot
void button::moveServoIn()
@@ -53,3 +59,36 @@
wait(0.01);
}
}
+
+int button::updateState()
+{
+ //myled = 0;
+ // state 0 - button is up, pb = 0
+ if (pb == 0 && state == 3) {
+ // nothing happens here, servo is still
+ state = 0;
+ }
+ // state 1 - button is moving down, pb = 1
+ if (pb == 1 && state == 0) {
+ moveServoIn();
+ state = 1;
+ press = 1;
+ }
+ // state 2 - button is down, pb = 0
+ if (pb == 0 && state == 1) {
+ // nothing happens here, servo is still
+ state = 2;
+ }
+ // state 3 - button is moving up, pb = 1
+ if (pb == 1 && state == 2) {
+ moveServoOut();
+ state = 3;
+ press = 0;
+ }
+ // state 4 - handle debouncing while button is down
+ /*if (pb1 = 1 && state == 2) {
+ count++;
+ }*/
+ return state;
+}
+
--- a/button.h Fri Oct 27 13:34:49 2017 +0000
+++ b/button.h Fri Oct 27 15:15:00 2017 +0000
@@ -1,5 +1,8 @@
#include "mbed.h"
+#ifndef BUTTON_H
+#define BUTTON_H
+
// This is a button class for our custom button
class button {
@@ -7,20 +10,27 @@
private:
PwmOut servo;
DigitalIn pb;
- // int state; // is the button up or down
+ int state; // where is the button (0 - 4)
+ int press; // is the button up or down
// int mode; // is the system in reading or typing mode
// AnalogIn linpot;
-
+
public:
// constructors
button(); // Default
button(PwmOut servo, DigitalIn pb);
+
// button(PwmOut servo, DigitalIn pb, AnalogIn linpot);
// functions
PwmOut getServoPin(); // get the servo pin
//void setState(int state); // set state
//void setMode(int mode); // set mode
- //int getState(); // determine what state the button is in - up or down
+ void setState(int); // determine what state the button is in - up or down
void moveServoIn(); // move servo into the slot
void moveServoOut(); // move servo out of the slot
-};
\ No newline at end of file
+ int updateState();
+ int getState();
+ int getPress();
+};
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/buttonArray.cpp Fri Oct 27 15:15:00 2017 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+#include "buttonArray.h"
+
+// button constructor
+buttonArray::buttonArray(button b1, button b2, button b3, button b4, button b5, button b6)
+ : button1(b1), button2(b2), button3(b3), button4(b4), button5(b5), button6(b6) {}
+//button::button(PwmOut servo, DigitalIn pb, AnalogIn lp)
+// : servo(servo), pb(pb), linpot(lp), mode(0), state(0) {}
+
+// FUNCTIONS
+int buttonArray::checkVal() {
+ char* val;
+ sprintf(val, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
+ button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
+ if (val == "000000")
+ return 1;
+ else
+ return 0;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/buttonArray.h Fri Oct 27 15:15:00 2017 +0000
@@ -0,0 +1,28 @@
+#include "mbed.h"
+#include "button.h"
+
+// This is a button class for our custom button
+class buttonArray {
+
+// buttons
+private:
+// 6 buttons here
+ button button1;
+ button button2;
+ button button3;
+ button button4;
+ button button5;
+ button button6;
+ int currVal;
+
+public:
+ // constructors
+ buttonArray(); // Default
+ buttonArray(button button1, button button2, button button3, button button4, button button5, button button6);
+ // functions
+ //int getState(); // determine what state the button is in - up or down
+ //void moveServoIn(); // move servo into the slot
+ //void moveServoOut(); // move servo out of the slot
+ int checkVal();
+
+};
\ No newline at end of file
--- a/main.cpp Fri Oct 27 13:34:49 2017 +0000
+++ b/main.cpp Fri Oct 27 15:15:00 2017 +0000
@@ -3,6 +3,7 @@
#include "wave_player.h"
#include "SDFileSystem.h"
#include "button.h"
+#include "buttonArray.h"
// DEFINE I/O
PwmOut myservo(p21);
@@ -34,6 +35,8 @@
button button5(myservo5, pb5);
button button6(myservo6, pb6);
+buttonArray buttonarr(button1, button2, button3, button4, button5, button6);
+
// INITIALIZE VARIABLES
// add mode, reset buttons
int start = 0;
@@ -65,69 +68,15 @@
void button_thread()
{
while(true) {
- // state 0 - button is up, pb = 0
- if (pb1 == 0 && state == 3) {
- // nothing happens here, servo is still
- state = 0;
- }
- // state 1 - button is moving down, pb = 1
- if (pb1 == 1 && state == 0) {
- button1.moveServoIn();
- state = 1;
- }
- // state 2 - button is down, pb = 0
- if (pb1 == 0 && state == 1) {
- // nothing happens here, servo is still
- state = 2;
- }
- // state 3 - button is moving up, pb = 1
- if (pb1 == 1 && state == 2) {
- button1.moveServoOut();
- state = 3;
- }
- // state 4 - handle debouncing while button is down
- /*if (pb1 = 1 && state == 2) {
- count++;
- }*/
+ state = button1.updateState();
+ Thread::wait(100); // wait till thread is done
}
}
void button2_thread()
{
while(true) {
- // state 0 - button is up, pb = 0
- if (pb2 == 0 && state2 == 3) {
- // nothing happens here, servo is still
- state2 = 0;
- //led3 = 0;
- //led4 = 0;
- }
- // state 1 - button is moving down, pb = 1
- if (pb2 == 1 && state2 == 0) {
- button2.moveServoIn();
- state2 = 1;
- //led3 = 0;
- //led4 = 1;
- }
- // state 2 - button is down, pb = 0
- if (pb2 == 0 && state2 == 1) {
- // nothing happens here, servo is still
- state2 = 2;
- //led3 = 1;
- //led4 = 0;
- }
- // state 3 - button is moving up, pb = 1
- if (pb2 == 1 && state2 == 2) {
- button2.moveServoOut();
- state2 = 3;
- //led3 = 1;
- //led4 = 1;
- }
- // state 4 - handle debouncing while button is down
- /*if (pb2 = 1 && state2 == 2) {
-
- }*/
-
+ state2 = button2.updateState();
Thread::wait(100); // wait till thread is done
}
}
@@ -136,31 +85,8 @@
void button3_thread()
{
while(true) {
- // state 0 - button is up, pb = 0
- if (pb3 == 0 && state3 == 3) {
- // nothing happens here, servo is still
- state3 = 0;
- }
- // state 1 - button is moving down, pb = 1
- if (pb3 == 1 && state3 == 0) {
- button3.moveServoIn();
- state3 = 1;
- }
- // state 2 - button is down, pb = 0
- if (pb3 == 0 && state3 == 1) {
- // nothing happens here, servo is still
- state3 = 2;
- }
- // state 3 - button is moving up, pb = 1
- if (pb3 == 1 && state3 == 2) {
- button3.moveServoOut();
- state3 = 3;
- }
- // state 4 - handle debouncing while button is down
- /*if (pb3 = 1 && state3 == 2) {
-
- }*/
-
+ state3 = button3.updateState();
+ Thread::wait(100); // wait till thread is done
}
}
@@ -168,64 +94,17 @@
void button4_thread()
{
while(true) {
- // state 0 - button is up, pb = 0
- if (pb4 == 0 && state4 == 3) {
- // nothing happens here, servo is still
- state4 = 0;
- }
- // state 1 - button is moving down, pb = 1
- if (pb4 == 1 && state4 == 0) {
- button4.moveServoIn();
- state4 = 1;
- }
- // state 2 - button is down, pb = 0
- if (pb4 == 0 && state4 == 1) {
- // nothing happens here, servo is still
- state4 = 2;
- }
- // state 3 - button is moving up, pb = 1
- if (pb4 == 1 && state4 == 2) {
- button4.moveServoOut();
- state4 = 3;
- }
- // state 4 - handle debouncing while button is down
- /*if (pb4 = 1 && state4 == 2) {
-
- }*/
-
+ state4 = button4.updateState();
+ Thread::wait(100); // wait till thread is done
}
}
// thread for the custom button
void button5_thread()
{
-
while(true) {
- // state 0 - button is up, pb = 0
- if (pb5 == 0 && state5 == 3) {
- // nothing happens here, servo is still
- state5 = 0;
- }
- // state 1 - button is moving down, pb = 1
- if (pb5 == 1 && state5 == 0) {
- button5.moveServoIn();
- state5 = 1;
- }
- // state 2 - button is down, pb = 0
- if (pb5 == 0 && state5 == 1) {
- // nothing happens here, servo is still
- state5 = 2;
- }
- // state 3 - button is moving up, pb = 1
- if (pb5 == 1 && state5 == 2) {
- button5.moveServoOut();
- state5 = 3;
- }
- // state 4 - handle debouncing while button is down
- /*if (pb5 = 1 && state5 == 2) {
-
- }*/
-
+ state5 = button5.updateState();
+ Thread::wait(100); // wait till thread is done
}
}
@@ -233,31 +112,8 @@
void button6_thread()
{
while(true) {
- // state 0 - button is up, pb = 0
- if (pb6 == 0 && state6 == 3) {
- // nothing happens here, servo is still
- state6 = 0;
- }
- // state 1 - button is moving down, pb = 1
- if (pb6 == 1 && state6 == 0) {
- button6.moveServoIn();
- state6 = 1;
- }
- // state 2 - button is down, pb = 0
- if (pb6 == 0 && state6 == 1) {
- // nothing happens here, servo is still
- state6 = 2;
- }
- // state 3 - button is moving up, pb = 1
- if (pb6 == 1 && state6 == 2) {
- button6.moveServoOut();
- state6 = 3;
- }
- // state 4 - handle debouncing while button is down
- /*if (pb6 = 1 && state6 == 2) {
-
- }*/
-
+ state6 = button6.updateState();
+ Thread::wait(100); // wait till thread is done
}
}
@@ -299,17 +155,17 @@
myservo2 = i/100.0;
wait(0.01);
}
-
+
for(int i=0; i<=3; i++) {
myservo3 = i/100.0;
wait(0.01);
}
-
+
for(int i=0; i<=3; i++) {
myservo4 = i/100.0;
wait(0.01);
}
-
+
for(int i=0; i<=3; i++) {
myservo5 = i/100.0;
wait(0.01);
@@ -319,7 +175,7 @@
myservo6 = i/100.0;
wait(0.01);
}
-
+
//led1 = 1;
//led2 = 1;
Thread t1(button_thread);
@@ -363,7 +219,7 @@
//if (linpot < 0.5) {
//float potval = linpot;
//pc.printf("linear pot: %f\n", potval);
-
+
// MAIN THREAD
while(true) {
Thread::wait(500); // wait till thread is done
