Skip to content

Instantly share code, notes, and snippets.

@idriszmy
Last active October 6, 2021 06:26
Show Gist options
  • Save idriszmy/db953f3558356f09261a966c95ed6cce to your computer and use it in GitHub Desktop.
Save idriszmy/db953f3558356f09261a966c95ed6cce to your computer and use it in GitHub Desktop.
Keyestudio Multi-Purpose Shield V2 simple test.
/*
Keyestudio Multi-Purpose Shield V2 simple test.
Board:
- Maker UNO https://my.cytron.io/p-maker-uno-simplifying-arduino-for-education?tracking=idris
- Keyestudio Multi-Purpose Shield V2
References:
- https://wiki.keyestudio.com/Ks0184_keyestudio_Multi-purpose_Shield_V2
Last Modified: 6 Oct 2021
*/
// Pins for 74HC595
#define LATCH_PIN 4
#define CLOCK_PIN 5
#define DATA_PIN 2
#define VAR A0
// Buttons
#define S1 A1
#define S2 A2
#define S3 A3
// Piezo buzzer
#define BUZZER 3
#define NONE 0x00
// Select 7-segment
int segment[4] = {
0x01, 0x02, 0x04, 0x08
};
// Show numbers (0-9) on 7-segment
int number[10] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99,
0x92, 0x82, 0xF8, 0x80, 0x90
};
#define NOTE_G3 196
#define NOTE_A3 220
#define NOTE_B3 247
#define NOTE_C4 262
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
int led = 0;
void setup ()
{
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(VAR, INPUT);
pinMode(S1, INPUT);
pinMode(S2, INPUT);
pinMode(S3, INPUT);
pinMode(BUZZER, OUTPUT);
// Turn off all LEDs
for (led = 8; led < 14; led++) {
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
}
}
void loop()
{
// Button 1 is pressed
if (digitalRead(S1) == LOW) {
while (digitalRead(S1) == LOW) {
display_numbers(9380);
}
}
// Button 2 is pressed
else if (digitalRead(S2) == LOW) {
clear_numbers();
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(BUZZER, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(BUZZER);
}
}
// Button 3 is pressed
else if (digitalRead(S3) == LOW) {
clear_numbers();
for (led = 8; led < 14; led++) {
digitalWrite(led, LOW); // Light up LED
delay(100);
}
for (led = 8; led < 14; led++) {
digitalWrite(led, HIGH); // Turn off LED
delay(100);
}
}
// No button pressed
else {
int adc = analogRead(VAR);
display_numbers(adc);
}
}
void display_numbers(int value)
{
int thousand = value / 1000;
int hundred = value % 1000;
hundred = hundred / 100;
int ten = value % 100;
ten = ten / 10;
int one = value % 10;
if (thousand) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, number[thousand]);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, segment[0]);
digitalWrite(LATCH_PIN, HIGH);
}
if (thousand || hundred) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, number[hundred]);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, segment[1]);
digitalWrite(LATCH_PIN, HIGH);
}
if (thousand || hundred || ten) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, number[ten]);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, segment[2]);
digitalWrite(LATCH_PIN, HIGH);
}
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, number[one]);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, segment[3]);
digitalWrite(LATCH_PIN, HIGH);
}
void clear_numbers()
{
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, NONE);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, NONE);
digitalWrite(LATCH_PIN, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment