By the end of this chapter, you will have created an LED sequencing display using arrays, operators, and EEPROM storage.

Learning Goal: I’m working on a computer science writing question and need an explanation and answer to help me learn.

This LAB relates to Module 8, Section 4.3. Follow this section and set up your breadboard for some Bitwise operations

Reading

Chapter 4: Data Manipulation and EEPROM – Part 2

4.0 CHAPTER OVERVIEW

In Module 7 you learned about:

  • Using variables within Arrays
  • Using arithmetic, relational, logical, and assignment operators

In this Module you’ll learn about:

  • Manipulating data using bit-wise operators and logic
  • Storing information between resets using EEPROM

By the end of this chapter, you will have created an LED sequencing display using arrays, operators, and EEPROM storage.

(447) Arduino Workshop – Chapter 4 – Overview – YouTubeLinks to an external site.


4.3 BIT MATH

In this section, you’ll be learning about controlling individual bits within bytes using bitwise operators and logic.

(447) Arduino Workshop – Chapter 4 – Bit Math – YouTubeLinks to an external site.

Bitwise Operator Examples

//AND
    x = 0011 0100
    y = 0101 1101
x & y = 0001 0100

//OR
    x = 0011 0100
    y = 0101 1101
x | y = 0111 1101

//XOR
    x = 0011 0100
    y = 0101 1101
x ^ y = 0110 1001

//NOT
 x = 0011 0100
~x = 1100 1011
 y = 0101 1101
~y = 1010 0010

//SHIFT LEFT
     x = 0011 0100
x << 2 = 1101 0000
     y = 0101 1101
y << 2 = 0111 0100

//SHIFT RIGHT
     x = 0011 0100
x >> 2 = 0000 1101
     y = 0101 1101
y >> 2 = 0001 0111

Truth Tables

combined-truth-table.webp

Source Code for ‘AND, OR, XOR Calculator’

const int dataPin = 6;
const int clockPin = 7;
const int latchPin = 8;
byte ledMap = 0b11111111;
int delayTime = 3000;
void setup() {
  // put your setup code here, to run once:
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  
  Serial.begin(9600);
  shiftWrite(0x00);
  Serial.println("Enter a number between 0-255");
}
void loop() {
  if(Serial.available())
  {
    int inputVal = Serial.parseInt();
    
    if(inputVal > 255)
    {
      Serial.println("Uh oh, try again");
      Serial.println("Enter a number between 0-255"); 
      return;
    }
    
    Serial.print("DECIMAL: ");
    Serial.println(inputVal);
    Serial.print("BINARY: ");
    Serial.println(inputVal, BIN);
    Serial.println();
    
    Serial.print("AND result: ");
    Serial.println(ledMap & inputVal, BIN);
    shiftWrite(ledMap & inputVal);
    delay(delayTime);
    
    Serial.print("OR  result: ");
    Serial.println(ledMap | inputVal, BIN);
    shiftWrite(ledMap | inputVal);
    delay(delayTime);
    
    Serial.print("XOR result: ");
    Serial.println(ledMap ^ inputVal, BIN);
    shiftWrite(ledMap ^ inputVal);
    delay(delayTime);
    Serial.println();
    Serial.println("Enter a number between 0-255");
  }  
}
void shiftWrite(byte value)
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, value);
  digitalWrite(latchPin, HIGH);
}

Wiring Diagram for ‘AND, OR, XOR Calculator’

Bit-Math.png


4.4 EEPROM

In this section, we’ll learn about storing data in the non-volatile memory known as EEPROM. This allows data to be retained when power is disconnected and accessed later.

(447) Arduino Workshop – Chapter 4 – Using EEPROM – YouTubeLinks to an external site.

Source Code for ‘EEPROM Counter’

#include <EEPROM.h>

// pin definitions
int ledPin = 13;
int buttonPin = 2;

// global variables
int lastButtonState = 1;
long unsigned int lastPress;
int debounceTime = 20;
int counter;

void setup() {
  // setup pin modes
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

  //initialise Serial port
  Serial.begin(9600);

  //assign counter the value of address 0
  counter = EEPROM.read(0);
  //write a 0 to address 0. This allows for consecutive resets to reset the counter
  EEPROM.write(0,0);
}

void loop() {  
  int buttonState = digitalRead(buttonPin);   //read the state of buttonPin and store it as buttonState (0 or 1)
  
  if((millis() - lastPress) > debounceTime)   //if the time between the last buttonChange is greater than the debounceTime
  {
    lastPress = millis();   //update lastPress                                                     
    if(buttonState == 0 && lastButtonState == 1)    //if button is pressed and was released last change
    {
      counter++;
      EEPROM.write(0, counter); //write counter to address 0
      digitalWrite(ledPin, HIGH); //momentary LED
      lastButtonState = 0;    //record the lastButtonState

      //print the results
      Serial.print("Counter: ");
      Serial.println(counter);
    }
    if(buttonState == 1 && lastButtonState == 0)    //if button is not pressed, and was pressed last change
    {
      lastButtonState = 1;    //record the lastButtonState
      digitalWrite(ledPin, LOW);  //momentary LED
    }
  }
}

Wiring Diagram for ‘EEPROM Counter’

button-1_bb.png

Lab4-.png combined-truth-table-1.webp

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more
error: Content is protected !!