Your right. My son had a crack at it and the code appears to be very simple and should work. But still not sure why it doesn't..
Binary output works and the counter goes up to 16 bits easily. But as simple as the code is the conversion seems to throw a wobbly ..1,2,4,8,16,32,64,128,512,1084? what happened there..?
This is not doing what you think. If stringBinary contains "1011" the .toInt() function interprets that as base 10, not binary - so it is the number 1011 (base 10), not the number 11 (base 10)
You are basically reading pins, building up a binary string and then converting that to a number. Just do it directly.
/*
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>
const float startingFrequency = 1240.000;
const float increment = .025;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT);
void setup() {
Serial.begin(9600);
/*
//Sets pin 2 to pin 13 as input
for (int x = 25; x < 41; x++) {
pinMode(x, INPUT);
*/
// }
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.clearDisplay();
oled.clearDisplay();
}
void loop() {
//Reads the Binary number from the DIP Switch
unsigned int number = 0;
for (int x = 25; x < 41; x++) {
int bitVal = digitalRead(x);
number = number * 2 + bitVal;
}
float frequency = (number - 1) * increment + startingFrequency;
//Prints the Binary number on the Serial Monitor
Serial.print("Binary: ");
Serial.print(number, BIN);
Serial.print(" ");
//Prints the Decimal number on the Serial Monitor
Serial.print("Decimal: ");
Serial.println(number, DEC);
Serial.print(" Frequency: ");
Serial.println(frequency, 3);
//Prints the Binary number on the OLED Display
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(2, 50);
oled.print("RPos:");
oled.println(number, BIN);
//Prints the Decimal number on the OLED Display
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(2, 30);
oled.print(frequency, 3);
oled.setTextSize(1);
// oled.setCursor(2, 30);
oled.println("MHz");
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(2, 20);
oled.setTextSize(1);
oled.println("23cms ATV Band");
oled.display();
}
Just a comment, the title of this thread states "BCD to decimal", but you have been discussing binary to decimal. BCD (binary coded decimal) is a way of storing a decimal number where each decimal digit is represented by a four-bit binary number from 0 through 9. Your 16-bit input would then be four BCD digits, with a range of 0000 through 9999, completely different from a 16-bit binary number which can range from 0000 through 65535.
Oh man. You got the Mega anyway; just hook up the encoder to the Mega and let the Mega do the counting, conversion to BCD as well as show the desired numbers etc. on a suitable display. Why drag all the 1980s electronics into it and then add a fairly powerful uC just to display a number if it can do everything...
That was My choice initially and well I investigated that route..
I am from that era!
Reasoning behind that is the following..
Very little / none programming skills.. I can sit all day playing with code and achieving very little..
The adruino draws too much current! (Correct me if I'm wrong) to leave it on standby using backup battery and loses its data when switched off.. Applying a additional RAM to retain the memory , then more coding etc which I do not have the skills to do..
This method!
Using 90's Logic chips and SMD technology all works and using a backup battery and current on standby is about 0.2uA.. good enough.
PLL is an 80's design, and lots of other options out there (I2C) but sticking with my old legacy hardware and using the new technology we have to provide the interface structure..
I guess I'm sticking with what I know and understand. Son provided lots of support now he has gone to UNI and studying Computing, programming and AI technology I've lost that support..
Your best bet is to work on that. If you can understand the kind of circuit you built, you can also learn to program a microcontroller to a sufficient extent to solve this kind of thing. Sure, there's a learning curve, but you'll notice that your current way of solving this is very convoluted by today's standards. And besides, sounds like you're still running into a programming problem with your current approach, so it seems there's no way around it anyway.
Microcontrollers have various power saving states, the modern ones even more so.
They all have in-built EEPROMS that are likely more than spacious enough for whatever settings you'd want to store, and if not, adding an I2C EEPROM is very simple and cheap.
I know what I'd do in your place...forget about the old stuff and get with the times.
I'm sorry for you, I really am. But consider this - are you going to mourn that loss, or are you going to stand on your own feet once again?