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.
main.cpp
00001 #include "mbed.h" 00002 #include "Trackball.h" 00003 00004 //set up the trackball 00005 00006 Trackball trackball(p12, p13, p14, p15, p16, p17, p18, p19, p20); 00007 00008 //for prints to serial port 00009 Serial pc(USBTX, USBRX); 00010 00011 00012 int main() { 00013 00014 //turn on each LED individually for 2 seconds each 00015 trackball.write(1, color_WHITE); 00016 wait(2); 00017 trackball.write(0, color_WHITE); 00018 trackball.write(1, color_BLUE); 00019 wait(2); 00020 trackball.write(0, color_BLUE); 00021 trackball.write(1, color_GREEN); 00022 wait(2); 00023 trackball.write(0, color_GREEN); 00024 trackball.write(1, color_RED); 00025 wait(2); 00026 trackball.write(0, color_RED); 00027 00028 //positioning data 00029 int x = 0; 00030 int y = 0; 00031 //keeping track of changes in movement 00032 unsigned int old_up = 1; 00033 unsigned int new_up = 1; 00034 unsigned int old_down = 1; 00035 unsigned int new_down = 1; 00036 unsigned int old_right = 1; 00037 unsigned int new_right = 1; 00038 unsigned int old_left = 1; 00039 unsigned int new_left = 1; 00040 00041 00042 while(1) { 00043 00044 00045 //comparing old val to new val makes sure to only register changes to the value as movement 00046 new_up = trackball.read(dir_UP); 00047 new_down = trackball.read(dir_DOWN); 00048 new_left = trackball.read(dir_LEFT); 00049 new_right = trackball.read(dir_RIGHT); 00050 00051 if((new_up == 0) && (old_up == 1)){ 00052 trackball.write(1, color_BLUE); 00053 trackball.write(0, color_WHITE); 00054 trackball.write(0, color_GREEN); 00055 trackball.write(0, color_RED); 00056 y++; 00057 } 00058 if((new_down == 0) && (old_down == 1)){ 00059 trackball.write(1, color_GREEN); 00060 trackball.write(0, color_WHITE); 00061 trackball.write(0, color_BLUE); 00062 trackball.write(0, color_RED); 00063 y--; 00064 } 00065 if((new_left == 0) && (old_left == 1)){ 00066 00067 trackball.write(1, color_WHITE); 00068 trackball.write(0, color_GREEN); 00069 trackball.write(0, color_BLUE); 00070 trackball.write(0, color_RED); 00071 x--; 00072 } 00073 if((new_right == 0) && (old_right == 1)){ 00074 trackball.write(1, color_RED); 00075 trackball.write(0, color_WHITE); 00076 trackball.write(0, color_GREEN); 00077 trackball.write(0, color_BLUE); 00078 x++; 00079 } 00080 if(trackball.read(dir_BUTTON) == 0){ 00081 trackball.write(1, color_RED); 00082 trackball.write(1, color_BLUE); 00083 trackball.write(1, color_GREEN); 00084 trackball.write(1, color_WHITE); 00085 } 00086 00087 pc.printf("X: %d, Y: %d\n", x, y); 00088 00089 //keep track of these now old vals to compare to the future to see changes 00090 old_up = new_up; 00091 old_down = new_down; 00092 old_left = new_left; 00093 old_right = new_right; 00094 00095 00096 wait(.2); 00097 00098 00099 } 00100 }
Generated on Tue Jul 26 2022 10:13:19 by
1.7.2