Binary to Decimal Conversion (Arduino Project)

preview_player
Показать описание
Binary to Decimal Conversion (Arduino Project)
Links:
Рекомендации по теме
Комментарии
Автор

/* Free Sample Code

This sketch converts an 8-Bit Binary number into a Decimal number.
The Binary number is fed to the Arduino through an 8x DIP Switch.
A function then converts this Binary number to its Decimal
equivalent. These numbers are displayed on an OLED Display and Serial Monitor.

This program is made by Shreyas for Electronics Champ YouTube Channel.
Please subscribe to this channel. Thank You.

*/

//Include the libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//Initialize the variables
int bitVal;
String stringBit;
String stringBinary;
long binaryNumber;
int decimalNumber;

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT);



void setup() {

Serial.begin(9600);

//Sets pin 3 to pin 10 as input
for (int x = 3; x < 11; x++) {

pinMode(x, INPUT);

}

oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.clearDisplay();




oled.setTextSize(2); // Normal 1:1 pixel scale
// Draw white text
oled.setCursor(24, 16); // Start at top-left corner
oled.println(F("Binary"));

oled.setTextSize(2);
// Draw 'inverse' text
oled.println("Conversion");

oled.setTextSize(0); // Draw 2X-scale text

oled.println(" ");

oled.setTextSize(1); // Draw 2X-scale text

oled.println(" Project");

oled.display();
delay(3000);

oled.clearDisplay();

}

void loop() {

//Reads the Binary number from the DIP Switch
for (int x = 3; x < 11; x++) {

bitVal = digitalRead(x);
stringBit = String(bitVal);
stringBinary = stringBinary + stringBit;
binaryNumber = stringBinary.toInt();

}

//Function to convert Binary to Decimal
decimalNumber =

//Prints the Binary number on the Serial Monitor
Serial.print("Binary: ");
Serial.print(stringBinary);
Serial.print(" ");

//Prints the Decimal number on the Serial Monitor
Serial.print("Decimal: ");


//Prints the Binary number on the OLED Display
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(5, 10);
oled.print("B:");
oled.println(stringBinary);

//Prints the Decimal number on the OLED Display
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(5, 40);
oled.print("D:");
oled.println(decimalNumber);
oled.display();

//Resets all the variables
binaryNumber = 0;
bitVal = 0;
stringBit = "";
stringBinary = "";

}

//Function to convert Binary to Decimal
long convertBinaryToDecimal(long binary) {

long number = binary;
long decimalVal = 0;
long baseVal = 1;
long tempVal = number;
long previousDigit;

while (tempVal) {

//Converts Binary to Decimal
previousDigit = tempVal % 10;
tempVal = tempVal / 10;
decimalVal += previousDigit * baseVal;
baseVal = baseVal * 2;

}

//Returns the Decimal number
return decimalVal;

}

bit
welcome to shbcf.ru