only works the first time - can't work out why?!


hi,

i have following code i'm using make foot pedal controller. has 22 foot switches each led plus lcd display can send messages via serial connection.

i have built simple message system interpret messages computer.

sending "1 hello" on serial writes "hello" row 1 of lcd.
sending "2 people" on serial writes "people" row 2 of lcd.
this works planned i'm having real trouble implementing control on leds. if send message "3 1" turns on correct led second time send message nothing happens although correct 'ledval' printed serial moitor.

i have tried can think of debug this, can see why happening?

code: [select]
// *** 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 st_cp of 74hc595
int clockpin = 12; //pin connected sh_cp of 74hc595
int datapin = 11; ////pin connected ds of 74hc595
//string chopping
#include <stdio.h>
#include <string.h>
   
    char str2[15];
    int  j;
    int ledval = 0;



//holders infromation you're going pass shifting function
byte dataa;
byte datab;
byte datac;


// *** lcd variables ***
#include <liquidcrystal.h>
liquidcrystal lcd(13, 10, 5, 4, 3, 2); // initialize library numbers of 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 lcd's number of rows , columns:
 lcd.clear();
 
////dataarraya[0] = 15; //11111111
 // dataarrayb[0] = 1; //11111111
// dataarrayc[0] = 1; //11111111
  // dataa = 0;  //  set leds off!
  // datab = 0;
  // datac = 0;
//  ground latchpin , hold low long transmitting
 // digitalwrite(latchpin, 0);
   //move 'em out
   //shiftout(datapin, clockpin, datac);  
   //shiftout(datapin, clockpin, datab);
     // shiftout(datapin, clockpin, dataa);
   //return latch pin high signal chip
   //no longer needs listen information
//digitalwrite(latchpin, 1);
 
}

void loop() {  // *** loop start ***

 divvalue = analogread(analoginpin);  // read analog in value fs voltage divider (divvalue) , decide 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 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 vd reading has been same 5 readings in row
{
if (outval != outvalold){ // if has check outval different last 1 out (a change filter)
serial.print("0 ");
serial.println(outval);
outvalold = outval;     // send outval on serial prefix '0'
}
}

valcount ++;
valcount = valcount % 5;

// *** reading serial data max/msp ***
if( serial.available())
 {

   char ch = serial.read();
   
   charcount ++; // count characters coming through serial
   // delay(1);
   if( ch == '^')  // terminating carriage return
   {

     buffer[ bufferindex ] = 0; // terminate string 0
     bufferindex = 0;  // reset index ready string
     updatelcd = 1;

     if (updatelcd == 1 && buffer[0] == '1') {

       lcd.setcursor(0,0);
       for(int = 0; <  strlen; i++) {
         lcd.write(32); // erase row writing spaces colls.
       }

       lcd.setcursor(0,0);
       for(int = 2; <  charcount - 2; i++) {
         lcd.write(buffer[i]); // write buffer first row of lcd
       }
     }

     if (updatelcd == 1 && buffer[0] == '2') {

       lcd.setcursor(0,1);
       for(int = 0; <  strlen; i++) {
         lcd.write(32);  // erase row writing spaces colls.
       }
       lcd.setcursor(0,1);
       for(int = 2; <  charcount - 2; i++) {
         lcd.write(buffer[i]); // write buffer second row of lcd
       }
     }
     
       if (updatelcd == 1 && buffer[0] == '3') {
         
            (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 , hold low long 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 latch pin high signal chip
   //no longer needs listen information

//  delay(1);
   
   
   
   
         
     serial.println(ledval); // debug
     // serial.flush();
     }
     
    charcount = 0;
    updatelcd = 0;

   }
   else
     buffer[ bufferindex++ ] = ch; // add character buffer
 }


}   // *** loop end ***

// heart of led program
void shiftout(int mydatapin, int myclockpin, byte mydataout) {
 // shifts 8 bits out msb first,
 //on rising edge of clock,
 //clock idles low

 //internal function setup
 int i=0;
 int pinstate;
 pinmode(myclockpin, output);
 pinmode(mydatapin, output);

 //clear out in case to
 //prepare shift register bit shifting
 digitalwrite(mydatapin, 0);
 digitalwrite(myclockpin, 0);

 //for each bit in byte mydataout[ch65533]
 //notice counting down in our loop
 //this means %00000001 or "1" go through such
 //that pin q0 lights.
 for (i=7; i>=0; i--)  {
   digitalwrite(myclockpin, 0);

   //if value passed mydataout , bitmask result
   // true then... if @ i=6 , our value is
   // %11010100 code compares %01000000
   // , proceeds set pinstate 1.
   if ( mydataout & (1<<i) ) {
     pinstate= 1;
   }
   else {      
     pinstate= 0;
   }

   //sets pin high or low depending on pinstate
   digitalwrite(mydatapin, pinstate);
   //register shifts bits on upstroke of clock pin  
   digitalwrite(myclockpin, 1);
   //zero data pin after shift prevent bleed through
   digitalwrite(mydatapin, 0);
 }

 //stop shifting
 digitalwrite(myclockpin, 0);
}



thanks john.

ah! worked out.

just needed
code: [select]
pinmode(latchpin, output);
in setup.

cheers,

john


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > only works the first time - can't work out why?!


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums