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: Motordriver TextLCD mbed
Fork of TextLCD_HelloWorld by
Revision 3:a59397561c9d, committed 2012-10-11
- Comitter:
- luckybug38
- Date:
- Thu Oct 11 20:59:05 2012 +0000
- Parent:
- 2:ad0b044d0a10
- Commit message:
- PROJECT3;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motordriver.lib Thu Oct 11 20:59:05 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/farbodjam/code/Motordriver/#541fb1742c8b
--- a/main.cpp Sat Dec 04 11:31:07 2010 +0000
+++ b/main.cpp Thu Oct 11 20:59:05 2012 +0000
@@ -1,10 +1,430 @@
-// Hello World! for the TextLCD
+#include "mbed.h"
+#include "TextLCD.h"
+#include <string.h>
+#include "motordriver.h"
+#include "mpr121.h"
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+DigitalIn a(p5);
+DigitalIn b(p6);
+DigitalIn c(p7);
+DigitalIn d(p8);
+
+InterruptIn interrupt(p11);
+// Setup the i2c bus on pins 28 and 27
+I2C i2c(p9, p10);
+// Setup the Mpr121:
+// constructor(i2c object, i2c address of the mpr121)
+Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
+
+//Motor
+Motor left(p21, p22, p23, 1); // pwm, fwd, rev, has brake feature
+Motor right(p26, p25, p24, 1);
+
+//TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
+TextLCD lcd(p15, p16, p17, p18, p19, p20);
+Serial pc(USBTX, USBRX); // tx, rx
+Serial device(p13, p14); // tx, rx
+
+//forward or in reverse mode
+int forward;
+
+//stores directions
+int direction[10];
+
+//stores speeds
+float speed[10];
+
+//stores durations
+int duration[10];
+
+//speed of the car
+float car_speed;
+
+//duration of the car
+int car_duration;
+
+//stores the command of the touchpad
+int _command;
+
+//check if there has been input to the touchpad
+bool no_command;
+
+//count variable for card reader
+int count=0;
+
+//Number of total moves
+int num_moves;
+
+//stores buzzcard information
+char output[50];
+
+//number of card swipes on the reader
+int num_swipe=0;
+int o_a, o_b;
+//int count,ch, ignore_count;
+int command[12];
+int count_1=0;
+
+
+//password variables
+int p_1, p_2,p_3,p_4;
-#include "mbed.h"
-#include "TextLCD.h"
+//interrupt routine for touchpad
+void fallInterrupt() {
+ no_command = true;
+ int s2=0;
+ int key_code=0;
+ int i=0;
+ int value=mpr121.read(0x00);
+ value +=mpr121.read(0x01)<<8;
+ // LED demo mod by J. Hamblen
+ //pc.printf("MPR value: %x \r\n", value);
+ i=0;
+ // puts key number out to LEDs for demo
+ for (i=0; i<12; i++) {
+ if (((value>>i)&0x01)==1) key_code=i+1;
+ }
+
+ s2 = key_code - 1;
+ if(s2 > 0){
+
+ _command=s2;
+ no_command = false;
+ led4=s2 & 0x01;
+ led3=(s2>>1) & 0x01;
+ led2=(s2>>2) & 0x01;
+ led1=(s2>>3) & 0x01;
+ }
+
+ count_1++;
+ //pc.printf("hello %f\n",s);
+
+}
+
+//function to turn left
+void turnLeft(){
+ lcd.cls();
+ lcd.printf("Turning Left");
+ left.speed(0);
+ right.speed(0.4);
+ wait(2);
+ right.speed(0);
+ wait(1);
+}
+
+//function to turn right
+void turnRight(){
+ lcd.cls();
+ lcd.printf("Turning Right");
+ left.speed(0.4);
+ right.speed(0);
+ wait(2);
+ left.speed(0);
+ wait(1);
+}
+
+//function to turn around
+void turnAround(){
+ lcd.cls();
+ lcd.printf("Turning Around");
+
+ left.speed(0.4);
+ right.speed(0);
+ wait(4);
+ left.speed(0);
+ wait(1);
+}
+
+
+//to move with input speed and duration of time
+void go(float _speed, int _time){
+ lcd.cls();
+ lcd.printf("Speed %f Duration %d sec", _speed, _time);
+ left.speed(_speed);
+ right.speed(_speed);
+ wait(_time);
+ left.speed(0);
+ right.speed(0);
+ wait(1);
+}
+
+//starts the card reader and verifies the user
+int cardReader(){
+ num_swipe++;
+ int count=0;
+ int ch=0;
+ int ignore_count=0;
+ // char output[50];
+ while(1){
+ if(device.readable()){
-TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
+ ch=device.getc();
+ if(ignore_count>5){
+ output[count]=ch;
+ count++;
+ }
+ ignore_count++;
+ pc.putc(ch);
+ }
+ if (ignore_count>14){
+ lcd.cls();
+
+ if (strcmp(output,"902512409")==0){
+ lcd.printf("Confirmed\n");
+ lcd.printf("Welcome Sehoon\n");
+ for (int i=0; i<1000; i++){
+ if(device.readable())
+ device.getc();
+ }
+ return 1;
+ }
+ else if (strcmp(output,"902536564")==0){
+ lcd.printf("Confirmed\n");
+ lcd.printf("Welcome Tanvir\n");
+ for (int i=0; i<1000; i++){
+ if(device.readable())
+ device.getc();
+ }
+ return 1;
+ }
+ else {
+ for (int i=0; i<100; i++){
+ if(device.readable())
+ ch=device.getc();
+ wait(.01);
+ }
+ if (num_swipe==5){
+ lcd.printf("Too many tries\nEnter password");
+
+ _command=0;
+ no_command=true;
+ while(1){
+ while(no_command){
+ wait(0.1);
+ }
+ p_1=_command;
+ _command=0;
+ no_command=true;
+ while(no_command){
+ wait(0.1);
+ }
+ p_2=_command;
+ _command=0;
+ no_command=true;
+ while(no_command){
+ wait(0.1);
+ }
+ p_3=_command;
+ _command=0;
+ no_command=true;
+ while(no_command){
+ wait(0.1);
+ }
+ p_4=_command;
+ _command=0;
+ no_command=true;
+
+ if (p_1==1&&p_2==3&&p_3==5&&p_4==7)
+ {
+ lcd.cls();
+ lcd.printf("Correct!!!");
+ wait(2);
+ lcd.cls();
+ lcd.printf("Try swiping AGAIN!!");
+ break;
+ }
+ else{
+ lcd.cls();
+ lcd.printf("WRONG WRONG wROGNWRWEWERWER!@#!@");
+ wait(2);
+ lcd.cls();
+ }
+
+ }
+
+
+ }
+ else
+ lcd.printf("Try again\n");
+ return 0;
+ }
+ }
+ }
+
+}
+
+//number of moves for the car
+int number_of_moves(){
+ //select duration
+ //Selecting number of moves
+ lcd.printf("Select Number of Moves");
+ num_moves=100;
+ _command=100;
+ no_command = true;
+ while(no_command){
+ wait(0.1);
+
+ }
+
+ num_moves = _command;
+
+
+
+ lcd.cls();
+ lcd.printf("Number of Moves: %d", num_moves);
+ wait(2);
+ return 0;
+ }
+
+int main() {
+
+ interrupt.fall(&fallInterrupt);
+ interrupt.mode(PullUp);
+ //Swiping buzz card
+ lcd.printf("Please Swipe your Buzzcard");
+ while(!cardReader());
+ wait(2);
+ lcd.cls();
+
+
-int main() {
- lcd.printf("Hello World!\n");
+ number_of_moves();
+ if (a||b||c||d){
+ lcd.cls();
+ lcd.printf("please turn off all switches");
+ while(1){
+ if (!(a||b||c||d))
+ break;
+ }
+ }
+
+ lcd.cls();
+
+
+ for (int i=0; i<num_moves; i++){
+
+ //select direction
+ lcd.printf("Select Direction2=L 10=R 7=F 5=B");
+ no_command = true;
+ _command=0;
+ while(_command!=2&&_command!=10&&_command!=7&&_command!=5){
+ wait(0.1);
+ }
+ if(_command==2){
+ direction[i]=1;
+ lcd.cls();
+ lcd.printf("Left");
+
+ }
+ else if (_command==10){
+ direction[i]=2;
+ lcd.cls();
+ lcd.printf("Right");
+
+
+ }
+ else if (_command==7){
+ direction[i]=3;
+ lcd.cls();
+ lcd.printf("Forward");
+
+ }
+ else if (_command==5){
+ direction[i]=4;
+ lcd.cls();
+ lcd.printf("Reverse");
+
+ }
+
+
+
+ wait(2);
+ lcd.cls();
+
+
+ //select speed
+ lcd.printf("Move Forward-1 or in Reverse-2");
+ car_speed=0;
+ _command=0;
+ forward=0;;
+ no_command = true;
+ while(_command!=1&&_command!=2){
+ wait(0.1);
+ }
+ if (_command==1){
+ forward=1;
+ lcd.cls();
+ lcd.printf("Car moves forward");
+ }
+ else if (_command==2){
+ forward=-1;
+ lcd.cls();
+ lcd.printf("Car moves in reverse");
+ }
+ wait(2);
+ lcd.cls();
+ _command=0;
+ lcd.printf("Select Car Speed 1-10");
+ while(_command!=1&&_command!=2&&_command!=3&&_command!=4&&_command!=5&&_command!=6&&_command!=7&&_command!=8&&_command!=9&&_command!=10){
+ wait(0.1);
+ }
+ car_speed=forward*_command*0.1;
+
+ speed[i]=car_speed;
+ lcd.cls();
+ lcd.printf("car speed: %d\n", _command);
+ wait(2);
+
+ lcd.cls();
+
+
+ //select duration
+
+ lcd.printf("Select Car duration 1-10 seconds");
+ car_duration=0;
+ _command=0;
+ while(_command!=1&&_command!=2&&_command!=3&&_command!=4&&_command!=5&&_command!=6&&_command!=7&&_command!=8&&_command!=9&&_command!=10){
+ wait(0.1);
+ }
+ car_duration=_command;
+ duration[i]=car_duration;
+ lcd.cls();
+ lcd.printf("car duration: %d seconds\n", _command);
+ wait(2);
+
+
+
+ lcd.cls();
+
+ }
+
+ for (int i=10; i>0; i--){
+ lcd.printf("Car will start in %d seconds",i);
+ wait(1);
+ lcd.cls();
+ }
+ lcd.printf("GO GO GO GO GO GO GO GO GO !!!");
+ wait(1);
+ for (int i=0; i<num_moves; i++){
+
+ switch (direction[i]){
+ case 1:
+ turnLeft();
+ break;
+ case 2:
+ turnRight();
+ break;
+ case 3:
+ break;
+ case 4:
+ turnAround();
+ break;
+ }
+ go(speed[i], duration[i]);
+ }
+ lcd.cls();
+ lcd.printf("Finished");
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mpr121.cpp Thu Oct 11 20:59:05 2012 +0000
@@ -0,0 +1,221 @@
+/*
+Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <mbed.h>
+#include <sstream>
+#include <string>
+#include <list>
+
+#include <mpr121.h>
+
+Mpr121::Mpr121(I2C *i2c, Address i2cAddress)
+{
+ this->i2c = i2c;
+
+ address = i2cAddress;
+
+ // Configure the MPR121 settings to default
+ this->configureSettings();
+}
+
+
+void Mpr121::configureSettings()
+{
+ // Put the MPR into setup mode
+ this->write(ELE_CFG,0x00);
+
+ // Electrode filters for when data is > baseline
+ unsigned char gtBaseline[] = {
+ 0x01, //MHD_R
+ 0x01, //NHD_R
+ 0x00, //NCL_R
+ 0x00 //FDL_R
+ };
+
+ writeMany(MHD_R,gtBaseline,4);
+
+ // Electrode filters for when data is < baseline
+ unsigned char ltBaseline[] = {
+ 0x01, //MHD_F
+ 0x01, //NHD_F
+ 0xFF, //NCL_F
+ 0x02 //FDL_F
+ };
+
+ writeMany(MHD_F,ltBaseline,4);
+
+ // Electrode touch and release thresholds
+ unsigned char electrodeThresholds[] = {
+ E_THR_T, // Touch Threshhold
+ E_THR_R // Release Threshold
+ };
+
+ for(int i=0; i<12; i++){
+ int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2);
+ }
+
+ // Proximity Settings
+ unsigned char proximitySettings[] = {
+ 0xff, //MHD_Prox_R
+ 0xff, //NHD_Prox_R
+ 0x00, //NCL_Prox_R
+ 0x00, //FDL_Prox_R
+ 0x01, //MHD_Prox_F
+ 0x01, //NHD_Prox_F
+ 0xFF, //NCL_Prox_F
+ 0xff, //FDL_Prox_F
+ 0x00, //NHD_Prox_T
+ 0x00, //NCL_Prox_T
+ 0x00 //NFD_Prox_T
+ };
+ writeMany(MHDPROXR,proximitySettings,11);
+
+ unsigned char proxThresh[] = {
+ PROX_THR_T, // Touch Threshold
+ PROX_THR_R // Release Threshold
+ };
+ writeMany(EPROXTTH,proxThresh,2);
+
+ this->write(FIL_CFG,0x04);
+
+ // Set the electrode config to transition to active mode
+ this->write(ELE_CFG,0x0c);
+}
+
+void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){
+
+ if(electrode > 11) return;
+
+ // Get the current mode
+ unsigned char mode = this->read(ELE_CFG);
+
+ // Put the MPR into setup mode
+ this->write(ELE_CFG,0x00);
+
+ // Write the new threshold
+ this->write((ELE0_T+(electrode*2)), touch);
+ this->write((ELE0_T+(electrode*2)+1), release);
+
+ //Restore the operating mode
+ this->write(ELE_CFG, mode);
+}
+
+
+unsigned char Mpr121::read(int key){
+
+ unsigned char data[2];
+
+ //Start the command
+ i2c->start();
+
+ // Address the target (Write mode)
+ int ack1= i2c->write(address);
+
+ // Set the register key to read
+ int ack2 = i2c->write(key);
+
+ // Re-start for read of data
+ i2c->start();
+
+ // Re-send the target address in read mode
+ int ack3 = i2c->write(address+1);
+
+ // Read in the result
+ data[0] = i2c->read(0);
+
+ // Reset the bus
+ i2c->stop();
+
+ return data[0];
+}
+
+
+int Mpr121::write(int key, unsigned char value){
+
+ //Start the command
+ i2c->start();
+
+ // Address the target (Write mode)
+ int ack1= i2c->write(address);
+
+ // Set the register key to write
+ int ack2 = i2c->write(key);
+
+ // Read in the result
+ int ack3 = i2c->write(value);
+
+ // Reset the bus
+ i2c->stop();
+
+ return (ack1+ack2+ack3)-3;
+}
+
+
+int Mpr121::writeMany(int start, unsigned char* dataSet, int length){
+ //Start the command
+ i2c->start();
+
+ // Address the target (Write mode)
+ int ack= i2c->write(address);
+ if(ack!=1){
+ return -1;
+ }
+
+ // Set the register key to write
+ ack = i2c->write(start);
+ if(ack!=1){
+ return -1;
+ }
+
+ // Write the date set
+ int count = 0;
+ while(ack==1 && (count < length)){
+ ack = i2c->write(dataSet[count]);
+ count++;
+ }
+ // Stop the cmd
+ i2c->stop();
+
+ return count;
+}
+
+
+bool Mpr121::getProximityMode(){
+ if(this->read(ELE_CFG) > 0x0c)
+ return true;
+ else
+ return false;
+}
+
+void Mpr121::setProximityMode(bool mode){
+ this->write(ELE_CFG,0x00);
+ if(mode){
+ this->write(ELE_CFG,0x30); //Sense proximity from ALL pads
+ } else {
+ this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active.
+ }
+}
+
+
+int Mpr121::readTouchData(){
+ return this->read(0x00);
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mpr121.h Thu Oct 11 20:59:05 2012 +0000
@@ -0,0 +1,157 @@
+/*
+Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
+
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+ Parts written by Jim Lindblom of Sparkfun
+ Ported to mbed by A.Buckton, Feb 2011
+*/
+
+#ifndef MPR121_H
+#define MPR121_H
+
+//using namespace std;
+
+class Mpr121
+{
+
+public:
+ // i2c Addresses, bit-shifted
+ enum Address { ADD_VSS = 0xb4,// ADD->VSS = 0x5a <-wiring on Sparkfun board
+ ADD_VDD = 0xb6,// ADD->VDD = 0x5b
+ ADD_SCL = 0xb8,// ADD->SDA = 0x5c
+ ADD_SDA = 0xba // ADD->SCL = 0x5d
+ };
+
+ // Real initialiser, takes the i2c address of the device.
+ Mpr121(I2C *i2c, Address i2cAddress);
+
+ bool getProximityMode();
+
+ void setProximityMode(bool mode);
+
+ int readTouchData();
+
+ unsigned char read(int key);
+
+ int write(int address, unsigned char value);
+ int writeMany(int start, unsigned char* dataSet, int length);
+
+ void setElectrodeThreshold(int electrodeId, unsigned char touchThreshold, unsigned char releaseThreshold);
+
+protected:
+ // Configures the MPR with standard settings. This is permitted to be overwritten by sub-classes.
+ void configureSettings();
+
+private:
+ // The I2C bus instance.
+ I2C *i2c;
+
+ // i2c address of this mpr121
+ Address address;
+};
+
+
+// MPR121 Register Defines
+#define MHD_R 0x2B
+#define NHD_R 0x2C
+#define NCL_R 0x2D
+#define FDL_R 0x2E
+#define MHD_F 0x2F
+#define NHD_F 0x30
+#define NCL_F 0x31
+#define FDL_F 0x32
+#define NHDT 0x33
+#define NCLT 0x34
+#define FDLT 0x35
+// Proximity sensing controls
+#define MHDPROXR 0x36
+#define NHDPROXR 0x37
+#define NCLPROXR 0x38
+#define FDLPROXR 0x39
+#define MHDPROXF 0x3A
+#define NHDPROXF 0x3B
+#define NCLPROXF 0x3C
+#define FDLPROXF 0x3D
+#define NHDPROXT 0x3E
+#define NCLPROXT 0x3F
+#define FDLPROXT 0x40
+// Electrode Touch/Release thresholds
+#define ELE0_T 0x41
+#define ELE0_R 0x42
+#define ELE1_T 0x43
+#define ELE1_R 0x44
+#define ELE2_T 0x45
+#define ELE2_R 0x46
+#define ELE3_T 0x47
+#define ELE3_R 0x48
+#define ELE4_T 0x49
+#define ELE4_R 0x4A
+#define ELE5_T 0x4B
+#define ELE5_R 0x4C
+#define ELE6_T 0x4D
+#define ELE6_R 0x4E
+#define ELE7_T 0x4F
+#define ELE7_R 0x50
+#define ELE8_T 0x51
+#define ELE8_R 0x52
+#define ELE9_T 0x53
+#define ELE9_R 0x54
+#define ELE10_T 0x55
+#define ELE10_R 0x56
+#define ELE11_T 0x57
+#define ELE11_R 0x58
+// Proximity Touch/Release thresholds
+#define EPROXTTH 0x59
+#define EPROXRTH 0x5A
+// Debounce configuration
+#define DEB_CFG 0x5B
+// AFE- Analogue Front End configuration
+#define AFE_CFG 0x5C
+// Filter configuration
+#define FIL_CFG 0x5D
+// Electrode configuration - transistions to "active mode"
+#define ELE_CFG 0x5E
+
+#define GPIO_CTRL0 0x73
+#define GPIO_CTRL1 0x74
+#define GPIO_DATA 0x75
+#define GPIO_DIR 0x76
+#define GPIO_EN 0x77
+#define GPIO_SET 0x78
+#define GPIO_CLEAR 0x79
+#define GPIO_TOGGLE 0x7A
+// Auto configration registers
+#define AUTO_CFG_0 0x7B
+#define AUTO_CFG_U 0x7D
+#define AUTO_CFG_L 0x7E
+#define AUTO_CFG_T 0x7F
+
+// Threshold defaults
+// Electrode touch threshold
+#define E_THR_T 0x0F
+// Electrode release threshold
+#define E_THR_R 0x0A
+// Prox touch threshold
+#define PROX_THR_T 0x02
+// Prox release threshold
+#define PROX_THR_R 0x02
+
+#endif
