Write My Paper Button

WhatsApp Widget

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

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’

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’

Don`t copy text!
WeCreativez WhatsApp Support
Our customer support team is here to answer your questions. Ask us anything!
???? Hi, how can I help?