Arduino Update – MuFFy

Arduino Update

So, a few weeks in to Arduino its been great.

After doing the “basic” projects: blinking LED, blinking LED without delay… I then tried my hand at writing some of my own programs. My first project was RGB LED that increments through each colour mixing as it increases.

The next was RGB LED with random colour generation. This was my first play with PWM. Very basic program:

 C++ (Qt) | 
 
 copy code |
?

01
const int green=10;
02
const int red=11;
03
const int blue=9;
04
 
05
void setup() {
06
pinMode(green,OUTPUT);
07
pinMode(red,OUTPUT);
08
pinMode(blue,OUTPUT);
09
}
10
 
11
void loop(){
12
analogWrite(green,254−random(254));
13
analogWrite(red,254−random(254));
14
analogWrite(blue,254−random(254));
15
delay(500);
16
}

From here I moved on to a 4-Digit counter using a 4×7 segment display, common cathode. This led to my first crack at multiplexing. I think I coded it quite well for a first attempt. I know I can neaten up the dispProcess() to not have so many case statements.

http://www.youtube.com/watch?v=xoLKC9M8OQw

 C++ | 
 
 copy code |
?

001
//Segment Mapping
002
const int a=2;
003
const int b=3;
004
const int c=4;
005
const int d=5;
006
const int e=6;
007
const int f=7;
008
const int g=8;
009
 
010
//Digit Mapping
011
const int dig1=9;
012
const int dig2=10;
013
const int dig3=11;
014
const int dig4=12;
015
 
016
//Update timer (1000=seconds, 100=MilliSeconds ... etc)
017
const int timer=10;
018
 
019
//Array to initialise pins easier
020
int seg[]={a,b,c,d,e,f,g};
021
int dig[]={dig1,dig2,dig3,dig4};
022
 
023
void setup(){
024
  //Loop to setup segment pins
025
  for(int temp=0;temp<7;temp++){
026
    pinMode(seg[temp],OUTPUT);
027
    digitalWrite(seg[temp],0);
028
  }
029
 
030
  //Loop to set up Digit pins
031
  for(int temp=0;temp<4;temp++){
032
    pinMode(dig[temp],OUTPUT);
033
    digitalWrite(dig[temp],1);
034
  }
035
}
036
 
037
void loop(){
038
  //Variable initialisation for count display
039
  int thousand,hundred,tens,ones=0;
040
 
041
  for(thousand=0;thousand<10;thousand++){ //Loop for Thousand digit
042
    for(hundred=0;hundred<10;hundred++){  //Loop for Hundred digit
043
      for(tens=0;tens<10;tens++){         //Loop for Tens digit
044
        for(ones=0;ones<10;ones++){       //Loop for Ones
045
 
046
          long startTime=millis();                    //Start time for current value
047
          while(millis()<(startTime+timer)){          //Loop displaying current value until has been displayed for amount of time set by 'timer' variable
048
            dispProcess(thousand,hundred,tens,ones);  //Call the digit processing and display function function
049
          }//Close While
050
        }//Close Ones
051
      }//Close Tens
052
    }//Close Hundred
053
  }//Close Thousand
054
 
055
}//Close Loop
056
 
057
int dispProcess(int th,int hun,int ten,int one){
058
  //This function process each value 'th', 'hun', 'ten' and 'one'
059
  //and uses the 'Switch' control type to determine vale of each digit.
060
  //The digitalWrite(dig1,0) takes the cathode pin LOW (GND), then
061
  //the required segments are set HIGH, then set LOW then the cathode is 
062
  //set high again.
063
 
064
  if(1){
065
    digitalWrite(dig1,0);
066
    switch(th){
067
      case 0:
068
        digitalWrite(a,1);
069
        digitalWrite(b,1);
070
        digitalWrite(c,1);
071
        digitalWrite(d,1);
072
        digitalWrite(e,1);
073
        digitalWrite(f,1);
074
        digitalWrite(a,0);
075
        digitalWrite(b,0);
076
        digitalWrite(c,0);
077
        digitalWrite(d,0);
078
        digitalWrite(e,0);
079
        digitalWrite(f,0);
080
        break;
081
      case 1:
082
        digitalWrite(b,1);
083
        digitalWrite(c,1);
084
        digitalWrite(b,0);
085
        digitalWrite(c,0);
086
        break;
087
      case 2:
088
        digitalWrite(a,1);
089
        digitalWrite(b,1);
090
        digitalWrite(g,1);
091
        digitalWrite(d,1);
092
        digitalWrite(e,1);
093
        digitalWrite(a,0);
094
        digitalWrite(b,0);
095
        digitalWrite(g,0);
096
        digitalWrite(d,0);
097
        digitalWrite(e,0);
098
        break;
099
      case 3:
100
        digitalWrite(a,1);
101
        digitalWrite(b,1);
102
        digitalWrite(g,1);
103
        digitalWrite(c,1);
104
        digitalWrite(c,1);
105
        digitalWrite(d,1);
106
        digitalWrite(a,0);
107
        digitalWrite(b,0);
108
        digitalWrite(g,0);
109
        digitalWrite(c,0);
110
        digitalWrite(c,0);
111
        digitalWrite(d,0);
112
        break;
113
      case 4:
114
        digitalWrite(b,1);
115
        digitalWrite(c,1);
116
        digitalWrite(g,1);
117
        digitalWrite(f,1);
118
        digitalWrite(b,0);
119
        digitalWrite(c,0);
120
        digitalWrite(g,0);
121
        digitalWrite(f,0);
122
        break;
123
      case 5:
124
        digitalWrite(a,1);
125
        digitalWrite(f,1);
126
        digitalWrite(g,1);
127
        digitalWrite(d,1);
128
        digitalWrite(c,1);
129
        digitalWrite(a,0);
130
        digitalWrite(f,0);
131
        digitalWrite(g,0);
132
        digitalWrite(d,0);
133
        digitalWrite(c,0);
134
        break;
135
      case 6:
136
        digitalWrite(a,1);
137
        digitalWrite(c,1);
138
        digitalWrite(d,1);
139
        digitalWrite(e,1);
140
        digitalWrite(f,1);
141
        digitalWrite(g,1);
142
        digitalWrite(a,0);
143
        digitalWrite(c,0);
144
        digitalWrite(d,0);
145
        digitalWrite(e,0);
146
        digitalWrite(f,0);
147
        digitalWrite(g,0);
148
        break;
149
      case 7:
150
        digitalWrite(a,1);
151
        digitalWrite(b,1);
152
        digitalWrite(c,1);
153
        digitalWrite(a,0);
154
        digitalWrite(b,0);
155
        digitalWrite(c,0);
156
        break;
157
      case 8:
158
        digitalWrite(a,1);
159
        digitalWrite(b,1);
160
        digitalWrite(c,1);
161
        digitalWrite(d,1);
162
        digitalWrite(e,1);
163
        digitalWrite(f,1);
164
        digitalWrite(g,1);
165
        digitalWrite(a,0);
166
        digitalWrite(b,0);
167
        digitalWrite(c,0);
168
        digitalWrite(d,0);
169
        digitalWrite(e,0);
170
        digitalWrite(f,0);
171
        digitalWrite(g,0);
172
        break;
173
      case 9:
174
        digitalWrite(a,1);
175
        digitalWrite(b,1);
176
        digitalWrite(c,1);
177
        digitalWrite(d,1);
178
        digitalWrite(f,1);
179
        digitalWrite(g,1);
180
        digitalWrite(a,0);
181
        digitalWrite(b,0);
182
        digitalWrite(c,0);
183
        digitalWrite(d,0);
184
        digitalWrite(f,0);
185
        digitalWrite(g,0);
186
        break;
187
    }//Close Switch
188
    digitalWrite(dig1,1);
189
  }//Close 'th' IF
190
 
191
  if(1){
192
    digitalWrite(dig2,0);
193
    switch(hun){
194
      case 0:
195
        digitalWrite(a,1);
196
        digitalWrite(b,1);
197
        digitalWrite(c,1);
198
        digitalWrite(d,1);
199
        digitalWrite(e,1);
200
        digitalWrite(f,1);
201
        digitalWrite(a,0);
202
        digitalWrite(b,0);
203
        digitalWrite(c,0);
204
        digitalWrite(d,0);
205
        digitalWrite(e,0);
206
        digitalWrite(f,0);
207
        break;
208
      case 1:
209
        digitalWrite(b,1);
210
        digitalWrite(c,1);
211
        digitalWrite(b,0);
212
        digitalWrite(c,0);
213
        break;
214
      case 2:
215
        digitalWrite(a,1);
216
        digitalWrite(b,1);
217
        digitalWrite(g,1);
218
        digitalWrite(d,1);
219
        digitalWrite(e,1);
220
        digitalWrite(a,0);
221
        digitalWrite(b,0);
222
        digitalWrite(g,0);
223
        digitalWrite(d,0);
224
        digitalWrite(e,0);
225
        break;
226
      case 3:
227
        digitalWrite(a,1);
228
        digitalWrite(b,1);
229
        digitalWrite(g,1);
230
        digitalWrite(c,1);
231
        digitalWrite(c,1);
232
        digitalWrite(d,1);
233
        digitalWrite(a,0);
234
        digitalWrite(b,0);
235
        digitalWrite(g,0);
236
        digitalWrite(c,0);
237
        digitalWrite(c,0);
238
        digitalWrite(d,0);
239
        break;
240
      case 4:
241
        digitalWrite(b,1);
242
        digitalWrite(c,1);
243
        digitalWrite(g,1);
244
        digitalWrite(f,1);
245
        digitalWrite(b,0);
246
        digitalWrite(c,0);
247
        digitalWrite(g,0);
248
        digitalWrite(f,0);
249
        break;
250
      case 5:
251
        digitalWrite(a,1);
252
        digitalWrite(f,1);
253
        digitalWrite(g,1);
254
        digitalWrite(d,1);
255
        digitalWrite(c,1);
256
        digitalWrite(a,0);
257
        digitalWrite(f,0);
258
        digitalWrite(g,0);
259
        digitalWrite(d,0);
260
        digitalWrite(c,0);
261
        break;
262
      case 6:
263
        digitalWrite(a,1);
264
        digitalWrite(c,1);
265
        digitalWrite(d,1);
266
        digitalWrite(e,1);
267
        digitalWrite(f,1);
268
        digitalWrite(g,1);
269
        digitalWrite(a,0);
270
        digitalWrite(c,0);
271
        digitalWrite(d,0);
272
        digitalWrite(e,0);
273
        digitalWrite(f,0);
274
        digitalWrite(g,0);
275
        break;
276
      case 7:
277
        digitalWrite(a,1);
278
        digitalWrite(b,1);
279
        digitalWrite(c,1);
280
        digitalWrite(a,0);
281
        digitalWrite(b,0);
282
        digitalWrite(c,0);
283
        break;
284
      case 8:
285
        digitalWrite(a,1);
286
        digitalWrite(b,1);
287
        digitalWrite(c,1);
288
        digitalWrite(d,1);
289
        digitalWrite(e,1);
290
        digitalWrite(f,1);
291
        digitalWrite(g,1);
292
        digitalWrite(a,0);
293
        digitalWrite(b,0);
294
        digitalWrite(c,0);
295
        digitalWrite(d,0);
296
        digitalWrite(e,0);
297
        digitalWrite(f,0);
298
        digitalWrite(g,0);
299
        break;
300
      case 9:
301
        digitalWrite(a,1);
302
        digitalWrite(b,1);
303
        digitalWrite(c,1);
304
        digitalWrite(d,1);
305
        digitalWrite(f,1);
306
        digitalWrite(g,1);
307
        digitalWrite(a,0);
308
        digitalWrite(b,0);
309
        digitalWrite(c,0);
310
        digitalWrite(d,0);
311
        digitalWrite(f,0);
312
        digitalWrite(g,0);
313
        break;
314
    }//Close Switch
315
    digitalWrite(dig2,1);
316
  }//Close 'hun' IF
317
 
318
  if(1){
319
    digitalWrite(dig3,0);
320
    switch(ten){
321
      case 0:
322
        digitalWrite(a,1);
323
        digitalWrite(b,1);
324
        digitalWrite(c,1);
325
        digitalWrite(d,1);
326
        digitalWrite(e,1);
327
        digitalWrite(f,1);
328
        digitalWrite(a,0);
329
        digitalWrite(b,0);
330
        digitalWrite(c,0);
331
        digitalWrite(d,0);
332
        digitalWrite(e,0);
333
        digitalWrite(f,0);
334
        break;
335
      case 1:
336
        digitalWrite(b,1);
337
        digitalWrite(c,1);
338
        digitalWrite(b,0);
339
        digitalWrite(c,0);
340
        break;
341
      case 2:
342
        digitalWrite(a,1);
343
        digitalWrite(b,1);
344
        digitalWrite(g,1);
345
        digitalWrite(d,1);
346
        digitalWrite(e,1);
347
        digitalWrite(a,0);
348
        digitalWrite(b,0);
349
        digitalWrite(g,0);
350
        digitalWrite(d,0);
351
        digitalWrite(e,0);
352
        break;
353
      case 3:
354
        digitalWrite(a,1);
355
        digitalWrite(b,1);
356
        digitalWrite(g,1);
357
        digitalWrite(c,1);
358
        digitalWrite(c,1);
359
        digitalWrite(d,1);
360
        digitalWrite(a,0);
361
        digitalWrite(b,0);
362
        digitalWrite(g,0);
363
        digitalWrite(c,0);
364
        digitalWrite(c,0);
365
        digitalWrite(d,0);
366
        break;
367
      case 4:
368
        digitalWrite(b,1);
369
        digitalWrite(c,1);
370
        digitalWrite(g,1);
371
        digitalWrite(f,1);
372
        digitalWrite(b,0);
373
        digitalWrite(c,0);
374
        digitalWrite(g,0);
375
        digitalWrite(f,0);
376
        break;
377
      case 5:
378
        digitalWrite(a,1);
379
        digitalWrite(f,1);
380
        digitalWrite(g,1);
381
        digitalWrite(d,1);
382
        digitalWrite(c,1);
383
        digitalWrite(a,0);
384
        digitalWrite(f,0);
385
        digitalWrite(g,0);
386
        digitalWrite(d,0);
387
        digitalWrite(c,0);
388
        break;
389
      case 6:
390
        digitalWrite(a,1);
391
        digitalWrite(c,1);
392
        digitalWrite(d,1);
393
        digitalWrite(e,1);
394
        digitalWrite(f,1);
395
        digitalWrite(g,1);
396
        digitalWrite(a,0);
397
        digitalWrite(c,0);
398
        digitalWrite(d,0);
399
        digitalWrite(e,0);
400
        digitalWrite(f,0);
401
        digitalWrite(g,0);
402
        break;
403
      case 7:
404
        digitalWrite(a,1);
405
        digitalWrite(b,1);
406
        digitalWrite(c,1);
407
        digitalWrite(a,0);
408
        digitalWrite(b,0);
409
        digitalWrite(c,0);
410
        break;
411
      case 8:
412
        digitalWrite(a,1);
413
        digitalWrite(b,1);
414
        digitalWrite(c,1);
415
        digitalWrite(d,1);
416
        digitalWrite(e,1);
417
        digitalWrite(f,1);
418
        digitalWrite(g,1);
419
        digitalWrite(a,0);
420
        digitalWrite(b,0);
421
        digitalWrite(c,0);
422
        digitalWrite(d,0);
423
        digitalWrite(e,0);
424
        digitalWrite(f,0);
425
        digitalWrite(g,0);
426
        break;
427
      case 9:
428
        digitalWrite(a,1);
429
        digitalWrite(b,1);
430
        digitalWrite(c,1);
431
        digitalWrite(d,1);
432
        digitalWrite(f,1);
433
        digitalWrite(g,1);
434
        digitalWrite(a,0);
435
        digitalWrite(b,0);
436
        digitalWrite(c,0);
437
        digitalWrite(d,0);
438
        digitalWrite(f,0);
439
        digitalWrite(g,0);
440
        break;
441
    }//Close Switch
442
    digitalWrite(dig3,1);
443
  }//Close 'ten' IF
444
 
445
  if(1){
446
    digitalWrite(dig4,0);
447
    switch(one){
448
      case 0:
449
        digitalWrite(a,1);
450
        digitalWrite(b,1);
451
        digitalWrite(c,1);
452
        digitalWrite(d,1);
453
        digitalWrite(e,1);
454
        digitalWrite(f,1);
455
        digitalWrite(a,0);
456
        digitalWrite(b,0);
457
        digitalWrite(c,0);
458
        digitalWrite(d,0);
459
        digitalWrite(e,0);
460
        digitalWrite(f,0);
461
        break;
462
      case 1:
463
        digitalWrite(b,1);
464
        digitalWrite(c,1);
465
        digitalWrite(b,0);
466
        digitalWrite(c,0);
467
        break;
468
      case 2:
469
        digitalWrite(a,1);
470
        digitalWrite(b,1);
471
        digitalWrite(g,1);
472
        digitalWrite(d,1);
473
        digitalWrite(e,1);
474
        digitalWrite(a,0);
475
        digitalWrite(b,0);
476
        digitalWrite(g,0);
477
        digitalWrite(d,0);
478
        digitalWrite(e,0);
479
        break;
480
      case 3:
481
        digitalWrite(a,1);
482
        digitalWrite(b,1);
483
        digitalWrite(g,1);
484
        digitalWrite(c,1);
485
        digitalWrite(c,1);
486
        digitalWrite(d,1);
487
        digitalWrite(a,0);
488
        digitalWrite(b,0);
489
        digitalWrite(g,0);
490
        digitalWrite(c,0);
491
        digitalWrite(c,0);
492
        digitalWrite(d,0);
493
        break;
494
      case 4:
495
        digitalWrite(b,1);
496
        digitalWrite(c,1);
497
        digitalWrite(g,1);
498
        digitalWrite(f,1);
499
        digitalWrite(b,0);
500
        digitalWrite(c,0);
501
        digitalWrite(g,0);
502
        digitalWrite(f,0);
503
        break;
504
      case 5:
505
        digitalWrite(a,1);
506
        digitalWrite(f,1);
507
        digitalWrite(g,1);
508
        digitalWrite(d,1);
509
        digitalWrite(c,1);
510
        digitalWrite(a,0);
511
        digitalWrite(f,0);
512
        digitalWrite(g,0);
513
        digitalWrite(d,0);
514
        digitalWrite(c,0);
515
        break;
516
      case 6:
517
        digitalWrite(a,1);
518
        digitalWrite(c,1);
519
        digitalWrite(d,1);
520
        digitalWrite(e,1);
521
        digitalWrite(f,1);
522
        digitalWrite(g,1);
523
        digitalWrite(a,0);
524
        digitalWrite(c,0);
525
        digitalWrite(d,0);
526
        digitalWrite(e,0);
527
        digitalWrite(f,0);
528
        digitalWrite(g,0);
529
        break;
530
      case 7:
531
        digitalWrite(a,1);
532
        digitalWrite(b,1);
533
        digitalWrite(c,1);
534
        digitalWrite(a,0);
535
        digitalWrite(b,0);
536
        digitalWrite(c,0);
537
        break;
538
      case 8:
539
        digitalWrite(a,1);
540
        digitalWrite(b,1);
541
        digitalWrite(c,1);
542
        digitalWrite(d,1);
543
        digitalWrite(e,1);
544
        digitalWrite(f,1);
545
        digitalWrite(g,1);
546
        digitalWrite(a,0);
547
        digitalWrite(b,0);
548
        digitalWrite(c,0);
549
        digitalWrite(d,0);
550
        digitalWrite(e,0);
551
        digitalWrite(f,0);
552
        digitalWrite(g,0);
553
        break;
554
      case 9:
555
        digitalWrite(a,1);
556
        digitalWrite(b,1);
557
        digitalWrite(c,1);
558
        digitalWrite(d,1);
559
        digitalWrite(f,1);
560
        digitalWrite(g,1);
561
        digitalWrite(a,0);
562
        digitalWrite(b,0);
563
        digitalWrite(c,0);
564
        digitalWrite(d,0);
565
        digitalWrite(f,0);
566
        digitalWrite(g,0);
567
        break;
568
    }//Close Switch
569
    digitalWrite(dig4,1);
570
  }//Close 'one' IF
571
}

1 comment for “Arduino Update

  1. August 25, 2014 at 18:59

    I read a lot of interesting content here. Probably you spend a lot of time writing, i know how to save you a lot of time, there is an online
    tool that creates unique, google friendly articles in minutes, just type
    in google – laranitas free content source

Leave a Reply

Your email address will not be published. Required fields are marked *