bcd input to lcd

Hi guys, new to all this and have been playing with code to try and get my project running but struggling somewhat! Here is what I need if anyone can help.

Hardware: arduino uno. 16x2 lcd. I have from the truth table 9 possible inputs.

What I want to happen is for binary:
0000 to display "No radio input"
1000 to display "160m"
0100 to display "80m"....etc

So far I have figured how to upload some code I got from the net and kinda dissected it a little. Basic stuff to you guys. Sorta figured how to assign pins to the LCD and put my name on it.
I found some code that almost does what I want but cant figure it out.

Some help tuning it would be great. If I see in any code you can provide withjust one combination of input say 0001 and it displays "160M" then I think I can copy co creat all 9
This is the code I have been playing with...

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte bcdInputA0 = 7; // 10k pull down resistor
const byte bcdInputA1 = 8; // 10k pull down resistor
const byte bcdInputA2 = 9; // 10k pull down resistor
const byte bcdInputA3 = 10; // 10k pull down resistor
int solenoid = 6; // output to mosfet
static int Sequence = 0;
int A0state = 0;
int A1state = 0;
int A2state = 0;
int A3state = 0;

void setup() 
{
 // put your setup code here, to run once:
    pinMode(bcdInputA0, INPUT);
    pinMode(bcdInputA1, INPUT);
    pinMode(bcdInputA2, INPUT);
    pinMode(bcdInputA3, INPUT);
    pinMode(solenoid, OUTPUT);
    lcd.begin(16, 2);
    lcd.print("Hello");
    lcd.setCursor(0, 1);
    lcd.print("select sequence");
} // end of void setup

void loop()

{
 // put your main code here, to run repeatedly:

    A0state = digitalRead(bcdInputA0); // LSB
    A1state = digitalRead(bcdInputA1);
    A2state = digitalRead(bcdInputA2);
    A3state = digitalRead(bcdInputA3); // MSB
 // lots more code here!!!
 
 // e.g. if A0 =1, A1=0, A2=1, A3=0, that is 0101 (5) in binary
 // that will select switch case 5 and so on...
 // only codes available from external circuit are
 // 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001
 


  switch (Sequence)
  {
     case 0: 
     // code to display sequence 1 on lcd
     lcd.print("sequence 1");
     digitalWrite(solenoid, LOW);
     break;
  
     case 1: 
     // code to display sequence 2 on lcd
     lcd.print("sequence 2");
     digitalWrite(solenoid, LOW);
     break;
     
     case 2: 
     // code to display sequence 3 on lcd
     lcd.print("sequence 3");
     digitalWrite(solenoid, HIGH);
     break;
     
     case 3: 
     // code to display sequence 4 on lcd
     lcd.print("sequence 4");
     digitalWrite(solenoid, LOW);
     break;
     
     case 4: 
     // code to display sequence 5 on lcd
     lcd.print("sequence 5");
     digitalWrite(solenoid, HIGH);
     break;
// etc etc

  } // end of case

} //  end of void loop

Also where does the ground from my input device connect? Next to i/o13 maybe?
Many thanks in advance

A suggestion. Read the 4 inputs and use their values to set the 4 low bits of a byte variable using the bitSet() function using a for loop. This will give you a byte with 16 possible values with which you can use switch/case.

Please edit you post and add code-tags like explained in the How to use this forum post.

And indeed, UKHeliBob is right. And use arrays to keep it easy:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//Let's use a array
cont byte bcdInputs[] = {7, 8, 9, 10};
/*
const byte bcdInputA0 = 7; // 10k pull down resistor
const byte bcdInputA1 = 8; // 10k pull down resistor
const byte bcdInputA2 = 9; // 10k pull down resistor
const byte bcdInputA3 = 10; // 10k pull down resistor
*/
//no idea about the mosfet...
//int solenoid = 6; // output to mosfet

//don't need it
/*
static int Sequence = 0;
int A0state = 0;
int A1state = 0;
int A2state = 0;
int A3state = 0;
*/

void setup()
{
  // put your setup code here, to run once:
  /*
     pinMode(bcdInputA0, INPUT);
     pinMode(bcdInputA1, INPUT);
     pinMode(bcdInputA2, INPUT);
     pinMode(bcdInputA3, INPUT);
     */
     for(byte i = 0; i < sizeof(bcdInputs); i++){
       pinMode(bcdInputs[i], INPUT);
     }
     pinMode(solenoid, OUTPUT);
     lcd.begin(16, 2);
     lcd.print("Hello");
     lcd.setCursor(0, 1);
     lcd.print("select sequence");
} // end of void setup

void loop()

{
  // put your main code here, to run repeatedly:
    
    //again in array
    
    byte sequence = 0;
    for(byte i = 0; i < sizeof(bcdInputs); i++){
      //shift left by 10
      sequence <<= 1;
      
      //add the next bcd input, starting at the last counting down
      sequence |= digitalRead(bcdInputs[sizeof(bcdInputs) - i]);
    }
    
    lcd.setCursor(0,0);//whereever you want
    
    if(sequence == 0){
      lcd.print("No radio input");
    }
    else{
      lcd.print(sequence * 20);
      lcd.print("m");
    }

} //  end of void loop

Thanks both, sorry for bad post will try and figure how to edit. the code you sent shows an error..

4th line 'cont' does not name a type

???

Of course "cont" needs to be "const". Not that hard to think of, is it? Maybe there are some more errors but it gives you an idea.

Sorted thanks. getting somewhere now. Still cant find how to edit or put code in right place on here. Will sort tho

Thanks again all

Still cant find how to edit or put code in right place on here.

Auto Format the code in the IDE, select all of the code and copy it.
In the forum editor paste the code and add [­code] before it and [­/code] after it or select the code and click the code tags icon (</>) top/left of the forum editor window or do that first and past the code between the code tags that have been inserted.

Ok sorted the code position I think. Cant get the display to work right tho.

Counting up from 1-9 in binary (1000 - 1001) needs to display 160, 80, 40, 30, 20, 17, 15, 12, 10) if no input then I assume it will read 0000 which is good as it can display "No radio attached"

1000 gives me 160 thats ok.. 0100 gives 180 - if that dropped the '1' then its ok. After that all is wrong.
1100=180
0010=200
1010=200
0110=220
1110=220
0001=240
1001=240

Ok sorted the code position I think. Cant get the display to work right tho.

How about posting the code ?

septillions code posted previous

ta

davedpg:
Counting up from 1-9 in binary (1000 - 1001)

That's not counting in binary... 1 = 0b0001, not 0b1000. You have MSB and LSB mixed up :wink:

davedpg:
needs to display 160, 80, 40, 30, 20, 17, 15, 12, 10)

That's the first time you post all the distances. From the first post I had to come up with a scale and it kind of looked linear so went with that. If it's not then just add a lookup table.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//Let's use a array
cont byte bcdInputs[] = {7, 8, 9, 10};

//and a array as a lookup table for the distances
const byte distancesLookup[] = {160, 8-, 40, 30, 20, 17, 15, 12, 10};

/*
const byte bcdInputA0 = 7; // 10k pull down resistor
const byte bcdInputA1 = 8; // 10k pull down resistor
const byte bcdInputA2 = 9; // 10k pull down resistor
const byte bcdInputA3 = 10; // 10k pull down resistor
*/
//no idea about the mosfet...
//int solenoid = 6; // output to mosfet

//don't need it
/*
static int Sequence = 0;
int A0state = 0;
int A1state = 0;
int A2state = 0;
int A3state = 0;
*/

void setup()
{
  // put your setup code here, to run once:
  /*
     pinMode(bcdInputA0, INPUT);
     pinMode(bcdInputA1, INPUT);
     pinMode(bcdInputA2, INPUT);
     pinMode(bcdInputA3, INPUT);
     */
     for(byte i = 0; i < sizeof(bcdInputs); i++){
       pinMode(bcdInputs[i], INPUT);
     }
     pinMode(solenoid, OUTPUT);
     lcd.begin(16, 2);
     lcd.print("Hello");
     lcd.setCursor(0, 1);
     lcd.print("select sequence");
} // end of void setup

void loop()

{
  // put your main code here, to run repeatedly:
   
    //again in array
   
    byte sequence = 0;
    for(byte i = 0; i < sizeof(bcdInputs); i++){
      //shift left by 10
      sequence <<= 1;
     
      //add the next bcd input, starting at the last counting down
      sequence |= digitalRead(bcdInputs[sizeof(bcdInputs) - i]);
    }
   
    lcd.setCursor(0,0);//whereever you want
   
    if(sequence == 0){
      lcd.print("No radio input");
    }
    else if(sequence < sizeof(distancesLookup + 1)){
      lcd.print(distancesLookup[sequence - 1]);
      lcd.print("m");
    }
    else{
      lcd.print("Invallid input");
    }

} //  end of void loop

It should have just one more feature - the LCD should not be written every time through loop(). It can make flickering on the display. The input should be stored and new input should be compared with it. Only if it has changed, should the display be updated.

You are totally right. That's a nice assignment for the TS.

septillion:
You are totally right. That's a nice assignment for the TS.

Ha ha, exactly what I thought after I posted. :slight_smile:

Thanks guys, appreciate the help, I must point out again however , this is only day two of looking/ writing and trying to understand code. So with reference to "Just add a lookup table" sorry thats beyond me. Kinda like me asking my Mum to build a super het transceiver out of a CRT tv

davedpg:
sorry thats beyond me.

Luckily I did that for you. And it's not that hard, just have a look :wink: