updated 7seg controls for new 7 seg boards

Dependencies:   PixelArray WS2812 mbed

Fork of frdm_pong_table_controller by Demo Team

Committer:
samuelmoss
Date:
Tue Jul 04 15:51:03 2017 +0000
Revision:
2:ff409472bc08
Parent:
1:24bc4d8ed2ae
Child:
3:51604b5710f1
Updated LED code for the new 7-seg displays

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benswindell 0:f2b739e846ae 1 #include "mbed.h"
benswindell 0:f2b739e846ae 2 #include "WS2812.h"
benswindell 0:f2b739e846ae 3 #include "PixelArray.h"
benswindell 0:f2b739e846ae 4
samuelmoss 2:ff409472bc08 5 #define WS2812_BUF 122
benswindell 0:f2b739e846ae 6 #define NUM_COLORS 6
benswindell 0:f2b739e846ae 7 #define NUM_LEDS_PER_COLOR 1
benswindell 0:f2b739e846ae 8
benswindell 0:f2b739e846ae 9 //-------- Colours -----------
benswindell 0:f2b739e846ae 10 #define RED 0x2f0000
benswindell 0:f2b739e846ae 11 #define YELLOW 0x2f2f00
benswindell 0:f2b739e846ae 12 #define GREEN 0x002f00
benswindell 0:f2b739e846ae 13 #define LIGHTBLUE 0x002f2f
benswindell 0:f2b739e846ae 14 #define DARKBLUE 0x00002f
benswindell 0:f2b739e846ae 15 #define BLUE 0x0000ff // Player scored a goal
benswindell 0:f2b739e846ae 16 #define PINK 0x2f002f
benswindell 0:f2b739e846ae 17 #define OFF 0x000000
benswindell 1:24bc4d8ed2ae 18 #define WHITE 0xffffaa
benswindell 0:f2b739e846ae 19 #define ARMBLUE 0x128BAB
benswindell 0:f2b739e846ae 20 #define PURPLE 0xff0055 // Player has conceded a goal
benswindell 0:f2b739e846ae 21
benswindell 0:f2b739e846ae 22 // GPIO
benswindell 0:f2b739e846ae 23 AnalogIn robotBreakBeam(A0);
benswindell 0:f2b739e846ae 24 AnalogIn playerBreakBeam(A1);
benswindell 0:f2b739e846ae 25 DigitalOut led_green(LED_GREEN, 1);
samuelmoss 2:ff409472bc08 26 DigitalIn PB1(PTC6);
samuelmoss 2:ff409472bc08 27 DigitalIn PB2(PTA4);
benswindell 0:f2b739e846ae 28
benswindell 0:f2b739e846ae 29 // SERIAL
benswindell 0:f2b739e846ae 30 Serial pc(USBTX, USBRX); // tx, rx
benswindell 0:f2b739e846ae 31
benswindell 0:f2b739e846ae 32 // LED STRIPS
benswindell 0:f2b739e846ae 33 // See the program page for information on the timing numbers
benswindell 0:f2b739e846ae 34 // The given numbers are for the K64F
samuelmoss 2:ff409472bc08 35 WS2812 robotScoreLED(D3, WS2812_BUF, 0, 5, 5, 0);
samuelmoss 2:ff409472bc08 36 WS2812 playerScoreLED(D5,WS2812_BUF, 0, 5, 5, 0);
benswindell 0:f2b739e846ae 37
benswindell 0:f2b739e846ae 38 // LED Variables
benswindell 0:f2b739e846ae 39 bool seg1A, seg1B, seg1C, seg1D, seg1E, seg1F, seg1G;
samuelmoss 2:ff409472bc08 40 int seg1Array[122];
benswindell 0:f2b739e846ae 41 int mainArray[11][122];
benswindell 1:24bc4d8ed2ae 42 PixelArray robotScorePx(WS2812_BUF);
benswindell 1:24bc4d8ed2ae 43 PixelArray playerScorePx(WS2812_BUF);
benswindell 0:f2b739e846ae 44
benswindell 0:f2b739e846ae 45 // Score counters
benswindell 0:f2b739e846ae 46 int robotScore;
benswindell 0:f2b739e846ae 47 int playerScore;
benswindell 0:f2b739e846ae 48 int scoreLimit = 3;
benswindell 0:f2b739e846ae 49 bool finishedGame = false;
benswindell 1:24bc4d8ed2ae 50 int endFlashes = 3;
benswindell 0:f2b739e846ae 51 int numFlashes;
benswindell 0:f2b739e846ae 52
benswindell 0:f2b739e846ae 53 // Robot Bream Beam value
benswindell 0:f2b739e846ae 54 double prevRbbValue; // Previous Robot break beam value
benswindell 0:f2b739e846ae 55 double prevPbbValue; // Previous player break beam value
benswindell 0:f2b739e846ae 56
benswindell 0:f2b739e846ae 57 // FUNCTION DECLERATIONS
benswindell 0:f2b739e846ae 58 void Write7Seg(int num);
benswindell 0:f2b739e846ae 59 void SetLEDArray(int x);
samuelmoss 2:ff409472bc08 60
benswindell 0:f2b739e846ae 61 void WriteScores();
benswindell 0:f2b739e846ae 62 void HandleGoal(bool hasRobotScored);
benswindell 1:24bc4d8ed2ae 63 void WritePxScores(int line_num, bool isRobot);
benswindell 1:24bc4d8ed2ae 64 void WritePxGoal(unsigned int colour,bool isRobot);
benswindell 1:24bc4d8ed2ae 65 void HandleWin();
samuelmoss 2:ff409472bc08 66
benswindell 0:f2b739e846ae 67
benswindell 0:f2b739e846ae 68
benswindell 0:f2b739e846ae 69 void setup()
benswindell 0:f2b739e846ae 70 {
benswindell 0:f2b739e846ae 71
benswindell 0:f2b739e846ae 72 // Turn on green LED
benswindell 0:f2b739e846ae 73 led_green = 0;
benswindell 0:f2b739e846ae 74
benswindell 1:24bc4d8ed2ae 75 // Set brightness of the 4 LED strips
samuelmoss 2:ff409472bc08 76 robotScoreLED.setII(0xB0);
benswindell 1:24bc4d8ed2ae 77 robotScoreLED.useII(WS2812::GLOBAL);
samuelmoss 2:ff409472bc08 78 playerScoreLED.setII(0xB0);
benswindell 1:24bc4d8ed2ae 79 playerScoreLED.useII(WS2812::GLOBAL);
samuelmoss 2:ff409472bc08 80
benswindell 1:24bc4d8ed2ae 81
benswindell 0:f2b739e846ae 82 // Fills 2D array with data
benswindell 0:f2b739e846ae 83 for(int i=0; i<10; i++) {
benswindell 0:f2b739e846ae 84 Write7Seg(i);
benswindell 0:f2b739e846ae 85 }
benswindell 0:f2b739e846ae 86
benswindell 0:f2b739e846ae 87 // Set scores to 0
benswindell 0:f2b739e846ae 88 robotScore = 0;
benswindell 0:f2b739e846ae 89 playerScore = 0;
benswindell 0:f2b739e846ae 90 numFlashes = 0;
benswindell 0:f2b739e846ae 91
benswindell 0:f2b739e846ae 92 // Set LEDS to start values
benswindell 0:f2b739e846ae 93 WriteScores();
samuelmoss 2:ff409472bc08 94
benswindell 1:24bc4d8ed2ae 95
samuelmoss 2:ff409472bc08 96 wait(1);
benswindell 1:24bc4d8ed2ae 97
samuelmoss 2:ff409472bc08 98
benswindell 0:f2b739e846ae 99 // Turn off green LED
benswindell 0:f2b739e846ae 100 led_green = 1;
benswindell 0:f2b739e846ae 101 }
benswindell 0:f2b739e846ae 102
benswindell 0:f2b739e846ae 103 // Set segment variables
benswindell 0:f2b739e846ae 104 void Write7Seg(int num)
benswindell 0:f2b739e846ae 105 {
benswindell 0:f2b739e846ae 106 switch (num) {
benswindell 0:f2b739e846ae 107 case 0 :
samuelmoss 2:ff409472bc08 108 // 0
benswindell 0:f2b739e846ae 109 seg1A = 1;
benswindell 0:f2b739e846ae 110 seg1B = 1;
benswindell 0:f2b739e846ae 111 seg1C = 1;
benswindell 0:f2b739e846ae 112 seg1D = 1;
benswindell 0:f2b739e846ae 113 seg1E = 1;
benswindell 0:f2b739e846ae 114 seg1F = 1;
benswindell 0:f2b739e846ae 115 seg1G = 0;
benswindell 0:f2b739e846ae 116
benswindell 0:f2b739e846ae 117 SetLEDArray(0);
benswindell 0:f2b739e846ae 118
benswindell 0:f2b739e846ae 119 break;
benswindell 0:f2b739e846ae 120
benswindell 0:f2b739e846ae 121 case 1 :
benswindell 0:f2b739e846ae 122 // 1
benswindell 0:f2b739e846ae 123 seg1A = 0;
benswindell 0:f2b739e846ae 124 seg1B = 1;
benswindell 0:f2b739e846ae 125 seg1C = 1;
benswindell 0:f2b739e846ae 126 seg1D = 0;
benswindell 0:f2b739e846ae 127 seg1E = 0;
benswindell 0:f2b739e846ae 128 seg1F = 0;
benswindell 0:f2b739e846ae 129 seg1G = 0;
benswindell 0:f2b739e846ae 130
benswindell 0:f2b739e846ae 131 SetLEDArray(1);
benswindell 0:f2b739e846ae 132 break;
benswindell 0:f2b739e846ae 133
benswindell 0:f2b739e846ae 134 case 2 :
benswindell 0:f2b739e846ae 135 // 2
benswindell 0:f2b739e846ae 136 seg1A = 1;
benswindell 0:f2b739e846ae 137 seg1B = 1;
benswindell 0:f2b739e846ae 138 seg1C = 0;
benswindell 0:f2b739e846ae 139 seg1D = 1;
benswindell 0:f2b739e846ae 140 seg1E = 1;
benswindell 0:f2b739e846ae 141 seg1F = 0;
benswindell 0:f2b739e846ae 142 seg1G = 1;
benswindell 0:f2b739e846ae 143
benswindell 0:f2b739e846ae 144 SetLEDArray(2);
benswindell 0:f2b739e846ae 145
benswindell 0:f2b739e846ae 146 break;
benswindell 0:f2b739e846ae 147
benswindell 0:f2b739e846ae 148 case 3 :
benswindell 0:f2b739e846ae 149 // 3
benswindell 0:f2b739e846ae 150 seg1A = 1;
benswindell 0:f2b739e846ae 151 seg1B = 1;
benswindell 0:f2b739e846ae 152 seg1C = 1;
benswindell 0:f2b739e846ae 153 seg1D = 1;
benswindell 0:f2b739e846ae 154 seg1E = 0;
benswindell 0:f2b739e846ae 155 seg1F = 0;
benswindell 0:f2b739e846ae 156 seg1G = 1;
benswindell 0:f2b739e846ae 157
benswindell 0:f2b739e846ae 158 SetLEDArray(3);
benswindell 0:f2b739e846ae 159 break;
benswindell 0:f2b739e846ae 160
benswindell 0:f2b739e846ae 161 case 4:
benswindell 0:f2b739e846ae 162 // 4
benswindell 0:f2b739e846ae 163 seg1A = 0;
benswindell 0:f2b739e846ae 164 seg1B = 1;
benswindell 0:f2b739e846ae 165 seg1C = 1;
benswindell 0:f2b739e846ae 166 seg1D = 0;
benswindell 0:f2b739e846ae 167 seg1E = 0;
benswindell 0:f2b739e846ae 168 seg1F = 1;
benswindell 0:f2b739e846ae 169 seg1G = 1;
benswindell 0:f2b739e846ae 170
benswindell 0:f2b739e846ae 171 SetLEDArray(4);
benswindell 0:f2b739e846ae 172
benswindell 0:f2b739e846ae 173 break;
benswindell 0:f2b739e846ae 174
benswindell 0:f2b739e846ae 175 case 5:
benswindell 0:f2b739e846ae 176 // 5
benswindell 0:f2b739e846ae 177 seg1A = 1;
benswindell 0:f2b739e846ae 178 seg1B = 0;
benswindell 0:f2b739e846ae 179 seg1C = 1;
benswindell 0:f2b739e846ae 180 seg1D = 1;
benswindell 0:f2b739e846ae 181 seg1E = 0;
benswindell 0:f2b739e846ae 182 seg1F = 1;
benswindell 0:f2b739e846ae 183 seg1G = 1;
benswindell 0:f2b739e846ae 184
benswindell 0:f2b739e846ae 185 SetLEDArray(5);
benswindell 0:f2b739e846ae 186
benswindell 0:f2b739e846ae 187 break;
benswindell 0:f2b739e846ae 188
benswindell 0:f2b739e846ae 189 case 6:
benswindell 0:f2b739e846ae 190 // 6
benswindell 0:f2b739e846ae 191 seg1A = 1;
benswindell 0:f2b739e846ae 192 seg1B = 0;
benswindell 0:f2b739e846ae 193 seg1C = 1;
benswindell 0:f2b739e846ae 194 seg1D = 1;
benswindell 0:f2b739e846ae 195 seg1E = 1;
benswindell 0:f2b739e846ae 196 seg1F = 1;
benswindell 0:f2b739e846ae 197 seg1G = 1;
benswindell 0:f2b739e846ae 198
benswindell 0:f2b739e846ae 199 SetLEDArray(6);
benswindell 0:f2b739e846ae 200
benswindell 0:f2b739e846ae 201 break;
benswindell 0:f2b739e846ae 202
benswindell 0:f2b739e846ae 203 case 7:
benswindell 0:f2b739e846ae 204 // 7
benswindell 0:f2b739e846ae 205 seg1A = 1;
benswindell 0:f2b739e846ae 206 seg1B = 1;
benswindell 0:f2b739e846ae 207 seg1C = 1;
benswindell 0:f2b739e846ae 208 seg1D = 0;
benswindell 0:f2b739e846ae 209 seg1E = 0;
benswindell 0:f2b739e846ae 210 seg1F = 0;
benswindell 0:f2b739e846ae 211 seg1G = 0;
benswindell 0:f2b739e846ae 212
benswindell 0:f2b739e846ae 213 SetLEDArray(7);
benswindell 0:f2b739e846ae 214
benswindell 0:f2b739e846ae 215 break;
benswindell 0:f2b739e846ae 216
benswindell 0:f2b739e846ae 217 case 8:
benswindell 0:f2b739e846ae 218 // 8
benswindell 0:f2b739e846ae 219 seg1A = 1;
benswindell 0:f2b739e846ae 220 seg1B = 1;
benswindell 0:f2b739e846ae 221 seg1C = 1;
benswindell 0:f2b739e846ae 222 seg1D = 1;
benswindell 0:f2b739e846ae 223 seg1E = 1;
benswindell 0:f2b739e846ae 224 seg1F = 1;
benswindell 0:f2b739e846ae 225 seg1G = 1;
benswindell 0:f2b739e846ae 226
benswindell 0:f2b739e846ae 227 SetLEDArray(8);
benswindell 0:f2b739e846ae 228 break;
benswindell 0:f2b739e846ae 229
benswindell 0:f2b739e846ae 230 case 9:
benswindell 0:f2b739e846ae 231 // 9
benswindell 0:f2b739e846ae 232 seg1A = 1;
benswindell 0:f2b739e846ae 233 seg1B = 1;
benswindell 0:f2b739e846ae 234 seg1C = 1;
benswindell 0:f2b739e846ae 235 seg1D = 0;
benswindell 0:f2b739e846ae 236 seg1E = 0;
benswindell 0:f2b739e846ae 237 seg1F = 1;
benswindell 0:f2b739e846ae 238 seg1G = 1;
benswindell 0:f2b739e846ae 239
benswindell 0:f2b739e846ae 240 SetLEDArray(9);
benswindell 0:f2b739e846ae 241 break;
benswindell 0:f2b739e846ae 242
benswindell 0:f2b739e846ae 243 case 10:
benswindell 0:f2b739e846ae 244 // OFF
benswindell 0:f2b739e846ae 245 seg1A = 0;
benswindell 0:f2b739e846ae 246 seg1B = 0;
benswindell 0:f2b739e846ae 247 seg1C = 0;
benswindell 0:f2b739e846ae 248 seg1D = 0;
benswindell 0:f2b739e846ae 249 seg1E = 0;
benswindell 0:f2b739e846ae 250 seg1F = 0;
benswindell 0:f2b739e846ae 251 seg1G = 0;
benswindell 0:f2b739e846ae 252
benswindell 0:f2b739e846ae 253 SetLEDArray(10);
benswindell 0:f2b739e846ae 254 break;
benswindell 0:f2b739e846ae 255
benswindell 0:f2b739e846ae 256 default :
benswindell 0:f2b739e846ae 257
benswindell 0:f2b739e846ae 258 break;
benswindell 0:f2b739e846ae 259
benswindell 0:f2b739e846ae 260 }
benswindell 0:f2b739e846ae 261 }
benswindell 0:f2b739e846ae 262
benswindell 0:f2b739e846ae 263 // Write segment config to main array
benswindell 0:f2b739e846ae 264 void SetLEDArray(int x)
benswindell 0:f2b739e846ae 265 {
benswindell 0:f2b739e846ae 266 for (int i = 0; i < WS2812_BUF; i++) {
samuelmoss 2:ff409472bc08 267 if (i < 18) {
benswindell 0:f2b739e846ae 268 mainArray[x][i] = seg1A;
benswindell 0:f2b739e846ae 269 }
benswindell 0:f2b739e846ae 270
samuelmoss 2:ff409472bc08 271 if ( i >= 18 && i < 35) {
benswindell 0:f2b739e846ae 272 mainArray[x][i] = seg1B;
benswindell 0:f2b739e846ae 273 }
benswindell 0:f2b739e846ae 274
samuelmoss 2:ff409472bc08 275 if (i >= 35 && i < 52) {
benswindell 0:f2b739e846ae 276 mainArray[x][i] = seg1C;
benswindell 0:f2b739e846ae 277 }
benswindell 0:f2b739e846ae 278
samuelmoss 2:ff409472bc08 279 if (i >= 52 && i < 70) {
benswindell 0:f2b739e846ae 280 mainArray[x][i]= seg1D;
benswindell 0:f2b739e846ae 281 }
benswindell 0:f2b739e846ae 282
samuelmoss 2:ff409472bc08 283 if ( i >= 70 && i < 87) {
benswindell 0:f2b739e846ae 284 mainArray[x][i] = seg1E;
benswindell 0:f2b739e846ae 285 }
benswindell 0:f2b739e846ae 286
samuelmoss 2:ff409472bc08 287 if (i >= 87 && i < 104) {
benswindell 0:f2b739e846ae 288 mainArray[x][i] = seg1F;
benswindell 0:f2b739e846ae 289 }
benswindell 0:f2b739e846ae 290
samuelmoss 2:ff409472bc08 291 if ( i >= 104 && i < 122) {
benswindell 0:f2b739e846ae 292 mainArray[x][i] = seg1G;
benswindell 0:f2b739e846ae 293 }
benswindell 0:f2b739e846ae 294 }// FOR LOOP
benswindell 0:f2b739e846ae 295 }
benswindell 0:f2b739e846ae 296
benswindell 1:24bc4d8ed2ae 297 // Update both Score LEDs with the current score
benswindell 0:f2b739e846ae 298 void WriteScores()
benswindell 0:f2b739e846ae 299 {
benswindell 1:24bc4d8ed2ae 300 WritePxScores(playerScore,false);
benswindell 1:24bc4d8ed2ae 301 playerScoreLED.write(playerScorePx.getBuf());
benswindell 0:f2b739e846ae 302 wait(0.01);
benswindell 1:24bc4d8ed2ae 303 WritePxScores(robotScore,true);
benswindell 1:24bc4d8ed2ae 304 robotScoreLED.write(robotScorePx.getBuf());
benswindell 0:f2b739e846ae 305 }
benswindell 0:f2b739e846ae 306
benswindell 0:f2b739e846ae 307 // Write pixel array
benswindell 1:24bc4d8ed2ae 308 void WritePxScores(int line_num,bool isRobot)
benswindell 0:f2b739e846ae 309 {
benswindell 1:24bc4d8ed2ae 310 if(isRobot == true) {
benswindell 1:24bc4d8ed2ae 311
benswindell 1:24bc4d8ed2ae 312 for (int i = 0; i < WS2812_BUF; i++) {
benswindell 1:24bc4d8ed2ae 313 if (mainArray[line_num][i] == 0) {
benswindell 1:24bc4d8ed2ae 314 robotScorePx.Set(i,OFF);
benswindell 1:24bc4d8ed2ae 315 }
benswindell 1:24bc4d8ed2ae 316
benswindell 1:24bc4d8ed2ae 317 if (mainArray[line_num][i] == 1) {
samuelmoss 2:ff409472bc08 318 robotScorePx.Set(i,BLUE);
benswindell 1:24bc4d8ed2ae 319 }
benswindell 0:f2b739e846ae 320 }
benswindell 1:24bc4d8ed2ae 321 } else {
benswindell 1:24bc4d8ed2ae 322 for (int i = 0; i < WS2812_BUF; i++) {
benswindell 1:24bc4d8ed2ae 323 if (mainArray[line_num][i] == 0) {
benswindell 1:24bc4d8ed2ae 324 playerScorePx.Set(i,OFF);
benswindell 1:24bc4d8ed2ae 325 }
benswindell 0:f2b739e846ae 326
benswindell 1:24bc4d8ed2ae 327 if (mainArray[line_num][i] == 1) {
samuelmoss 2:ff409472bc08 328 playerScorePx.Set(i,BLUE);
benswindell 1:24bc4d8ed2ae 329 }
benswindell 1:24bc4d8ed2ae 330 }
benswindell 1:24bc4d8ed2ae 331 }
benswindell 1:24bc4d8ed2ae 332
benswindell 1:24bc4d8ed2ae 333 }
benswindell 1:24bc4d8ed2ae 334
benswindell 1:24bc4d8ed2ae 335 // Write the goal LEDs and score LEDs when a goal is recorded. Also check to see if the goal being scored
benswindell 1:24bc4d8ed2ae 336 // is the final point of the game and the game is over.
benswindell 1:24bc4d8ed2ae 337 // INPUTS: hasRobotScored
benswindell 1:24bc4d8ed2ae 338 //
benswindell 1:24bc4d8ed2ae 339 // hasRobotScored - true if the goal being recorded is from the playerside goal, meaning the robot has scored a point
benswindell 1:24bc4d8ed2ae 340 //
benswindell 0:f2b739e846ae 341 void HandleGoal(bool hasRobotScored)
benswindell 0:f2b739e846ae 342 {
benswindell 0:f2b739e846ae 343 if(hasRobotScored == true) {
benswindell 0:f2b739e846ae 344 robotScore++;
benswindell 0:f2b739e846ae 345 WriteScores();
benswindell 0:f2b739e846ae 346 } else {
benswindell 0:f2b739e846ae 347 playerScore++;
benswindell 0:f2b739e846ae 348 WriteScores();
benswindell 0:f2b739e846ae 349 }
benswindell 1:24bc4d8ed2ae 350
samuelmoss 2:ff409472bc08 351 wait(1);
benswindell 1:24bc4d8ed2ae 352
benswindell 1:24bc4d8ed2ae 353 // If either player reaches the score limit, then call HandleWin(). If not, then turn goal LEDs off
benswindell 1:24bc4d8ed2ae 354 if(robotScore == scoreLimit || playerScore == scoreLimit) {
benswindell 1:24bc4d8ed2ae 355 HandleWin();
samuelmoss 2:ff409472bc08 356 }
benswindell 1:24bc4d8ed2ae 357 }
benswindell 1:24bc4d8ed2ae 358
benswindell 1:24bc4d8ed2ae 359 // Handle behaviour when either the player or robot has won a game.
benswindell 1:24bc4d8ed2ae 360 void HandleWin()
benswindell 1:24bc4d8ed2ae 361 {
benswindell 1:24bc4d8ed2ae 362 // Initalise variable as true
benswindell 1:24bc4d8ed2ae 363 bool isRobotWinner = true;
benswindell 1:24bc4d8ed2ae 364
benswindell 1:24bc4d8ed2ae 365 // Decide if the robot is the winner. If not, then change isRobotWinner to false
benswindell 1:24bc4d8ed2ae 366 if(playerScore == scoreLimit) {
benswindell 1:24bc4d8ed2ae 367 isRobotWinner = false;
benswindell 1:24bc4d8ed2ae 368 pc.printf("Player won!");
benswindell 1:24bc4d8ed2ae 369
samuelmoss 2:ff409472bc08 370
benswindell 1:24bc4d8ed2ae 371
samuelmoss 2:ff409472bc08 372
benswindell 1:24bc4d8ed2ae 373
benswindell 1:24bc4d8ed2ae 374 } else {
benswindell 1:24bc4d8ed2ae 375 pc.printf("Robot won!");
benswindell 1:24bc4d8ed2ae 376
samuelmoss 2:ff409472bc08 377
benswindell 1:24bc4d8ed2ae 378
samuelmoss 2:ff409472bc08 379
benswindell 1:24bc4d8ed2ae 380 }
benswindell 1:24bc4d8ed2ae 381
benswindell 1:24bc4d8ed2ae 382 // Reset scores then write to LEDs
benswindell 1:24bc4d8ed2ae 383 robotScore = 0;
benswindell 1:24bc4d8ed2ae 384 playerScore = 0;
benswindell 1:24bc4d8ed2ae 385 WriteScores();
benswindell 0:f2b739e846ae 386 }
benswindell 0:f2b739e846ae 387
benswindell 0:f2b739e846ae 388 int main()
benswindell 0:f2b739e846ae 389 {
benswindell 1:24bc4d8ed2ae 390 setup();
benswindell 1:24bc4d8ed2ae 391
benswindell 0:f2b739e846ae 392 while(1) {
benswindell 0:f2b739e846ae 393 double rbbValue = robotBreakBeam; // Read Robot Break beam
benswindell 0:f2b739e846ae 394 double pbbValue = playerBreakBeam; // Read Player Break beam
benswindell 0:f2b739e846ae 395
benswindell 0:f2b739e846ae 396 // IF PLAYER HAS SCORED A GOAL
samuelmoss 2:ff409472bc08 397 if (((prevRbbValue - rbbValue)>0.03)|| (PB1==0)) {
benswindell 0:f2b739e846ae 398 pc.printf("Player has scored a goal \n\r");
benswindell 0:f2b739e846ae 399 HandleGoal(false);
benswindell 0:f2b739e846ae 400 }
benswindell 0:f2b739e846ae 401
benswindell 0:f2b739e846ae 402 // IF ROBOT HAS SCORED A GOAL
samuelmoss 2:ff409472bc08 403 if (((prevPbbValue - pbbValue) > 0.03)|| (PB2==0)) {
benswindell 0:f2b739e846ae 404 pc.printf("Robot has scored a goal \n\r");
benswindell 0:f2b739e846ae 405 HandleGoal(true);
benswindell 0:f2b739e846ae 406 }
benswindell 1:24bc4d8ed2ae 407
benswindell 0:f2b739e846ae 408 prevRbbValue = rbbValue;
benswindell 0:f2b739e846ae 409 prevPbbValue = pbbValue;
benswindell 1:24bc4d8ed2ae 410
benswindell 1:24bc4d8ed2ae 411 pc.printf("PlayerGoal: %f, RobotGoal: %f \r\n",pbbValue,rbbValue);
benswindell 1:24bc4d8ed2ae 412 pc.printf("Player: %i v %i : Robot \r\n",playerScore,robotScore);
benswindell 1:24bc4d8ed2ae 413 wait(0.02);
benswindell 0:f2b739e846ae 414
benswindell 0:f2b739e846ae 415 }
benswindell 0:f2b739e846ae 416 }