Hi,
I have the following code which i'm using to make a foot pedal controller. It has 22 foot switches each with an LED plus an LCD display which you can send messages to via a serial connection.
I have built a simple message system to interpret messages from a computer.
sending "1 hello" over serial writes "hello" to row one of the LCD.
sending "2 people" over serial writes "people" to row two of the LCD.
This all works as planned but i'm having real trouble implementing control over the LEDs. If I send the message "3 1" it turns on the correct LED but the second time i send the message nothing happens although the correct 'LEDval' is printed to the serial moitor.
I have tried everything i can think of to debug this, can anyone see why this is happening?
// *** Votage Divider VARIABLES ***
const int analogInPin = 0; // Voltage divider FSw
int outVal = 0; // VD
int outValOLD = 0; // VD
int divValue = 0; // voltage divider value (foot switches)
int valCount = 0; // VD
int outVal_0 = 0; // VD
int outVal_1 = 0; // VD
int outVal_2 = 0; // VD
int outVal_3 = 0; // VD
int outVal_4 = 0; // VD
int outVal_5 = 0; // VD
//*** LED VARIABLES ***
int latchPin = 8; //Pin connected to ST_CP of 74HC595
int clockPin = 12; //Pin connected to SH_CP of 74HC595
int dataPin = 11; ////Pin connected to DS of 74HC595
//string chopping
#include <stdio.h>
#include <string.h>
char str2[15];
int j;
int LEDval = 0;
//holders for infromation you're going to pass to shifting function
byte dataA;
byte dataB;
byte dataC;
// *** LCD VARIABLES ***
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 10, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
// *** SERIAL INPUT VARIABLES ***
#define STRLEN 40
int updateLCD = 1;
char buffer[STRLEN];
int charCount = 0;
int bufferIndex = 0;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // set up the LCD's number of rows and columns:
lcd.clear();
////dataArrayA[0] = 15; //11111111
// dataArrayB[0] = 1; //11111111
// dataArrayC[0] = 1; //11111111
// dataA = 0; // set all LEDs to off!
// dataB = 0;
// dataC = 0;
// ground latchPin and hold low for as long as you are transmitting
// digitalWrite(latchPin, 0);
//move 'em out
//shiftOut(dataPin, clockPin, dataC);
//shiftOut(dataPin, clockPin, dataB);
// shiftOut(dataPin, clockPin, dataA);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
//digitalWrite(latchPin, 1);
}
void loop() { // *** LOOP START ***
divValue = analogRead(analogInPin); // read the analog in value for FS Voltage divider (divValue) and decide which FS has been pressed (outVal).
if (divValue >= 510 && divValue <= 513) {outVal = 0;}
if (divValue >= 449 && divValue <= 451) {outVal = 1;}
if (divValue >= 425 && divValue <= 427) {outVal = 2;}
if (divValue >= 370 && divValue <= 372) {outVal = 3;}
if (divValue >= 339 && divValue <= 341) {outVal = 4;}
if (divValue >= 267 && divValue <= 269) {outVal = 5;}
if (divValue >= 796 && divValue <= 798) {outVal = 6;}
if (divValue >= 492 && divValue <= 494) {outVal = 7;}
if (divValue >= 472 && divValue <= 474) {outVal = 8;}
if (divValue >= 399 && divValue <= 401) {outVal = 9;}
if (divValue >= 126 && divValue <= 128) {outVal = 10;}
if (divValue >= 754 && divValue < 756) {outVal = 11;}
if (divValue >= 843 && divValue <= 845) {outVal = 12;}
if (divValue >= 650 && divValue <= 652) {outVal = 13;}
if (divValue >= 596 && divValue <= 598) {outVal = 14;}
if (divValue >= 550 && divValue <= 552) {outVal = 15;}
if (divValue >= 226 && divValue <= 228) {outVal = 16;}
if (divValue >= 716 && divValue <= 718) {outVal = 17;}
if (divValue >= 622 && divValue <= 624) {outVal = 18;}
if (divValue >= 573 && divValue <= 575) {outVal = 19;}
if (divValue >= 530 && divValue <= 532) {outVal = 20;}
if (divValue >= 305 && divValue <= 307) {outVal = 21;}
if (divValue >= 682 && divValue <= 684) {outVal = 22;}
switch (valCount) {
case 0:
outVal_0 = outVal;
break;
case 1:
outVal_1 = outVal;
break;
case 2:
outVal_2 = outVal;
break;
case 3:
outVal_3 = outVal;
break;
case 4:
outVal_4 = outVal;
break;
// in case you need more jitter avoidence
//case 5:
//outVal_5 = outVal;
//break;
}
if (outVal_0 == outVal_1 && outVal_0 == outVal_2 && outVal_0 == outVal_3 && outVal_0 == outVal_4) // check that VD reading has been the same for 5 readings in a row
{
if (outVal != outValOLD){ // if it has check that the outVal is different to the last one out (a change filter)
Serial.print("0 ");
Serial.println(outVal);
outValOLD = outVal; // send outVal over serial with the prefix '0'
}
}
valCount ++;
valCount = valCount % 5;
// *** READING SERIAL DATA FROM MAX/MSP ***
if( Serial.available())
{
char ch = Serial.read();
charCount ++; // count characters coming through serial
// delay(1);
if( ch == '^') // is this the terminating carriage return
{
buffer[ bufferIndex ] = 0; // terminate the string with a 0
bufferIndex = 0; // reset the index ready for another string
updateLCD = 1;
if (updateLCD == 1 && buffer[0] == '1') {
lcd.setCursor(0,0);
for(int i = 0; i < STRLEN; i++) {
lcd.write(32); // erase row by writing spaces into all colls.
}
lcd.setCursor(0,0);
for(int i = 2; i < charCount - 2; i++) {
lcd.write(buffer[i]); // write from buffer into first row of LCD
}
}
if (updateLCD == 1 && buffer[0] == '2') {
lcd.setCursor(0,1);
for(int i = 0; i < STRLEN; i++) {
lcd.write(32); // erase row by writing spaces into all colls.
}
lcd.setCursor(0,1);
for(int i = 2; i < charCount - 2; i++) {
lcd.write(buffer[i]); // write from buffer into second row of LCD
}
}
if (updateLCD == 1 && buffer[0] == '3') {
for (j=0; buffer[j]; j++)
str2[j] = buffer[j+2];
str2[j] = '\0';
LEDval = 0;
LEDval = atoi(str2);
dataA = LEDval;
dataB = 0;
dataC = 0;
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, LOW);
//move 'em out
shiftOut(dataPin, clockPin, dataC);
//digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, dataB);
// digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, dataA);
digitalWrite(latchPin, HIGH);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
// delay(1);
Serial.println(LEDval); // debug
// Serial.flush();
}
charCount = 0;
updateLCD = 0;
}
else
buffer[ bufferIndex++ ] = ch; // add the character into the buffer
}
} // *** LOOP END ***
// the heart of the LED program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOut[ch65533]
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}
Thanks John.