Experiment 4: Arrays
Arrays
An array is a list of items, which could be numbers, or alphabetic characters, accessible by specifying the position in the list. Setting up an array in “C” begins with specifying what each item is:
specifies that each item in the array is one byte, and that there are six items.
similarly specifies that each item is an (16-bit wide) integer, and that there are 10 items.
Other array types are possible, which are specified using the same notation as for variables.
The contents of an array can be specified at the same time as the array is defined, for example:
sets up an array called numbers, containing the positive integers 1, 2, 3…
Listing 24.1 is a program which sets up an array containing integers, fetches them out of the array one at a time and displays them on one of the sets of LEDs. A short delay allows the numbers to be read, otherwise they would flash by too quickly to see! After displaying the individual numbers, the sum of the numbers is displayed for a longer period, then it starts all over again.
Create an Arduino sketch and paste in the program from Listing 24.1. Change the digits in the array to correspond to the digits in your student number; if your student number has six digits, begin with a leading zero so that there are seven items in the array.
Look at the for(;;)
loop in the programme. It starts at zero, because in “C” array index numbering starts from zero.
Compile and upload the programme, then check that the digits of your student number are displayed in order and that the sum of the digits is correct. You do not need to make any changes to the plug-in breadboard.
Draw a flow-chart of the program listed in Listing 24.1.
Exercise
Now that you have the program from Listing 24.1 as a model, write a program as follows:
- Create two arrays, one containing your student number and the other containing the last 7 digits of your telephone number.
- Write a program which examines the two buttons. If neither button is pressed, continue checking the status of the buttons (look at Experiment 2 for a reminder how to examine the buttons).
- If the left button is pressed, use the program from Listing 24.1 to read and display the contents of one of the arrays, finishing with displaying the sum of the digits.
- Use the other button to read and display the contents of the second array.
- When the display operation is complete, return to examining the switches.
Assessment of Experiment 4
When you are satisfied with the programme, copy and paste it into you lab diary. Don’t forget to put in plenty of comments (preceded by the double oblique stroke, “//”.)
Draw a flow chart of the program with two arrays.
Code Listing
Program using an array.
View or download code from GitHub Gist arrays.ino.
Wokwi simulation of program using an array
You can run a wokwi simulation of this circuit which was created by EEE project student Yousef Alsayegh (class of 2024-2025). The link is Lab 4: Program using an array and it uses the code from Listing 24.1. You can copy and edit this to create your own simulation of the assessed program with two arrays.
If you do, it would be useful to provide links to the simulations in your lab diary.
Copyright © 2021-2024 Swansea University. All rights reserved.