Help Requested  (Shift registers, Webserving)

I am working on a project trying to get the arduino to read in 10 digital inputs with shiftregisters and output a status that coresponds to a predetermined worded message which goes to a display, as well as updates a webserver with the status message. It also needs to read in some data ( 8 checkboxes) from a basic html page and use those checkboxes to set an 8 bit output register appropriately.

I never have taken any c++ or c coding classes, only some python and html.

I am having tons of trouble getting the arduino to read in the 2 shift registers. I have adapted some of the code from the shift in tutorial to it ( I am using 2 of the cd4021be). I can read it in as 2 bytes and compare it to preset values in the code with a case statement, but as soon as I let it run through all the case statements it freezes. I have tried if..else statments to compare to and they freeze the board as well. I'm not sure if I can change these 2 bytes into 1 string and use that as a 16 digit comparison which would make things easier.

Some of the ethernet code and output register code is set up as well, but its not all completed yet.

Please provide some help!!

I am still working but having trouble with the logic as well reading in the bytes and using them together to compare to one large 16 bit number that I have manually entered.

[quote]
#include <[color=#CC6600]SPI[/color].h>
#include <[color=#CC6600]Ethernet[/color].h>
#include <[color=#CC6600]String[/color].h>

[color=#7E7E7E]//configure lan settings//[/color]
[color=#CC6600]byte[/color] mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; [color=#7E7E7E]//physical mac address[/color]
[color=#CC6600]byte[/color] ip[] = {192,168,0,187 };       [color=#7E7E7E]// ip in lan[/color]
[color=#CC6600]byte[/color] gateway[] = {192,168,0,1 };       
[color=#CC6600]byte[/color] subnet[] = {255, 255, 255, 0 };
[color=#CC6600]Server[/color] server(80); 
[color=#CC6600]int[/color] ledPin = 4; [color=#7E7E7E]// led pin variable[/color]
[color=#CC6600]String[/color] readString = [color=#CC6600]String[/color](30); [color=#7E7E7E]//string for fetching data from address[/color]
[color=#CC6600]boolean[/color] LEDON = [color=#CC6600]false[/color]; [color=#7E7E7E]//LED status flag[/color]
[color=#7E7E7E]//end of ethernet stuff[/color]

[color=#7E7E7E]//Configure DIP variables for output shift register[/color]
[color=#7E7E7E]//boolean dip1;   //open = true = 1 [/color]
[color=#7E7E7E]//boolean dip2;[/color]
[color=#7E7E7E]//boolean dip3;[/color]
[color=#7E7E7E]//boolean dip4;[/color]
[color=#7E7E7E]//boolean dip5;[/color]
[color=#7E7E7E]//boolean dip6;[/color]
[color=#7E7E7E]//boolean dip7;[/color]
[color=#7E7E7E]//boolean dip8;[/color]

[color=#7E7E7E]//hold the LRM operational state human text[/color]
[color=#CC6600]char[/color]* registerinput;
[color=#CC6600]char[/color]* statusid[1]= {[color=#006699]"Status Is"[/color]};
[color=#CC6600]char[/color]* statusmessage[34] = {[color=#006699]"None/Power off"[/color], 
[color=#006699]"Resident Shutdown"[/color], 
[color=#006699]"NRB Shutdown"[/color],
[color=#006699]"GHS Shutdown"[/color],
[color=#006699]"Resident Initialization"[/color],
[color=#006699]"Common Monitor"[/color]};
[color=#7E7E7E]//"Evaluate RAM Loss/DRAM Initialization",[/color]
[color=#7E7E7E]//"COM/MON Initial Sync",[/color]
[color=#7E7E7E]//"NRB Flash Copy",[/color]
[color=#7E7E7E]//"PUB Entry Sync",[/color]
[color=#7E7E7E]//"PUB State 1",[/color]
[color=#7E7E7E]//"PUB State 2",[/color]
[color=#7E7E7E]//"OPS Flash Copy",[/color]
[color=#7E7E7E]//"OPS DRAM Initialization",[/color]
[color=#7E7E7E]//"Scheduler Initial Sync",[/color]
[color=#7E7E7E]//"GHS Initialization",[/color]
[color=#7E7E7E]//"Scheduling State 1",[/color]
[color=#7E7E7E]//"Scheduling State 2",[/color]
[color=#7E7E7E]//"Dataload Initialization",[/color]
[color=#7E7E7E]//"Dataload Master Wait for Operation",[/color]
[color=#7E7E7E]//"Dataload Master Wait for Header",[/color]
[color=#7E7E7E]//"Dataload Master Load NRB",[/color]
[color=#7E7E7E]//"Dataload Master Load SLDB",[/color]
[color=#7E7E7E]//"Dataload Master Validate SLDB",[/color]
[color=#7E7E7E]//"Dataload Master Load OPS",[/color]
[color=#7E7E7E]//"Dataload Primitive Erase",[/color]
[color=#7E7E7E]//"Dataload MAster Get Next File",[/color]
[color=#7E7E7E]//"Dataload Primitive Write",[/color]
[color=#7E7E7E]//"Dataload Master XDP Active",[/color]
[color=#7E7E7E]//"Dataload Information Operation",[/color]
[color=#7E7E7E]//"Dataload Operator Download",[/color]
[color=#7E7E7E]//"Dataload Media Download",[/color]
[color=#7E7E7E]//"Dataload Abort",[/color]
[color=#7E7E7E]//"Dataload Validate"};[/color]

[color=#7E7E7E]//Arduino pins that are connected to the INPUT shift register[/color]
[color=#CC6600]int[/color] inputlatchPin = 8;  [color=#7E7E7E]//to shift register pin 9[/color]
[color=#CC6600]int[/color] inputdataPin = 9; [color=#7E7E7E]// to shift register pin 3[/color]
[color=#CC6600]int[/color] inputclockPin = 10; [color=#7E7E7E]// to shift register pin 10[/color]

[color=#7E7E7E]//Define variables to hold the data for each shift register. starting with non-zero numbers can help troubleshoot[/color]
[color=#CC6600]int[/color] switchVar1;  [color=#7E7E7E]//01001000[/color]
[color=#CC6600]int[/color] switchVar2; [color=#7E7E7E]//10011111[/color]

[color=#7E7E7E]//end of shift reg input stuff[/color]

[color=#7E7E7E]// DEFINE output shift register variables and pins [/color]
[color=#7E7E7E]// 74HC595[/color]
[color=#CC6600]int[/color] outputlatchPin = 5; [color=#7E7E7E]// connected to sr pin 12[/color]
[color=#CC6600]int[/color] outputclockPin = 7; [color=#7E7E7E]// connected to sr pin 11[/color]
[color=#CC6600]int[/color] outputdataPin = 6; [color=#7E7E7E]// connected to sr pin 14[/color]
[color=#7E7E7E]//end  of shift out stuff[/color]





[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]() {
  [color=#7E7E7E]//start ethernet[/color]
  [color=#CC6600]Ethernet[/color].[color=#CC6600]begin[/color](mac, ip, gateway, subnet);
  [color=#7E7E7E]//start serial[/color]
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);

  [color=#7E7E7E]//define pin modes for input shift register[/color]
  [color=#CC6600]pinMode[/color](inputlatchPin, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](inputclockPin, [color=#006699]OUTPUT[/color]); 
  [color=#CC6600]pinMode[/color](inputdataPin, [color=#006699]INPUT[/color]);   
  [color=#7E7E7E]//set pins for output shift register[/color]
  [color=#CC6600]pinMode[/color](outputlatchPin, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](outputdataPin, [color=#006699]OUTPUT[/color]);  
  [color=#CC6600]pinMode[/color](outputclockPin, [color=#006699]OUTPUT[/color]);
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]"reset"[/color]);
  [color=#7E7E7E]// this is for ETHERNET. Set variable ledPin to output mode[/color]
  [color=#CC6600]pinMode[/color](ledPin, [color=#006699]OUTPUT[/color]); 
  [color=#7E7E7E]//TEST[/color]
  [color=#CC6600]String[/color] registerinput;
}

[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]() {
  
[color=#7E7E7E]//SHIFT INPUT[/color]
  [color=#7E7E7E]//Pulse the latch pin:[/color]
  [color=#CC6600]digitalWrite[/color](inputlatchPin,1);   [color=#7E7E7E]//set it high to tell it to collect data[/color]
    [color=#CC6600]delayMicroseconds[/color](20);   [color=#7E7E7E]// wait a bit to let it work  [/color]
  [color=#CC6600]digitalWrite[/color](inputlatchPin,0);   [color=#7E7E7E]//set it low to tell it to transmit data [/color]

  [color=#7E7E7E]//while the shift register is in serial mode collect each shift register into a byte [/color]
  [color=#7E7E7E]//the register attached to the chip comes in first [/color]
  switchVar1 = [color=#CC6600]char[/color]([color=#CC6600]shiftIn[/color](inputdataPin, inputclockPin));  [color=#7E7E7E]// read in data from first input shift reg[/color]
  switchVar2 = [color=#CC6600]char[/color]([color=#CC6600]shiftIn[/color](inputdataPin, inputclockPin)); [color=#7E7E7E]// read in data from second input shift reg[/color]
  registerinput = {strcat(switchVar1,switchVar2)};
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](registerinput);

  [color=#7E7E7E]//Print out the results.[/color]
  [color=#7E7E7E]//leading 0's at the top of the byte [/color]
  [color=#7E7E7E]//(7, 6, 5, etc) will be dropped before [/color]
  [color=#7E7E7E]//the first pin that has a high input[/color]
  [color=#7E7E7E]//reading  [/color]
 [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]"first one"[/color]);
 [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#CC6600]String[/color](switchVar1, [color=#006699]BIN[/color]));
 [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]"second one"[/color]);
 [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#CC6600]String[/color](switchVar2, [color=#006699]BIN[/color]));
 
[color=#7E7E7E]//white space[/color]
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]"-------------------"[/color]);
[color=#CC6600]switch[/color] (registerinput){
  [color=#CC6600]case[/color] 0: 
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](strcat(statusid[0], statusmessage[0]));
  [color=#CC6600]break[/color];

  [color=#CC6600]case[/color] 278:
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](strcat(statusid[0], statusmessage[1]));
  [color=#CC6600]break[/color];
  
 
}


[color=#7E7E7E]//attempt at the if statments.  They havent been converted all to case statments yet. [/color]
[color=#7E7E7E]//if(switchVar1==B00000000 && switchVar2 == B0000000){[/color]
[color=#7E7E7E]//Serial.println("State is :" + lrm3);[/color]
[color=#7E7E7E]//}[/color]
[color=#7E7E7E]//if(switchVar1==B00000000 && switchVar2 == B0000000){[/color]
[color=#7E7E7E]//Serial.println("State is :" + lrm4);[/color]
[color=#7E7E7E]//}[/color]
[color=#7E7E7E]//if(switchVar1==B00000000 && switchVar2 == B0000000){[/color]
[color=#7E7E7E]//Serial.println("State is :" + lrm5);[/color]
[color=#7E7E7E]//}[/color]
[color=#7E7E7E]//if(switchVar1==B00000000 && switchVar2 == B0000000){[/color]
[color=#7E7E7E]//Serial.println("State is :" + lrm6);[/color]
[color=#7E7E7E]//}[/color]
[color=#7E7E7E]//if(switchVar1==B00000000 && switchVar2 == B0000000){[/color]
[color=#7E7E7E]//Serial.println("State is :" + lrm7);[/color]
[color=#7E7E7E]//}[/color]
[color=#7E7E7E]//if(switchVar1==B00000000 && switchVar2 == B0000000){[/color]
[color=#7E7E7E]//Serial.println("State is :" + lrm8);[/color]
[color=#7E7E7E]//}[/color]
[color=#7E7E7E]//if(switchVar1==B00000000 && switchVar2 == B0000000){[/color]
[color=#7E7E7E]//Serial.println("State is :" + lrm9);[/color]

//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm9);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm10);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm11);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm12);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm13);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm14);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm15);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm16);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm17);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm18);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm19);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm20);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm21);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm22);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm23);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm24);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm25);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm25);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm26);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm27);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm28);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm29);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm30);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm31);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm32);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm33);
//}
//if(switchVar1==B00000000 && switchVar2 == B0000000){
//Serial.println("State is :" + lrm34);
//}
//delay so all these print satements can keep up.
delay(500);
//END SHIFT IN

//shift out BEGIN
if (Serial.available() > 0) {
// ASCII '0' through '9' characters are
// represented by the values 48 through 57.
// so if the user types a number from 0 through 9 in ASCII,
// you can subtract 48 to get the actual value:
int bitToSet = Serial.read() - 48;

// write to the shift register with the correct bit set high:
registerWrite(bitToSet, HIGH);
}

}

void registerWrite(int whichPin, int whichState) {
// the bits you want to send
byte bitsToSend = 0;
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(outputlatchPin, LOW);

// turn on the next highest bit in bitsToSend:
bitWrite(bitsToSend, whichPin, whichState);

// shift the bits out:
shiftOut(outputdataPin, outputclockPin, MSBFIRST, bitsToSend);

// turn on the output so the LEDs can light up:
digitalWrite(outputlatchPin, HIGH);

}

//end shift out

//leftBit 7 = Pin 7 / Bit 0= Pin 0

byte shiftIn(int myDataPin, int myClockPin) {
int a;
int i;
int temp = 0;
int pinState;
int myDataIn[] = 00000000;

pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, INPUT);

for (i=7; i>=0; i--){
digitalWrite(myClockPin, 0);
delayMicroseconds(2);
temp = digitalRead(myDataPin);
delay(200);
if (temp) {
pinState = 1;
bitWrite(myDataIn, i, 1);
}
else {
pinState = 0;
bitWrite(myDataIn,i,0);
}
digitalWrite(myClockPin, 1);
}
return myDataIn;
//Serial.println(myDataIn);

//ETHERNET server loop

}

I can read it in as 2 bytes and compare it to preset values in the code with a case statement, but as soon as I let it run through all the case statements it freezes. I have tried if..else statments to compare to and they freeze the board as well.

It's amazing. You've made a Peltier device out of your Arduino.

What exactly did you really mean?

If you are not getting proper behavior from one bit of code, create a separate sketch to work on that problem. Posting (or working on) hundreds of lines of code doesn't make sense, when 90% of the code is unrelated to the problem.

Post a smaller sketch that illustrates the problem you are having, and we'll help you solve it. One problem at a time, though.

int switchVar1;  //01001000
int switchVar2; //10011111
.
.
.
  switchVar1 = char(shiftIn(inputdataPin, inputclockPin));  // read in data from first input shift reg
  switchVar2 = char(shiftIn(inputdataPin, inputclockPin)); // read in data from second input shift reg
  registerinput = {strcat(switchVar1,switchVar2)};

If you want switchVar1 to contain a char value, why not declare it as a char?

The strcat function expects NULL terminated strings. The values you pass it are not NULL terminated strings.

The strcat function appends the second string to the end of the end of the first string. You do not have space in the first "string" to hold the second "string".

The strcat function returns a value, but you are not capturing that value in a variable, and the value is meaningless, anyway, since the function didn't do what you expected it to do. Even if you were capturing the value, and it was good, if the variable that you stored the result in were local, it would go out of scope at the end of the block (the {} define a block.

Blocks of code do not return values, so assigning this value to a String variable is meaningless.

Fix at least some of these issues, and see whether that solves your problem.

Thanks PaulS.

I posted the entire sketch because I wanted to give some perspective of the entire fuction I want to accomplish to see if anyone could offer better ways to do it.

When I enable all the variables in the statusmessage[34] or enable all of the if statements, before I started to try and use switch..case statments the board went from outputting the proper text over Serial, when only one was enabled to just printing a small bit of nonsense when all enabled and then stopping. I could provide a high input to the register and it would output the proper status until I uncomment all the varis and if statments.

Roughly put this is what I took out of your post that I should do:

//declare variables as chars
char* switchvar1
char* switchvar2

//read in the registers each as an 8 digit number of 1s and 0s
switchVar1 = shiftIn(inputdataPin, inputclockPin);
switchVar2 = shiftIn(inputdataPin, inputclockPin);

//combine the two into one 16 digit number
registerinput = strcat(switchVar1,switchVar2);

//compare this 16 digit number against a preset case and print a
//status out of the statusmessage[] array
switch (registerinput){
case 0000000000000000:
Serial.println(strcat(statusid[0], statusmessage[0]));
break;

Is there a better way to compare these two 8 digit numbers to a set of 10 states then making one large 16 digit (6 drop off) then converting to a string and comparing? Now that I think about it, when the register reads in bits, it drops insignificant zeros, which might be hard to keep track of if I am converting two together to a string.

registerinput = {strcat(switchVar1,switchVar2)};

This is really terribly broken code, about the worst I've seen on this forum - and this forum has a very deep pool of bad code. You don't want to know how that may run amok across your RAM. Use:

registerinput = (switchVar1 << 8) | switchVar2;

Depending on how the bits are read, you might need to shift the variable for less bits or shift the other variable.

If you do a switch, don't compare against B01010110, compare against 0b01010110. The first one is a kludgy macro working only for 8 bits, the second one is a legal way to input any number in binary and works also with integers and long (16 or 32 bits). The it works also with switch.

Is there a better way to compare these two 8 digit numbers to a set of 10 states then making one large 16 digit (6 drop off) then converting to a string and comparing?

Yes, but it depends on what you want to do with them. Are those 10 bits independent (eg bit3 is the toilet-brush, bit7 the kitchen-sink) or linked so you have 1024 distinct states. Depending on how things are, the way to treat them properly varies. From your message I can't really figure out.

Korman

//declare variables as chars
char* switchvar1
char* switchvar2

At the risk of repeating myself, when the comments and the code disagree, the code i ALWAYS right.

In this case, the variables are not character variables, They are "pointer to character" variables, and that is a completely different animal.

You missed the semicolons. by the way.

I posted the entire sketch because I wanted to give some perspective of the entire fuction I want to accomplish to see if anyone could offer better ways to do it.

And my point was that you don't even have the basics working properly.

When you build an application. you should create a sketch to test each part. Start with creating sketches to test each piece of hardware. When you have each sketch working for ONE piece of hardware, then begin combining the hardware, and test each sketch again. You'll then figure out what hardware conflicts there are, if any.

When the hardware all works together, then you can start combining the sketches to produce the final application.

This is called walking before running. As is is now, you haven't even got your shoes tied.

//combine the two into one 16 digit number
registerinput = strcat(switchVar1,switchVar2);

I told you before that this was wrong, and why it was wrong.

registerinput.append(switchvar1);
registerinput.append(switchvar2);

This will add both characters to the end of registerinput, once you make the two variables characters instead of pointer to characters.

switch (registerinput){
 case 0000000000000000:
 Serial.println(strcat(statusid[0], statusmessage[0]));
 break;

This will not work. The argument to the switch statement is an instance of the String class. The case values also need to be instances of the String class.

You really need to back up and think about what you are doing.

The shiftIn function returns a byte. That simple fact should tell you the type of variable to store the output in. A byte is 8 bits. An int is 16 bits. A perfect fit to hold two bytes. Korman's reply showed how to get two bytes into an int, even though it was not correct usage given the variable types involved. It will be when the variable on the left is an unsigned int, and the variables on the right are bytes.

Then, you can use the switch statement as you have shown. 0000000000000000 is the same as 0, though.

Quote:

Code:
registerinput = {strcat(switchVar1,switchVar2)};

This is really terribly broken code, about the worst I've seen on this forum - and this forum has a very deep pool of bad code. You don't want to know how that may run amok across your RAM. Use:

Code:
registerinput = (switchVar1 << 8) | switchVar2;

Depending on how the bits are read, you might need to shift the variable for less bits or shift the other variable.

If you do a switch, don't compare against B01010110, compare against 0b01010110. The first one is a kludgy macro working only for 8 bits, the second one is a legal way to input any number in binary and works also with integers and long (16 or 32 bits). The it works also with switch.

Quote:

Is there a better way to compare these two 8 digit numbers to a set of 10 states then making one large 16 digit (6 drop off) then converting to a string and comparing?

Yes, but it depends on what you want to do with them. Are those 10 bits independent (eg bit3 is the toilet-brush, bit7 the kitchen-sink) or linked so you have 1024 distinct states. Depending on how things are, the way to treat them properly varies. From your message I can't really figure out.

Korman

Thanks for the advice.

I am essentially trying to replicate an old hardware system. The old system displayed a bank of 10 LEDs to tell me what state it was in. The 2 registers will act in that banks replacement. This leads me to believe that they are linked.

Code:
//declare variables as chars
char* switchvar1
char* switchvar2

At the risk of repeating myself, when the comments and the code disagree, the code i ALWAYS right.

In this case, the variables are not character variables, They are "pointer to character" variables, and that is a completely different animal.

You missed the semicolons. by the way.

Quote:

I posted the entire sketch because I wanted to give some perspective of the entire fuction I want to accomplish to see if anyone could offer better ways to do it.
And my point was that you don't even have the basics working properly.

When you build an application. you should create a sketch to test each part. Start with creating sketches to test each piece of hardware. When you have each sketch working for ONE piece of hardware, then begin combining the hardware, and test each sketch again. You'll then figure out what hardware conflicts there are, if any.

When the hardware all works together, then you can start combining the sketches to produce the final application.

This is called walking before running. As is is now, you haven't even got your shoes tied.

Code:
//combine the two into one 16 digit number
registerinput = strcat(switchVar1,switchVar2);

I told you before that this was wrong, and why it was wrong.

Code:
registerinput.append(switchvar1);
registerinput.append(switchvar2);

This will add both characters to the end of registerinput, once you make the two variables characters instead of pointer to characters.

Code:
switch (registerinput){
case 0000000000000000:
Serial.println(strcat(statusid[0], statusmessage[0]));
break;

This will not work. The argument to the switch statement is an instance of the String class. The case values also need to be instances of the String class.

You really need to back up and think about what you are doing.

The shiftIn function returns a byte. That simple fact should tell you the type of variable to store the output in. A byte is 8 bits. An int is 16 bits. A perfect fit to hold two bytes. Korman's reply showed how to get two bytes into an int, even though it was not correct usage given the variable types involved. It will be when the variable on the left is an unsigned int, and the variables on the right are bytes.

Then, you can use the switch statement as you have shown. 0000000000000000 is the same as 0, though.

I had the registerinput variable declared as a unsigned int before I came here looking for help. I couldnt get it to work because I couldnt figure out the piece that Korman provided me. I figured converting it all to a char or string would be easier since I could try and implement strcat which I knew about. Ill change it back to bytes and ints and keep working on it when I get a chance.

I obviously can make the shiftin tutorial work, but it needs to work as a part of my bigger application. If there is a better way to read in those 10 variables that you know of then I would rather implement that, then to do something now and have to redo it.

I obviously can make the shiftin tutorial work, but it needs to work as a part of my bigger application.

I'm sorry, but that isn't at all obvious to me. I mean, I hope you can, but it appears that you need to spend some time with basic things like variable types and understanding that a function's output needs to be stored in an appropriately typed variable, and that when a function says it takes a variable of a specific type, that is what you need to send it.

If there is a better way to read in those 10 variables that you know of then I would rather implement that, then to do something now and have to redo it.

You are not reading in variables using shiftIn. You are reading in data, to store in values. And, not 10 of them.

What does the 10 variables refer to?

I am referring to the bits (data) read in from the shift register. I have two connected so it reads in 16 of them, of which I need to reference 10 in the bigger application. My old system used LEDs to represent a system state, which I need to now read into the arduino. I am simply creating a system that can serve a webpage that someone can use to see if the LED is on or off.

Not having any prior C knowledge, the variable types are somewhat confusing. I read the tutorials and the reference documents but selecting which one to use is sometime tricky.

I suggest you break down your project code into individual parts, and get a part working, then focus on the next. Much easier to spot where the issues are.

I am referring to the bits (data) read in from the shift register. I have two connected so it reads in 16 of them, of which I need to reference 10 in the bigger application.

OK. So what is the problem? You have a variable that can hold all 16 bits. You have bitRead(), bitWrite(), bitSet(), and bitClear() that can operate on individual bits.

You can check out the proper functioning of the shift registers and the code that deals with data from the shift registers while the ethernet shield is safely tucked away in a box.

When that code works, then get the ethernet shield back out.

I did restart the sketch, just working on the shift in code.

Before I read any of your replies I reworked most of the shiftIn function from the shiftIn tutorial to store the read in bits into an array 16 digits in length. It is able to capture the 16 digit long data. However I am having trouble returning that array to the main sketch(variable problem). I will post the code later tonight.

Would it be better to do it in bits still or to continue as an array?

I dont have much if any knowledge of bit wise operators or math.

I dont have much if any knowledge of bit wise operators or math.

The bitSet(), bitClear(), bitRead(), and bitWrite() functions mean that you don't have to. They allow you to set/get individual bits from an int, just like getting individual elements from an array.

Getting rid of the array simplifies passing the shifted in data to functions and reduces storage requirements.

On the other hand, if your code works, you are not limited on memory, and you can pass the array (not hard) to functions, stick with what you have.

OK I ditched the arrary code and am going to go with using Kormas code to combine the bytes.

How can I keep it from cutting off the LSB and just use a zero instead?

//define where your pins are
int latchPin = 8;
int dataPin = 9;
int clockPin = 10;

byte switchVar1;
byte switchVar2;
long registerinput;

void setup() {
  //start serial
  Serial.begin(9600);

  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);

}

void loop() {

  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin,1);
  //set it to 1 to collect parallel data, wait
  delayMicroseconds(20);
  //set it to 0 to transmit data serially  
  digitalWrite(latchPin,0);
  
  switchVar1 = shiftIn(dataPin, clockPin);
  switchVar2 = shiftIn(dataPin, clockPin);
  bitWrite(switchVar1,7,1);
  bitWrite(switchVar2,3,1);
  registerinput = (switchVar2 << 8) | switchVar1; 
  Serial.println(registerinput, BIN);
  Serial.println(bitRead(registerinput,2));
  Serial.println(bit(registerinput));
delay(1000);
}

int shiftIn(int myDataPin, int myClockPin) { 
    int i;
    int temp = 0;
    int pinState;
    byte myDataIn = 0;

    pinMode(myClockPin, OUTPUT);
    pinMode(myDataPin, INPUT);

    for (i=7; i>=0; i--)
  {
    digitalWrite(myClockPin, 0);
    delayMicroseconds(2);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      //set the bit to 0 no matter what
      myDataIn = myDataIn | (1 << i);
    }
    else {
      pinState = 0;
    }
    digitalWrite(myClockPin, 1);
  }
  return myDataIn;
}

Its what I have so far, I am keeping some bits high in the code so that I have pre set locations. It does seem like the wrong way to do it but I dont know another way. This outputs a 12 digit binary number of which 2 spots are coded high giving me 10 to work with.

  registerinput = (switchVar2 << 8) | switchVar1;

This is not completely overwriting the LSB of registerinput, but is completely overwriting the MSB. I think you want:

  registerinput = (switchVar2 << 8) + switchVar1;
  Serial.println(bit(registerinput));

I'm not sure what this is supposed to be doing. registerinput is a 16 bit value. It can't be converted to a single bit. Am I missing something?

Aside from that, are you still having issues you need help with? It isn't clear from your last post.

Thanks Paul.

The last line was just a test to see what the function would do. I forgot to remove it.

I started working on the Webserver code, indpendently of the input register, modifying some code I found on here that Zoomkat wrote to control and display the status of an LED from an html form.

I am running into the problem of when enabling too many if statments, the code will compile but not run on the arduino chip. I did some reseach and apparently around 9500k or so which my sketch is, the arduino runs out of memory and you need to start writing to PROGMEM, which I will have to learn how to do.

I believe I can add the entire block, or blocks of constant HTML form code to the program memory and save a lot of 'SRAM' in the process.

I was hoping to use PSTR() to move the html strings to the PROGMEM but it doesnt seem to be working.