reading CD4021BE shift in register

Hi guys;

How does the arduino read signals from a CD4021BE shift register?

is it the same as the 74HC595 output shift register, i.e. 1, 2, 4, 16, 32, 64, 128, 255?

cheers Guys,
Jon

Yes.

it doesnt seem to be working
my code is as follows

void readButtons()
{
digitalWrite(latchPin, HIGH);
delayMicroseconds(20);
digitalWrite(latchPin, LOW);

shiftIn(dataPin, clockPin, MSBFIRST);
}

void loop()
{
readButtons();
{
whichSwitch = digitalRead(dataPin);
if (whichSwitch == 256) //B00000001
{
lcd.setCursor(0,1);
lcd.print("down button");
}
}
}
}

I'm not surprised it doesn't work, it doesn't even compile.

And an 8 bit value can never be 256.

it is just a snipit of the whole code for ease of use.

ok so for the numbers what does the equivalent to the binary below:

B00000001
B00000010
B00000100
B00001000
B00010000
B00100000
B01000000
B10000000

how do i get it to work then

Ok, then, here’s a snippet of an answer. A URL. I won’t tell you how to interpret it, but it’s there for “ease of use”…

facetious much!!!!

ive just told you that

1 = 00000001
2 = 00000010
4 = 00000100
16 = 00001000
32 = 00010000
64 = 00100000
128 = 01000000
255 = 10000000

so how do i manipulate what data the shift register is sending in the code

is it the digitalRead?

can that function read only HIGH and LOW signals not bin or hex etc, if so how do i do it,

please help me

Junblud:
facetious much!!!!

ive just told you that

1 = 00000001
2 = 00000010
4 = 00000100
16 = 00001000
32 = 00010000
64 = 00100000
128 = 01000000
255 = 10000000

so how do i manipulate what data the shift register is sending in the code

No, those numbers are wrong. Very wrong.

your loads of help!

Junblud:
your loads of help!

And so are you. We can't help you unless you help us. Try posting ALL your code, then we can look at the code and tell you why it isn't working and help you get it working right.

If you need spoon feeding, I'll give you a clue. Here's the CORRECT binary translations:

00000001 = 1
00000010 = 2
00000100 = 4
00001000 = 8
00010000 = 16
00100000 = 32
01000000 = 64
10000000 = 128

ok, sorry i was just trying to make things easier for you.

my whole code so far is below
#include <Wire.h>
#include “RTClib.h”
RTC_DS1307 RTC;
#include <TimeAlarms.h>
#include <Time.h>
#include <LiquidCrystal.h>
#include <SPI.h>

int backLight = 10;
int latchPin = 8;
int dataPin = 9;
int clockPin = 7;

byte switchVar1 = 72; //01001000

const int numRows = 4;
const int numCols = 20;
const byte Symbol_1 = B10110000;
const byte Symbol_2 = B01111110;

LiquidCrystal lcd(12,11,5,4,3,2);

void setup()
{
pinMode (backLight, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);

//RTC boot up
Wire.begin();
RTC.begin();

//lcd boot up screen
digitalWrite(backLight, HIGH);
lcd.begin(numCols,numRows);
lcd.setCursor(6,0);
lcd.print(“WELCOME”);
lcd.setCursor(1,1);
lcd.print(“Dosing Pump Module”);
lcd.setCursor(5,2);
lcd.print(“By JUNBLUD”);
lcd.setCursor(4,3);
lcd.print(“Loading:”);
for (int i = 0; i <= 100; i++)
{
lcd.setCursor(12,3);
if (i<100) lcd.print(" “);
if (i<10) lcd.print(” “);
lcd.print(i);
lcd.print(”%");
delay(100);
}
delay(2000);
lcd.clear();
}

void mainMenu()
{
lcd.setCursor(0,0);
lcd.print(“Main Menu”);
lcd.setCursor(0,1);
lcd.write(Symbol_1);
lcd.setCursor(2,1);
lcd.print(“Pump 1”);
lcd.setCursor(0,2);
lcd.write(Symbol_1);
lcd.setCursor(2,2);
lcd.print(“Pump 2”);
lcd.setCursor(0,3);
lcd.write(Symbol_1);
lcd.setCursor(2,3);
lcd.print(“Set Schedule”);
lcd.setCursor(10,1);
lcd.write(Symbol_1);
lcd.setCursor(12,1);
lcd.print(“Pump 3”);
{
DateTime now = RTC.now();
lcd.setCursor(12,0);
lcd.print(now.hour(), DEC);
lcd.print(’:’);
lcd.print(now.minute(), DEC);
lcd.print(’:’);
lcd.print(now.second(), DEC);
delay(1000);
}
}

void pumpStatus(const char *pumpStatusNum)
{
lcd.setCursor(0,0);
lcd.print(pumpStatusNum);
lcd.setCursor(0,1);
lcd.write(Symbol_1);
lcd.setCursor(2,1);
lcd.print(“First Dosage”);
lcd.setCursor(0,2);
lcd.write(Symbol_1);
lcd.setCursor(2,2);
lcd.print(“Second Dosage”);
}

void setScheduleMenu()
{
lcd.setCursor(0,0);
lcd.print(“Set Schedule”);
lcd.setCursor(0,1);
lcd.write(Symbol_1);
lcd.setCursor(2,1);
lcd.print(“Adjust Pump 1”);
lcd.setCursor(0,2);
lcd.write(Symbol_1);
lcd.setCursor(2,2);
lcd.print(“Adjust Pump 2”);
lcd.setCursor(0,3);
lcd.write(Symbol_1);
lcd.setCursor(2,3);
lcd.print(“Adjust Pump 3”);
}

void adjustPump(const char *adjustPumpNum)
{
lcd.setCursor(0,0);
lcd.print(adjustPumpNum);
lcd.setCursor(0,1);
lcd.write(Symbol_1);
lcd.setCursor(2,1);
lcd.print(“Everyday at”);
lcd.setCursor(0,2);
lcd.write(Symbol_1);
lcd.setCursor(2,2);
lcd.print(“everyday at”);
lcd.setCursor(0,3);
lcd.write(Symbol_1);
lcd.setCursor(2,3);
lcd.print(“Amount 000ml”);
}

void options()
{
lcd.setCursor(0,0);
lcd.print(“Options”);
lcd.setCursor(0,1);
lcd.write(Symbol_1);
lcd.setCursor(2,1);
lcd.print(“Pump Flush”);
lcd.setCursor(0,2);
lcd.write(Symbol_1);
lcd.setCursor(2,2);
lcd.print(“Schedule Reminder”);
}

void pumpFlush()
{
lcd.setCursor(0,0);
lcd.print(“Pump Flush”);
lcd.setCursor(0,1);
lcd.write(Symbol_1);
lcd.setCursor(2,1);
lcd.print(“Flush Pump 1”);
lcd.setCursor(0,2);
lcd.write(Symbol_1);
lcd.setCursor(2,2);
lcd.print(“Flush Pump 2”);
lcd.setCursor(0,3);
lcd.write(Symbol_1);
lcd.setCursor(2,3);
lcd.print(“Flush Pump 3”);
}

void scheduleReminder()
{
lcd.setCursor(0,0);
lcd.print(“fist line goes here”);
lcd.setCursor(0,1);
lcd.write(Symbol_1);
lcd.setCursor(2,1);
lcd.print(“second line goes here”);
lcd.setCursor(0,2);
lcd.write(Symbol_1);
lcd.setCursor(2,2);
lcd.print(“third line goes here”);
lcd.setCursor(0,3);
lcd.write(Symbol_1);
lcd.setCursor(2,3);
lcd.print(“forth line goes here”);
}

void readButtons()
{
digitalWrite(latchPin, HIGH);
delayMicroseconds(20);
digitalWrite(latchPin, LOW);

switchVar1 = shiftIn(dataPin, clockPin, MSBFIRST);
}
void loop()
{
mainMenu();
{
for (int n=0; n<=7; n++)
{
if (dataPin = 2)
{
lcd.setCursor(0,1);
lcd.print(Symbol_2);
}
}
}
}
[/table]

i am attempting to make an lcd interface where i can scroll through menus.

I am trying but failing miserably at getting the Lcd to display B01111110(Symbol_2) rather than B10110000(Symbol_1) when certain buttons are pressed. I cannot code the arduino to read the input shift register

void loop()
{
  mainMenu();
  {
      for (int n=0; n<=7; n++)
  {
    if (dataPin = 2)
    {
      lcd.setCursor(0,1);
      lcd.print(Symbol_2);
    }
  }
  }
}

What do you think this bit is supposed to be doing?

setting the cursor and printing to the lcd if the value of dataPin(shift register serial input) = 2 (B00000010)

Well, that’s not what it’s doing.

  • for (int n=0; n<=7; n++)
    Do this 8 times:

  • if (dataPin = 2)
    Did I set the variable dataPin to the value 2? Of course I did.

  • lcd.setCursor(0,1);

  • lcd.print(Symbol_2);
    Set the cursor to 0,1 and print Symbol_2

dataPin is a variable used (by the looks of things) to define which pin is used as the serial data input for the shiftIn() function. You are then assigning it the value “2” and at the same time checking if you assigned it a value of anything other than 0 (btw, = is assign, == is compare). I don’t think you want to be ever touching dataPin (you should set it as a const so the compiler will complain if you do something silly like that), but you want to be looking at the results of the shiftIn() call.

hmm ok so…

const int latchPin = 8;
const int dataPin = 9;
const int clockPin = 7;

void readButtons()
{
digitalWrite(latchPin, HIGH);
delayMicroseconds(20);
digitalWrite(latchPin, LOW);

switchVar1 = shiftIn(dataPin, clockPin, MSBFIRST);
}

void loop()
{
mainMenu();
{
for (int n=0; n<=8; n++)
{
if (switchVar1 == 2)
{
lcd.setCursor(0,1);
lcd.print(Symbol_2);
}
}
}
}
I don’t understand whats going on here

  mainMenu();
  {

What's with the extra level of { ... } ? Not that it makes any functional difference, but it's just confusing to read.

I don't understand whats going on here

What is going on here? What are the results you are currently seeing?

so i know the ShiftIn function are happening within the main menu.

it doesnt do anything

I would suggest writing a much simpler program to start with. Begin with the basics: shift in a value from the shift register and display the value on the LCD. When you know you have that working you can expand it out to react to different buttons.

the below code works and is printing to the Lcd. however, the output is very odd, not binary code or equivalent numbers.

this is what the shift register is printing to the lcd screen:
switch 1 prints - 100000107
switch 2 prints - 1000000115
switch 3 prints - 10000001116
switch 4 prints - 10116
switch 5 prints - 100112
switch 6 prints - 1000110
switch 7 prints - 10000107

#include <LiquidCrystal.h>
#include <SPI.h>

LiquidCrystal lcd(12,11,5,4,3,2);
int backLight = 10;
int latchPin = 8;
int dataPin = 9;
int clockPin = 7;

byte switchVar1 = 72; //01001000
byte whichSwitch = {‘left’, ‘right’, ‘up’, ‘down’, ‘ok’, ‘back’, ‘options’};

const int numRows = 4;
const int numCols = 20;

void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
pinMode(backLight, OUTPUT);

lcd.begin(numCols, numRows);
}

void loop()
{
digitalWrite(backLight, HIGH);

digitalWrite(latchPin, HIGH);
delayMicroseconds(20);
digitalWrite(latchPin, LOW);

switchVar1 = shiftIn(dataPin, clockPin, MSBFIRST);

lcd.print(switchVar1, BIN);

for (int n=0; n<=7; n++)
{
if (switchVar1 & (1 << n) )
{
lcd.print(whichSwitch[n]);
}
}
delay(500);
}