Reading 5 pushbuttons using 1 analog pin with further RF transmission of status

Hi guys,
Novice but hopeful enthusiast here. Im hoping you can assist in a little project I’m knocking together. It involves two arduinos, one connected to 5 pushbuttons, the other with an LCD screen to display that the buttons have been pressed, wirelessly connected using a 433Mhz transmitter/receiver pair.

To avoid the use of 5 digital I/O pins, the pushbuttons are all connected to a single analog pin, with an equal value resistor between each of them going back to GND, and using the inbuilt analog to digital (ADC) converter and ten bit resolution, a numerical value between 0 and 1023 which relates to the analog voltage being read of between 0 and 5 volts DC creates an “addressing” system to identify which pushbutton is pressed. The code uses analogRead() Therefore at certain resistance values, analogRead() will return certain numerical values. The sketch looks at a range of values when reading the analog pin, and uses a function to read the buttons and return the button number for the sketch to act upon. this all works very well when it is 5 pbs and 1 arduino, but I really want to make it wireless (2 arduinos) using a 433Mhz transmitter/receiver pair. The question is how to integrate the code for RF into the below code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int a=0;

void setup()
{
lcd.begin(20, 4);
pinMode(A5, INPUT_PULLUP); // sets analog pin for input

}

int readButtons(int pin)
// returns the button number pressed, or zero for none pressed
// int pin is the analog pin number to read
{
int b,c = 0;
c=analogRead(pin); // get the analog value
if (c>1000)
{
b=0; // buttons have not been pressed
}
else
if (c<440 && c>470)
{
b=1; // button 1 pressed
}
else
if (c<400 && c>370)
{
b=2; // button 2 pressed
}
else
if (c>280 && c<310)
{
b=3; // button 3 pressed
}
else
if (c>150 && c<180)
{
b=4; // button 4 pressed
}
else
if (c<20)
{
b=5; // button 5 pressed
}
return b;
}

void loop()
{
a=readButtons(5);
lcd.clear();
if (a==0) // no buttons pressed
{
lcd.setCursor(0,1);
lcd.print(“Press a button”);
}
else
if (a>0) // A button has been pressed
{
lcd.setCursor(0,2);
lcd.print("Pressed button ");
lcd.print(a);
}
delay(1000); // gives the user time to read the LCD
}

Welcome to the Forum.

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Rather than thinking in terms of “integrating” the code for RF into the above code you should think of adding on a function that uses the value produced by the keys and sends it via wireless.

To start with, just write a separate short program that sends a fixed value using wireless. When you have that working you can then add that code to your “button” code. Keep things as simple as possible while you are developing code and learning.

I don’t have any personal experience of the 433MHz wireless devices.

…R

Thanks Robin 2,
That's the thing. I'm kind of stumped in what to do next. I have managed to send text through the serial monitor from my arduino (pc connected) via RF which is then received and displayed on the remote second arduino. This is based on code found on the subject. However, sending analog values or even digital values is harder than I thought, with intentions to display them on a remote LCD. Thanks for your suggestions, but is there anything more you could add? Cheers

geraldine:
Thanks Robin 2,
That's the thing. I'm kind of stumped in what to do next. I have managed to send text through the serial monitor from my arduino (pc connected) via RF which is then received and displayed on the remote second arduino. This is based on code found on the subject. However, sending analog values or even digital values is harder than I thought, with intentions to display them on a remote LCD. Thanks for your suggestions, but is there anything more you could add? Cheers

You should edit your post and add code tags, as has been requested.
Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question ...

Hi,
Apologies. Please find the example code below:

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int a=0;

void setup()
{
 lcd.begin(20, 4);
 pinMode(A5, INPUT_PULLUP); // sets analog pin for input 

} 

int readButtons(int pin)
// returns the button number pressed, or zero for none pressed 
// int pin is the analog pin number to read 
{
 int b,c = 0;
 c=analogRead(pin); // get the analog value  
 if (c>1000)
 {
   b=0; // buttons have not been pressed
 }   
else
 if (c<440 && c>470)
 {
   b=1; // button 1 pressed
 }     
 else
   if (c<400 && c>370)
   {
     b=2; // button 2 pressed
   }       
   else
     if (c>280 && c<310)
     {
       b=3; // button 3 pressed
     }         
     else
       if (c>150 && c<180)
       {
         b=4; // button 4 pressed
       }           
       else
         if (c<20)
         {
           b=5; // button 5 pressed
         }
return b;
}

void loop()
{
 a=readButtons(5);
 lcd.clear();
 if (a==0) // no buttons pressed
 {
   lcd.setCursor(0,1);
   lcd.print("Press a button");
 }   
 else
   if (a>0) // A button has been pressed
   {
     lcd.setCursor(0,2);
     lcd.print("Pressed button ");
     lcd.print(a);
   }
 delay(1000); // gives the user time to read the LCD
}

Ok, now get the RadioHead library and spend some time looking at it.

Hi,
I will, thanks. I was using virtual wire with Arduino 1.0.6 as it did not seem to work as well with the latest version. I will try radio head moving forward? Cheers

geraldine:
I’m kind of stumped in what to do next.

However, sending analog values or even digital values is harder than I thought,

You have not given us any ideas about what, specifically, you are having a problem with.

Maybe the pair of programs in this link would be helpful.

…R

Hi again,
Specifically I am unsure of how to code for transmission of the analog values determined by the pushbutton switching over RF, which is to be displayed on the second Arduino's LCD. So really it is perhaps a simplistic issue, but I am brainstorming at the novice level I'm at in sending analog value bands (0-1023) one at a time. Thanks for the help

geraldine:
Specifically I am unsure of how to code for transmission of the analog values determined by the pushbutton switching over RF,

You say you have a program that can send text using the nRF24. That probably sends the text as an array of characters. You can just put the data from the analogRead() into the array.

Post the programs that you have for sending and receiving text and it will probably be easier to explain.

Do you want to send the values from analogRead() or do you want to send the ID of the key that was pressed - which could be just a single character?

…R