In this part of my Arduino tutorial we’ll create 2 separate projects. First we’ll learn how 7 segment displays work and then create a dice simulation rolling circuit. We’ll then learn how to create a theremin, which is a weird instrument you can play without touching it. In addition we’ll learn about photoresistors, importing libraries, voltage dividers, map and much more.
All of the code follows the video below.
If you like videos like this, consider donating $1, or simply turn off Ad Blocking software. Doing either helps me to continue making free tutorials for all.
Code from the Video
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
// CONTROLLING 7 SEGMENT DISPLAYS // 7 Segment displays can represent individual numbers // A common cathode display has all the cathodes connected // to ground and you turn on a segment by supplying current // to its anode // Pins are labeled starting at top left clockwise G, F, GND, A, B, // DP, C, GND, D, E // Add a library -> Sketch -> Include Library -> Manage Libraries // Enter sevseg -> Click Install #include "SevSeg.h" SevSeg sevseg; // The pin the button is connected to const int buttonPin = 13; // Stores buttons current state int buttonState = 0; // Stores buttons last state int lastButtonState = 0; void setup(){ Serial.begin(9600); // Set pin 13 as an input from our button for finding a // random digit pinMode(buttonPin, INPUT); // The number of digits on my display byte numDigits = 1; // Left empty for a single digit display byte digitPins[] = {}; // Array that defines which pins are assigned to each segment // A - G, DP byte segmentPins[] = {6, 5, 2, 3, 4, 7, 8, 9}; // True when the current limiting resistor is connected in series // like we did with the 1K Ohm resistor bool resistorsOnSegments = true; // I'm using a common cathode display (also COMMON_ANODE) byte hardwareConfig = COMMON_CATHODE; sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments); // Can change brightness from 0 - 100 sevseg.setBrightness(90); // Set starting value as 0 sevseg.setNumber(0); sevseg.refreshDisplay(); } void loop(){ // Get current button state buttonState = digitalRead(buttonPin); // Check the button state if(buttonState != lastButtonState){ // Check if button was pressed if(buttonState == HIGH){ // Print numbers out as if we are searching for // a random value CycleNumbers(); // Display the random number sevseg.setNumber(GetRandomNumber()); sevseg.refreshDisplay(); } // Added to avoid bouncing delay(50); } // Save the current state as the last state lastButtonState = buttonState; // Serial.println(GetRandomNumber()); } // Cycles through numbers and displays them void CycleNumbers(){ for(int i = 0; i < 10; i++){ sevseg.setNumber(i); sevseg.refreshDisplay(); delay(200); } } // Returns a random value from 0 up to but not including 10 int GetRandomNumber(){ // Initialize the random number generator with // a random seed value using an unconnected pin // which has a floating value randomSeed(analogRead(0)); return random(0,10); } // ARDUINO THEREMIN // Will get the changing resistance from the photoresistor // The photoresistor changes the resistance provided depending // how much light it receives (Resistance increases with increasing light) // It has no orientation like other resistors // The photoresistor in series with the resistor is a circuit known // as a Voltage Dividor // They turn a large voltage into a smaller one // Vout = Vin * R2 / R1 + R2 const int photoRPin = A0; // Used to output a tone to the passive buzzer const int buzzerOutPin = 11; int photoRVal = 0; int toneVal = 0; void setup() { Serial.begin(9600); } void loop() { // Read resistance value from the analog port (0 (0V) - 1023 (5V)) photoRVal = analogRead(photoRPin); Serial.print("Photoresistor : "); Serial.println(photoRVal); // Scale the output tone from 0 - 1023 to 120 - 1500 // Tones normally range from 31 - 4978 toneVal = map(photoRVal, 0, 1023, 120, 1500); // Play a tone on the passive buzzer tone(buzzerOutPin, toneVal); // This delay pauses the program long enough to get another // sensor reading (This should normally end your loops) delay(2); } |
Leave a Reply