I am trying to read in 8 inputs. Then I will display the status on a LCD. I am going to have 5 nanos hooked to each other. Each one will feed into the previous one, so that I can read in the states of all 40 inputs. The computer will read one bit at at time by pulsing a clock signal. I currently have two hooked up. The one not directly connected to the computer works properly. However the data gets shifted on the closest one to the computer. An extra zero gets added, at the end of each byte.
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
int buf[9];
int Load = A0;
int Clock = A1;
int SER = A2;
int Q7 = A3;
void setup(void) {
/* U8g2 Project: SSD1306 Test Board */
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(10, 0);
digitalWrite(9, 0);
u8g2.begin();
pinMode (5, INPUT);
pinMode (6, INPUT);
pinMode (7, INPUT);
pinMode (8, INPUT);
pinMode (9, INPUT);
pinMode (10, INPUT);
pinMode (11, INPUT);
pinMode (12, INPUT);
pinMode (A0,INPUT);
pinMode (A1,INPUT);
pinMode (A2,INPUT);
pinMode (A3,OUTPUT);
}
void loop(void) {
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB10_tr);
u8g2.drawStr(0,12,"Parallel to Serial");
u8g2.drawGlyph(0,24,-(48)*buf[0]+48);
u8g2.drawGlyph(11,24,-(49)*buf[1]+49);
u8g2.drawGlyph(22,24,-(50)*buf[2]+50);
u8g2.drawGlyph(33,24,-(51)*buf[3]+51);
u8g2.drawGlyph(44,24,-(52)*buf[4]+52);
u8g2.drawGlyph(55,24,-(53)*buf[5]+53);
u8g2.drawGlyph(66,24,-(54)*buf[6]+54);
u8g2.drawGlyph(77,24,-(55)*buf[7]+55);
u8g2.drawGlyph(88,24,-(56)*buf[8]+56);
u8g2.drawGlyph(0,36,-(48)*digitalRead(5)+48);
u8g2.drawGlyph(11,36,-(49)*digitalRead(6)+49);
u8g2.drawGlyph(22,36,-(50)*digitalRead(7)+50);
u8g2.drawGlyph(33,36,-(51)*digitalRead(8)+51);
u8g2.drawGlyph(44,36,-(52)*digitalRead(9)+52);
u8g2.drawGlyph(55,36,-(53)*digitalRead(10)+53);
u8g2.drawGlyph(66,36,-(54)*digitalRead(11)+54);
u8g2.drawGlyph(77,36,-(55)*digitalRead(12)+55);
if (analogRead(SER) > 500)
u8g2.drawGlyph(88,36,0);
else u8g2.drawGlyph(88,36,56);
u8g2.drawStr(0,63,"Out : ");
} while ( u8g2.nextPage() );
//delay(1000);
if (analogRead(Load)<400){
buf[0] = digitalRead(5);
buf[1] = digitalRead(6);
buf[2] = digitalRead(7);
buf[3] = digitalRead(8);
buf[4] = digitalRead(9);
buf[5] = digitalRead(10);
buf[6] = digitalRead(11);
buf[7] = digitalRead(12);
if (analogRead(SER) > 500)
buf[8] = 1;
else buf[8] = 0;
do {
delay(1);}
while ((analogRead(Load)<400)or(analogRead(Clock)>500));
}
if (analogRead(Clock)>500){
analogWrite(Q7, buf[0]*680);
buf[0] = buf[1];
buf[1] = buf[2];
buf[2] = buf[3];
buf[3] = buf[4];
buf[4] = buf[5];
buf[5] = buf[6];
buf[6] = buf[7];
buf[7] = buf[8];
if (analogRead(SER) > 500)
buf[8] = 1;
else buf[8] = 0;
do {
delay(1);}
while (analogRead(Clock)>500);
}
}