Arduino Tutorial 4

Arduino ProgrammingIn 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

// 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

Your email address will not be published. Required fields are marked *