Skip to content

Commit 05797df

Browse files
committed
Added arduino project code
0 parents  commit 05797df

File tree

15 files changed

+1612
-0
lines changed

15 files changed

+1612
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Controlling Piezo Buzzer With a Push Button Switch
3+
4+
This program reproduces a frequency when pressing a push-button
5+
6+
Piezo Asigned to Pin 13
7+
Push-button Asigned to Pin 12
8+
9+
The circuit:
10+
1 Piezo Buzzer
11+
1 Push-button
12+
13+
Created 10/3/19
14+
By Miguel Osuna
15+
Modified: -/-/-
16+
By: -----------
17+
*/
18+
19+
const int buttonPin = 7;
20+
const int buzzerPin = 6;
21+
int freq;
22+
23+
void setup()
24+
{
25+
// Define the inputs and outputs
26+
pinMode(buttonPin, INPUT_PULLUP);
27+
pinMode(buzzerPin, OUTPUT);
28+
29+
// Begin serial communication
30+
Serial.begin(9600);
31+
Serial.println("Please enter a frequency for you buzzer in Hz: ");
32+
freq = Serial.read();
33+
}
34+
35+
void loop()
36+
{
37+
// Read the buttons state ('1' for floating value)
38+
int buttonState = digitalRead(buttonPin);
39+
40+
// 'LOW' when the button is pressed
41+
if(buttonState == LOW)
42+
tone(buzzerPin, freq);
43+
else
44+
noTone(buzzerPin);
45+
}
46+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Motor Speed Control
3+
4+
This program let's the user control the speed of a motor from the Serial Monitor
5+
6+
Transitor Base Attached Pin 6 (This controls the motor)
7+
8+
The circuit:
9+
1 Power Transistor
10+
1 DC Motor
11+
12+
Created 10/3/19
13+
By Miguel Osuna
14+
Modified: -/-/-
15+
By: -----------
16+
*/
17+
18+
const int dcMotor = 6;
19+
20+
void setup()
21+
{
22+
// Configure the transistor pin as an output
23+
pinMode(dcMotor, OUTPUT);
24+
25+
// Display a message to the user
26+
Serial.begin(9600);
27+
Serial.print("Please enter a number between 0 and 9: ");
28+
}
29+
30+
void loop()
31+
{
32+
char userInput;
33+
34+
//Check for an input from the user
35+
if(Serial.available() > 0)
36+
{
37+
// The user inputs a character
38+
userInput = Serial.read();
39+
40+
if(userInput >= '0' && userInput <= '9')
41+
{
42+
int motorSpeed = userInput - '0';
43+
motorSpeed = map(motorSpeed, 0, 9, 0, 255);
44+
analogWrite(dcMotor, motorSpeed);
45+
}
46+
}
47+
}
48+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Digital Dice Roller
3+
4+
This program displays a number entered by the user, from 0 to F (15) in hexadecimal on the 7-segment display
5+
6+
Push-Button Asigned to Pin 1
7+
7-Segment Display 'a' pin Asigned to Pin 2
8+
7-Segment Display 'b' pin Asigned to Pin 3
9+
7-Segment Display 'c' pin Asigned to Pin 4
10+
7-Segment Display 'd' pin Asigned to Pin 5
11+
7-Segment Display 'e' pin Asigned to Pin 6
12+
7-Segment Display 'f' pin Asigned to Pin 7
13+
7-Segment Display 'g' pin Asigned to Pin 8
14+
7-Segment Display '.' pin Asigned to Pin 9
15+
16+
The circuit:
17+
1 7-Segment Display Common Anode or Common Cathode
18+
8 Resistors
19+
1 Push-Button
20+
21+
Created 11/3/19
22+
By Miguel Osuna
23+
Modified: -/-/-
24+
By: -----------
25+
*/
26+
27+
const int buttonPin = 1;
28+
int sevenSegmentDisplayPins[] = {2, 3, 4, 5, 6, 7, 8};
29+
30+
// a, b, c, d, e, f, g, .
31+
byte digits[6][8] =
32+
{
33+
{0, 1, 1, 0, 0, 0, 0, 0}, //1
34+
{1, 1, 0, 1, 1, 0, 1, 0}, //2
35+
{1, 1, 1, 1, 0, 0, 1, 0}, //3
36+
{0, 1, 1, 0, 0, 1, 1, 0}, //4
37+
{1, 0, 1, 1, 0, 1, 1, 0}, //5
38+
{1, 0, 1, 1, 1, 1, 1, 0}, //6
39+
};
40+
41+
byte spinDigits[6][8] =
42+
{
43+
{1, 0, 0, 0, 0, 0, 0 ,0},
44+
{0, 1, 0, 0, 0, 0, 0, 0},
45+
{0, 0, 1, 0, 0, 0, 0, 0},
46+
{0, 0, 0, 1, 0, 0, 0, 0},
47+
{0, 0, 0, 0, 1, 0, 0, 0},
48+
{0, 0, 0, 0, 0, 1, 0, 0},
49+
};
50+
51+
void setup()
52+
{
53+
for(int i = 0; i < 8; i++)
54+
{
55+
pinMode(sevenSegmentDisplayPins[i], OUTPUT);
56+
digitalWrite(sevenSegmentDisplayPins[i], LOW);
57+
}
58+
pinMode(buttonPin, INPUT_PULLUP);
59+
}
60+
61+
void loop()
62+
{
63+
64+
// Wait for the push-button to be pressed
65+
// We espect a LOW, because of the Internal Pull-up resistor
66+
if(digitalRead(buttonPin) == LOW)
67+
{
68+
spin();
69+
rollDice();
70+
delay(500);
71+
}
72+
73+
}
74+
75+
void numberToDisplay(int number)
76+
{
77+
for(int i = 0; i < 8; i++)
78+
digitalWrite(sevenSegmentDisplayPins[i], digits[number-1][i]);
79+
}
80+
81+
void rollDice()
82+
{
83+
numberToDisplay(random(1,7));
84+
}
85+
86+
void spin()
87+
{
88+
for (int j = 0; j < 6; j++)
89+
{
90+
for (int i = 0; i < 8; i++)
91+
digitalWrite(sevenSegmentDisplayPins[i], spinDigits[j][i]);
92+
delay(250);
93+
}
94+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
Display Number on 4-digit Seven Segment Display
3+
4+
This program displays a number entered by the user, from 0000 to 9999 in on the 4-digit 7-segment display
5+
6+
7-Segment Display 'a' pin (11) Asigned to Pin 2
7+
7-Segment Display 'b' pin (7) Asigned to Pin 3
8+
7-Segment Display 'c' pin (4) Asigned to Pin 4
9+
7-Segment Display 'd' pin (2) Asigned to Pin 5
10+
7-Segment Display 'e' pin (1) Asigned to Pin 6
11+
7-Segment Display 'f' pin (10) Asigned to Pin 7
12+
7-Segment Display 'g' pin (5) Asigned to Pin 8
13+
7-Segment Display '.' pin (3) Asigned to Pin 9
14+
15+
Digit 1 Enable pin (12) Asigned to Pin 10
16+
Digit 2 Enable pin (9) Asigned to Pin 11
17+
Digit 3 Enable pin (8) Asigned to Pin 12
18+
Digit 4 Enable pin (6) Asigned to Pin 13
19+
20+
The circuit:
21+
1 4-digit 7-Segment Display Common Anode or Common Cathode
22+
8 Resistors
23+
24+
Created 22/3/19
25+
By Miguel Osuna
26+
Modified: -/-/-
27+
By: -----------
28+
*/
29+
30+
int sevenSegmentDisplayPins[8] = {2, 3, 4, 5, 6, 7, 8, 9};
31+
int digitPins[4] = {10, 11, 12, 13};
32+
33+
// a, b, c, d, e, f, g, .
34+
byte digits[10][8] =
35+
{
36+
{1, 1, 1, 1, 1, 1, 0, 0}, //0
37+
{0, 1, 1, 0, 0, 0, 0, 0}, //1
38+
{1, 1, 0, 1, 1, 0, 1, 0}, //2
39+
{1, 1, 1, 1, 0, 0, 1, 0}, //3
40+
{0, 1, 1, 0, 0, 1, 1, 0}, //4
41+
{1, 0, 1, 1, 0, 1, 1, 0}, //5
42+
{1, 0, 1, 1, 1, 1, 1, 0}, //6
43+
{1, 1, 1, 0, 0, 0, 0, 0}, //7
44+
{1, 1, 1, 1, 1, 1, 1, 0}, //8
45+
{1, 1, 1, 0, 0, 1, 1, 0}, //9
46+
};
47+
48+
int displayNumbers[4] = {0};
49+
50+
void setup()
51+
{
52+
for(int i = 0; i < 8; i++)
53+
{
54+
pinMode(sevenSegmentDisplayPins[i], OUTPUT);
55+
digitalWrite(sevenSegmentDisplayPins[i], LOW);
56+
}
57+
58+
for(int i = 0; i < 4; i++)
59+
{
60+
pinMode(digitPins[i], OUTPUT);
61+
digitalWrite(digitPins[i], LOW);
62+
}
63+
64+
Serial.begin(9600);
65+
Serial.setTimeout(20);
66+
Serial.print("Please enter a number between 0000 and 9999 (with a 4 digit format): ");
67+
}
68+
69+
void loop()
70+
{
71+
// Wait for something in the Serial Port
72+
// Get the input from the user
73+
74+
static String input;
75+
76+
if (Serial.available())
77+
{
78+
//We read the users input as a string to then separate character by character
79+
input = Serial.readString();
80+
Serial.println();
81+
82+
//Converting numbers to integers
83+
for(int i = 0; i < 4; i++)
84+
{
85+
//Read input string and convert each character to a number to be added to the displayNumbers array
86+
displayNumbers[i] = input.charAt(i) - '0';
87+
Serial.print(displayNumbers[i]);
88+
}
89+
Serial.println();
90+
}
91+
92+
clearDisplay();
93+
updateDigits(displayNumbers);
94+
}
95+
96+
void updateDigits(int displayNumbers[])
97+
{
98+
//Because the Display is Common Cathode, we have to set with the opposite State
99+
//for each digit Pins
100+
101+
//We set LOW to the output of the First Digit and HIGH to all others
102+
digitalWrite(digitPins[0], !HIGH);
103+
digitalWrite(digitPins[1], !LOW);
104+
digitalWrite(digitPins[2], !LOW);
105+
digitalWrite(digitPins[3], !LOW);
106+
numberToDisplay(displayNumbers[0]);
107+
delay(5);
108+
109+
//We set LOW to the output of the Second Digit and HIGH to all others
110+
digitalWrite(digitPins[0], !LOW);
111+
digitalWrite(digitPins[1], !HIGH);
112+
digitalWrite(digitPins[2], !LOW);
113+
digitalWrite(digitPins[3], !LOW);
114+
numberToDisplay(displayNumbers[1]);
115+
delay(5);
116+
117+
//We set LOW to the output of the Third Digit and HIGH to all others
118+
digitalWrite(digitPins[0], !LOW);
119+
digitalWrite(digitPins[1], !LOW);
120+
digitalWrite(digitPins[2], !HIGH);
121+
digitalWrite(digitPins[3], !LOW);
122+
numberToDisplay(displayNumbers[2]);
123+
delay(5);
124+
125+
//We set LOW to the output of the Fourth Digit and HIGH to all others
126+
digitalWrite(digitPins[0], !LOW);
127+
digitalWrite(digitPins[1], !LOW);
128+
digitalWrite(digitPins[2], !LOW);
129+
digitalWrite(digitPins[3], !HIGH);
130+
numberToDisplay(displayNumbers[3]);
131+
delay(5);
132+
}
133+
134+
void numberToDisplay(int number)
135+
{
136+
for(int i = 0; i < 8; i++)
137+
// The display is Common Anode, so we turn on each LED with a HIGH state
138+
digitalWrite(sevenSegmentDisplayPins[i], digits[number][i]);
139+
}
140+
141+
void clearDisplay()
142+
{
143+
for(int i = 0; i < 8; i++)
144+
digitalWrite(sevenSegmentDisplayPins[i], LOW);
145+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Push-Button Interrupt Example
3+
4+
An interrupt is attached to a push-button so it can be monitored
5+
6+
Push-button Asigned to Pin 2 (INT-0)
7+
8+
The circuit:
9+
1 Push-button
10+
1 LED
11+
12+
Created 11/3/19
13+
By Miguel Osuna
14+
Modified: -/-/-
15+
By: -----------
16+
*/
17+
18+
const byte buttonPin = 2;
19+
const byte ledPin = 13;
20+
volatile byte buttonState = LOW;
21+
22+
void setup()
23+
{
24+
pinMode(buttonPin, INPUT_PULLUP);
25+
pinMode(ledPin, OUTPUT);
26+
//digitalWrite(ledPin, LOW);
27+
28+
// We use the first parameter to indicate the Interrupt Pin
29+
// The second parameter is the name of the Interrupt Service Routine
30+
// The third parameter is to activate the interrupt when there is a change from HIGH to LOW and from LOW to HIGH
31+
attachInterrupt(digitalPinToInterrupt(buttonPin), toggleLed, CHANGE);
32+
}
33+
34+
void loop()
35+
{
36+
// We toggle the LED by changing the button state with the ISR
37+
digitalWrite(ledPin, buttonState);
38+
}
39+
40+
void toggleLed()
41+
{
42+
// Negate the button state
43+
buttonState = !buttonState;
44+
}
45+

0 commit comments

Comments
 (0)