// 2-dimensional array of row pin numbers: const int row[8] = { 2, 7, 19, 5, 13,
ID: 3730160 • Letter: #
Question
// 2-dimensional array of row pin numbers:
const int row[8] = {
2, 7, 19, 5, 13, 18, 12, 16
};
// 2-dimensional array of column pin numbers:
const int col[8] = {
6, 11, 10, 3, 17, 4, 8, 9
};
// 2-dimensional array of pixels:
int pixels[8][8];
// cursor position:
int x = 5;
int y = 5;
void setup() {
// initialize the I/O pins as outputs iterate over the pins:
for (int thisPin = 0; thisPin < 8; thisPin++) {
// initialize the output pins:
pinMode(col[thisPin], OUTPUT);
pinMode(row[thisPin], OUTPUT);
// take the col pins (i.e. the cathodes) high to ensure that the LEDS are off:
digitalWrite(col[thisPin], HIGH);
}
// initialize the pixel matrix:
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
pixels[x][y] = HIGH;
}
}
}
void loop() {
// read input:
readSensors();
// draw the screen:
refreshScreen();
}
void readSensors() {
// turn off the last position:
pixels[x][y] = HIGH;
// read the sensors for X and Y values:
x = 7 - map(analogRead(A0), 0, 1023, 0, 7);
y = map(analogRead(A1), 0, 1023, 0, 7);
// set the new pixel position low so that the LED will turn on in the next
// screen refresh:
pixels[x][y] = LOW;
}
void refreshScreen() {
// iterate over the rows (anodes):
for (int thisRow = 0; thisRow < 8; thisRow++) {
// take the row pin (anode) high:
digitalWrite(row[thisRow], HIGH);
// iterate over the cols (cathodes):
for (int thisCol = 0; thisCol < 8; thisCol++) {
// get the state of the current pixel;
int thisPixel = pixels[thisRow][thisCol];
// when the row is HIGH and the col is LOW,
// the LED where they meet turns on:
digitalWrite(col[thisCol], thisPixel);
// turn the pixel off:
if (thisPixel == LOW) {
digitalWrite(col[thisCol], HIGH);
}
}
// take the row pin low to turn off the whole row:
digitalWrite(row[thisRow], LOW);
}
}
I need help creating an Arduino Powered Binary Clock Project. I need to design a c++ program that through the Arduino IDE control a dot matrix in a circuit constructed in a breadboard it display a sequence to simulate an analog clock. An example of an Arduino program:
Explanation / Answer
You can try the below code that may be usefull for you...
#define vers = "1.01"
#define ledPin 13
#define secondPin 4
#define minutePin 5
#define hourPin 6
#define weekdayPin 7
int second=0, minute=0, hour=0, weekday=1; // declare time variables
// these time variables are declared globally so they can be used ANYWHERE in your program
void setup() {
blinkLED(ledPin, 4, 100); // blink an LED at the start of the program, to show the code is running
Serial.begin(9600); // start up serial communications
pinMode(secondPin, INPUT); //pins for normally closed switches to set the time
pinMode(minutePin, INPUT);
pinMode(hourPin, INPUT);
pinMode(weekdayPin, INPUT);
digitalWrite(secondPin, HIGH); // writing an input high turns on pull-up resistors
digitalWrite(minutePin, HIGH);
digitalWrite(hourPin, HIGH);
digitalWrite(weekdayPin, HIGH);
}
void loop() {
static unsigned long lastTick = 0; // set up a local variable to hold the last time we moved forward one second
// (static variables are initialized once and keep their values between function calls)
// move forward one second every 1000 milliseconds
if (millis() - lastTick >= 1000) {
lastTick = millis();
serialOutput();
second++;
}
// move forward one minute every 60 seconds
if (second > 59) {
minute++;
second = 0; // reset seconds to zero
}
// move forward one hour every 60 minutes
if (minute > 59) {
hour++;
minute = 0; // reset minutes to zero
}
// move forward one weekday every 24 hours
if (hour > 23) {
weekday++;
hour = 0; // reset hours to zero
}
// reset weekdays on Saturday
if (weekday > 7) {
weekday = 1;
}
checkButtons(); // runs a function that checks the setting buttons
}
void checkButtons() {
static boolean secPressed=false, minPressed=false, hourPressed=false, wkdayPressed=false; //track button state
if (digitalRead (secondPin)==LOW && secPressed == false) { // if a normally closed switch is pressed
second++; // advance by one second
secPressed = true; // note the pressed state
}
if (digitalRead (secondPin)==HIGH) secPressed = false; // reset the state when the button is released
if (digitalRead (minutePin)==LOW && minPressed == false) {
minute++;
minPressed = true;
}
if (digitalRead (minutePin)==HIGH) minPressed = false;
if (digitalRead (hourPin)==LOW && hourPressed == false) {
hour++;
hourPressed = true;
}
if (digitalRead (hourPin)==HIGH) hourPressed = false;
if (digitalRead (weekdayPin)==LOW && wkdayPressed == false) {
weekday++;
secPressed = true;
if (digitalRead (weekdayPin)==HIGH) wkdayPressed = false;
}
}
void printWeekday (int dayNum) {
// print a weekday, based on the day number
switch (dayNum) {
case 1:
Serial.print ("Sunday");
break;
case 2:
Serial.print ("Monday");
break;
case 3:
Serial.print ("Tuesday");
break;
case 4:
Serial.print ("Wednesday");
break;
case 5:
Serial.print ("Thursday");
break;
case 6:
Serial.print ("Friday");
break;
case 7:
Serial.print ("Saturday");
break;
}
}
void serialOutput() {
// this function creates a clock you can read through the serial port
// your clock project will have a MUCH more interesting way of displaying the time
// get creative!
printWeekday(weekday); // picks the right word to print for the weekday
Serial.print(", "); // a comma after the weekday
Serial.print(hour, DEC); // the hour, sent to the screen in decimal format
Serial.print(":"); // a colon between the hour and the minute
Serial.print(minute, DEC); // the minute, sent to the screen in decimal format
Serial.print(":"); // a colon between the minute and the second
Serial.println(second, DEC); // the second, sent to the screen in decimal format
}
// this utility function blinks the an LED light as many times as requested
void blinkLED(byte targetPin, int numBlinks, int blinkRate) {
for (int i=0; i < numBlinks; i++) {
digitalWrite(targetPin, HIGH); // sets the LED on
delay(blinkRate); // waits for a blinkRate milliseconds
digitalWrite(targetPin, LOW); // sets the LED off
delay(blinkRate);
}
}
#define vers = "1.01"
#define ledPin 13
#define secondPin 4
#define minutePin 5
#define hourPin 6
#define weekdayPin 7
int second=0, minute=0, hour=0, weekday=1; // declare time variables
// these time variables are declared globally so they can be used ANYWHERE in your program
void setup() {
blinkLED(ledPin, 4, 100); // blink an LED at the start of the program, to show the code is running
Serial.begin(9600); // start up serial communications
pinMode(secondPin, INPUT); //pins for normally closed switches to set the time
pinMode(minutePin, INPUT);
pinMode(hourPin, INPUT);
pinMode(weekdayPin, INPUT);
digitalWrite(secondPin, HIGH); // writing an input high turns on pull-up resistors
digitalWrite(minutePin, HIGH);
digitalWrite(hourPin, HIGH);
digitalWrite(weekdayPin, HIGH);
}
void loop() {
static unsigned long lastTick = 0; // set up a local variable to hold the last time we moved forward one second
// (static variables are initialized once and keep their values between function calls)
// move forward one second every 1000 milliseconds
if (millis() - lastTick >= 1000) {
lastTick = millis();
serialOutput();
second++;
}
// move forward one minute every 60 seconds
if (second > 59) {
minute++;
second = 0; // reset seconds to zero
}
// move forward one hour every 60 minutes
if (minute > 59) {
hour++;
minute = 0; // reset minutes to zero
}
// move forward one weekday every 24 hours
if (hour > 23) {
weekday++;
hour = 0; // reset hours to zero
}
// reset weekdays on Saturday
if (weekday > 7) {
weekday = 1;
}
checkButtons(); // runs a function that checks the setting buttons
}
void checkButtons() {
static boolean secPressed=false, minPressed=false, hourPressed=false, wkdayPressed=false; //track button state
if (digitalRead (secondPin)==LOW && secPressed == false) { // if a normally closed switch is pressed
second++; // advance by one second
secPressed = true; // note the pressed state
}
if (digitalRead (secondPin)==HIGH) secPressed = false; // reset the state when the button is released
if (digitalRead (minutePin)==LOW && minPressed == false) {
minute++;
minPressed = true;
}
if (digitalRead (minutePin)==HIGH) minPressed = false;
if (digitalRead (hourPin)==LOW && hourPressed == false) {
hour++;
hourPressed = true;
}
if (digitalRead (hourPin)==HIGH) hourPressed = false;
if (digitalRead (weekdayPin)==LOW && wkdayPressed == false) {
weekday++;
secPressed = true;
if (digitalRead (weekdayPin)==HIGH) wkdayPressed = false;
}
}
void printWeekday (int dayNum) {
// print a weekday, based on the day number
switch (dayNum) {
case 1:
Serial.print ("Sunday");
break;
case 2:
Serial.print ("Monday");
break;
case 3:
Serial.print ("Tuesday");
break;
case 4:
Serial.print ("Wednesday");
break;
case 5:
Serial.print ("Thursday");
break;
case 6:
Serial.print ("Friday");
break;
case 7:
Serial.print ("Saturday");
break;
}
}
void serialOutput() {
// this function creates a clock you can read through the serial port
// your clock project will have a MUCH more interesting way of displaying the time
// get creative!
printWeekday(weekday); // picks the right word to print for the weekday
Serial.print(", "); // a comma after the weekday
Serial.print(hour, DEC); // the hour, sent to the screen in decimal format
Serial.print(":"); // a colon between the hour and the minute
Serial.print(minute, DEC); // the minute, sent to the screen in decimal format
Serial.print(":"); // a colon between the minute and the second
Serial.println(second, DEC); // the second, sent to the screen in decimal format
}
// this utility function blinks the an LED light as many times as requested
void blinkLED(byte targetPin, int numBlinks, int blinkRate) {
for (int i=0; i < numBlinks; i++) {
digitalWrite(targetPin, HIGH); // sets the LED on
delay(blinkRate); // waits for a blinkRate milliseconds
digitalWrite(targetPin, LOW); // sets the LED off
delay(blinkRate);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.