This is my first time posting for help here - though I have lurked for years so I will do my best to post as complete a question as possible. Some of the code is my own, most is from others and I make no claims to it being correct, well written, following best practices, smart or otherwise anything but trash - so before firing off such commentary know that I "get" that this is bad code, poorly written for a nonsensical task that the Arduino gods should probably strike me down for even considering.
The base code for this project is from Martin Currey and his excellent tutorial on Arduino serial control found here.
What I want to accomplish
I want to use 8 switches toggle switches to set high/low values and send those values to the Arduino when I press a monetary pushbutton. I want to use those values to trigger specific actions.
What works
I have attached a crappy photo of my terrible circuit - it uses 8 slide switches (in place of toggles), a tactile switch, pulldown resistors (,probably wrong values) a 74HC165 shift register, some led's with current limiting resistors (probably wrong values) and a Mega2560.
As intended, when you set the switches and press the momentary switch, the values display in the serial monitor. When you type commands into the serial monitor with start and end markers defined in the code by M.Currey the correct output occurs: sets pin 4 HIGH and the attached LED illuminates. sets pin 4 LOW and the attached LED extinguishes. The same is true for <11111111> and <10000000>.
What does not work
When entering data from the shift register, I need the 8 bits to form a "keyword" (for lack of a better term) that will work in the same way as when entered via the serial port. The series of bits displays correctly in the serial port, but it is not read by the Arduino as a command - it is just writing out the 1's and 0's from the FOR loop as expected.
What I do not understand how to do is to obtain a single output variable from the FOR loop in the display_pin_values function (below with some testing artifacts) or a separate function that will accomplish the same thing as if it were typed into the serial port and sent to the Arduino. The full code is attached - it exceeded to post size limit.
void display_pin_values()
{
//start marker
//Serial.print("<");
//Serial.print("Experiment:\r\n");
for (int i = 0; i < DATA_WIDTH; i++)
{
if ((pinValues >> i) & 1) {
Serial.print("1");
}
else
Serial.print("0");
}
//end marker
//Serial.print(">");
Serial.print("\r\n");
}
Attachments
Photo of circuit and .ino file.
Thanks for any help you are able to provide.
arduinoSerialControl_01_sw_input.ino (9.12 KB)