Friday, October 28, 2016

OVERVIEW

In some Arduino projects we will end up using Potentiometers.

The potentiometer (or pot, as it is more commonly known) converts rotary or linear motion into a change of resistance that can be read by an Arduino analog pin.

But which value (or K) should you use in your project?

Let’s find out if the Potentiometer Values have an impact when read from an Arduino.

SCHEMATIC
                                   
PARTS USED
1.

                                                                UNO R3

2.

                                                      Potentiometer Knobs

3.

                                                      Rotary Potentiometer

4.

                                                              Breadboard

5.

                                                 Jumper Wires male to male

CODE


Continue reading

SCHEMATIC
                                  
                                    
Sometimes, the folks in charge just don't know when to shut up! In some situations, it can be helpful to set up two (or more!) Arduino or Genuino boards to share information with each other. In this example, two boards are programmed to communicate with one another in a Master Writer/Slave Receiver configuration via the I2C synchronous serial protocol. Several functions of Arduino's Wire Library are used to accomplish this. Arduino 1, the Master, is programmed to send 6 bytes of data every half second to a uniquely addressed Slave. Once that message is received, it can then be viewed in the Slave board's serial monitor window opened on the USB connected computer running the Arduino Software (IDE).
The I2C protocol involves using two lines to send and receive data: a serial clock pin (SCL) that the Arduino or Genuino Master board pulses at a regular interval, and a serial data pin (SDA) over which data is sent between the two devices. As the clock line changes from low to high (known as the rising edge of the clock pulse), a single bit of information - that will form in sequence the address of a specific device and a a command or data - is transferred from the board to the I2Cdevice over the SDA line. When this information is sent - bit after bit -, the called upon device executes the request and transmits it's data back - if required - to the board over the same line using the clock signal still generated by the Master on SCL as timing. The initial eight bits (i.e. eight clock pulses) from the Master to Slaves contain the address of the device the Master wants data from. The bits after contain the memory address on the Slave that the Master wants to read data from or write data to, and the data to be written, if any.


Each Slave device has to have its own unique address and both master and slave devices need to take turns communicating over a the same data line line. In this way, it's possible for your Arduino or Genuino boards to communicate with many device or other boards using just two pins of your microcontroller, using each device's unique address.
Connect pin 5 (the clock, or SCL, pin) and pin 4 (the data, or SDA, pin) on the master Arduino to their counterparts on the slave board. Make sure that both boards share a common ground. In order to enable serial communication, the slave Arduino must be connected to your computer via USB.
If powering the boards independently is an issue, connect the 5V output of the Master to the VIN pin on the slave.
PARTS USED
1.
                                                                    2 Arduino
2.
    m-m Wire
CODE

Code for master

// Wire Master Writer


#include <Wire.h>

void setup() {
  Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop() {
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte
  Wire.endTransmission();    // stop transmitting

  x++;
  delay(500);
}

Code for slave

// Wire Slave Receiver


#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

YOU CAN SEE THE DESCRIPTION
Wire.Begin
Wire.Begin.Transmission
Wire.End.Transmission
Wire.Send
Wire.On.Receive
Wire.Available

Continue reading

Thursday, October 27, 2016

SCHEMATIC
                                    
PARTS USED
1.
                                                                    Arduino UNO
2.
                                                                       m-m wire
3.
                                                                       Resistors
4.
                                                                          LED
5.
                                                                            Buzzer
6.
                                                                     Bread Board
CODE
int j;
int i;
void setup() {
  // put your setup code here, to run once:
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(4,OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  for(i=0; i<=30; i++)
  {
   
    digitalWrite(5,HIGH); //pinmode 5 high
    if(i>=10 && i<20)  //if statement for pin mode 5
    {
      if(i==10)
      {
        digitalWrite(4,HIGH);
        delay(1000);
        digitalWrite(4,LOW);
      }
      delay(3000);  //delay 3 seconds
      digitalWrite(5,LOW);  //pinmode 5 low
    }
    else if(i>=20 && i<=30)
    {
      if(i==20)
      {
        digitalWrite(4,HIGH);
        delay(1000);
        digitalWrite(4,LOW);
      }
      delay(3000);
    }
    else
    {
      if(i==0)
      {
        digitalWrite(4,HIGH);
        delay(1000);
        digitalWrite(4,LOW);
      }
      delay(100);
      digitalWrite(5,LOW);
    }
    digitalWrite(6,HIGH); //pinmode 6 high
    if(i>=10 && i<20)  //if statement for pinmode 6
    {
      if(i==10)
      {
        digitalWrite(4,HIGH);
        delay(1000);
        digitalWrite(4,LOW);
      }
      delay(3000);  //delay 3 seconds
      digitalWrite(6,LOW);  //pinmode 6 low
    }
    else if(i>=20 && i<=30)
    {
      if(i==20)
      {
        digitalWrite(4,HIGH);
        delay(1000);
        digitalWrite(4,LOW);
      }
      delay(3000);
     
    }
    else
    {
      if(i==0)
      {
        digitalWrite(4,HIGH);
        delay(1000);
        digitalWrite(4,LOW);
      }
      delay(100);
      digitalWrite(6,LOW);
    }
    digitalWrite(7,HIGH); //pinmode 7 high
    if(i>=10 && i<20)  //if statement for pinmode 7
    {
      if(i==10)
      {
        digitalWrite(4,HIGH);
        delay(1000);
        digitalWrite(4,LOW);
      }
      delay(3000);  //delay 3 seconds
      digitalWrite(7,LOW);  //pinmode 7 low
    }
    else if(i>=20 && i<=30)
    {
      j=0;
      if(i==20)
      {
        digitalWrite(4,HIGH);
        delay(1000);
        digitalWrite(4,LOW);
      }
      delay(3000);
     
    }
    else
    {
      if(i==0)
      {
        digitalWrite(4,HIGH);
        delay(1000);
        digitalWrite(4,LOW);
      }
      delay(100);
      digitalWrite(7,LOW);
    }
     if (j==0) //if statement for shutdown
  {
    digitalWrite(5,LOW);
    digitalWrite(7,LOW);
    digitalWrite(6,LOW);
    delay(1000);
    j=1;
  }
  }

 

}
Continue reading

SCHEMATIC
                                    
PARTS USED
1.
                                                                    Arduino UNO
2.
                                                                       m-m wire
3.
                                                                       Resistors
4.
                                                                          LED
5.
                                                                            Buzzer
6.
                                                                     Bread Board


CODE
void setup() {
  // put your setup code here, to run once:
  pinMode(4,OUTPUT);  //buzzer
  pinMode(5,OUTPUT);  //hour led
  pinMode(6,OUTPUT);  //minute led
  pinMode(7,OUTPUT);  //second led

}

void loop() {
  // put your main code here, to run repeatedly:
  int hour, minute, second, two, i=0; //initializtion
  int imcount=0, ihcount=0, ibcount=0;  //initialization
  for(two=0;two<2;two++)   //for statement for 12 hour time
  {
    for(hour=0;hour<12;hour++)  //for statement for hour hand
  {
    ibcount++;  //buzzer count increment
    for(minute=0;minute<60;minute++)  //for statement for minute hand
    {
      ihcount++;  //hour count increment
      for(second=0;second<60;second++)  //for statement for second hand
      {
        imcount++;  //minute count increment
        digitalWrite(7,HIGH); //pinmode 7 high
        if(imcount==59)
        {
          digitalWrite(6,HIGH); //pinmode 6 high
        }
        if(ihcount==59)
        {
          digitalWrite(5,HIGH); //pinmode 5 high
          digitalWrite(4,HIGH); //pinmode 4 high
         
        }
        delay(500); //delay 0.5 seconds
        digitalWrite(7,LOW);  //pinmode 7 low
        if(imcount==59)
        {
          digitalWrite(6,LOW);  //pinmode 6 low
        }
        if(ihcount==59)
        {
          digitalWrite(5,LOW);  //pinmode 5 low
          digitalWrite(4,LOW);  //pinmode 4 low
          i++;  //i increment
          if(i==ibcount)
          {
            ihcount=0;
          }
        }
        delay(500);
       
      }
      imcount=0;
     
    }
   
  }
  ibcount=0;
  }

}

Continue reading

Wednesday, October 26, 2016

OVERVIEW

Sometimes while building a project, you run out of inputs on your Arduino to connect multiple switches.

For example the DigiSpark is great for small enclosures, but it doesn’t offer much in ways of inputs…

By using a simple Shift Register like the CD4021, you can add up to 8 additional inputs, using only 3 pins on your Arduino.

You can even connect multiple of these Shift Register together to have more inputs if needed while still only using 3 pins.

In this tutorial we will use a DigiSpark with a CD4021 Shift Register and read 4 tact switches.

Depending on the switch pressed, the DigiSpark will send some keyboard strokes back to the PC.

Making a sort of Button Box, where we can assign each tact switches keyboard strokes combination.

SCHEMATIC
                       0
Here are the connection needed for this tutorial:

The GND and 5V (out) of the DigiSpark is connected to the breadboard power rails.
Pin 0-1-2 of the DigiSpark are connected to pin 3-10-9 of the CD4021.

One leg of the Tact Switches is connected to the 5V rail.
The other leg of the Tact Switches are connected to pin 4-5-6-7 of the CD4021.
Pins 4-5-6-7 also have some 10K resistors connected to ground for switch pullups.
Pin 8 and 16 of the CD4021 are connected to GND and 5V.

                                        
PARTS USED
1.

                                                          DigiSpark ATtiny85

2.

                                                         Full size Breadboard

3.

                                                Jumper Wires male to female

4.

                                                     Jumper Wires male to male

5.

                                                       Resistors 1/4w – 5pcs

6.

                                                   Tact switch | Push button

7.

                                         Tact switch | Push button Red – 5pcs

CODE
#include "DigiKeyboard.h"  // Include Library for Keyboard Emulation
//Define pins
int dataPin = 0;   // Pin 0 of DigiSpark connected to Pin 3 of CD4021
int clockPin = 1;  // Pin 1 of DigiSpark connected to Pin 10 of CD4021
int latchPin = 2;  // Pin 2 of DigiSpark connected to Pin 9 of CD4021
//Define variable
byte RegisterValue = 0;  // Used to hold data from DC4021
void setup() {
//define pins used to connect to the CD4021 Shift Register
  pinMode(dataPin, INPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
}
void loop() {
  //Set latch pin to 1 to get recent data into the CD4021
  digitalWrite(latchPin,1);
  
  delayMicroseconds(20);
  
  //Set latch pin to 0 to get data from the CD4021
  digitalWrite(latchPin,0);
  //Get CD4021 register data in byte variable
  RegisterValue = shiftIn(dataPin, clockPin, MSBFIRST);
  if (RegisterValue == B1000) {
    DigiKeyboard.print("Button 1 pressed  -->");
    DigiKeyboard.println(RegisterValue, BIN);
  }
  if (RegisterValue == B100) {
    DigiKeyboard.print("Button 2 pressed  -->");
    DigiKeyboard.println(RegisterValue, BIN);
  }
  if (RegisterValue == B10) {
    DigiKeyboard.print("Button 3 pressed  -->");
    DigiKeyboard.println(RegisterValue, BIN);
  }
  if (RegisterValue == B10000) {
    DigiKeyboard.print("Button 4 pressed  -->");
    DigiKeyboard.println(RegisterValue, BIN);
  }
delay(250);
}
IMPORTANT FILE TO DOWNLOAD
keyboard
Continue reading