stuck in loop


i have  8 inputs thru shift register (4021) shift in part works fine. want send out midi control change if contact inputs have changed previous state, continues send midi cc. stuck in loop. can make suggestions. here code
code: [select]
#include <midi.h>

// ******************************************
// variables definitions
// ******************************************
 int volume;
 byte stage;
// *********************
// buttons multiplexing variables
// definition of pins
int buttonsmuxclockpin = 7;
int buttonsmuxlatchpin = 8;
int buttonsmuxdatapin = 9;

// define variables hold data
// each shift register.
byte muxexpression1;

// midi channel send notes each mux
int midichannel1 = 1;

// *********************
// set application
// *********************
void setup() {
// standard serial begin
// serial.begin(9600);
// midi serial begin
serial.begin(31250);

// buttons mux
// define pin modes
pinmode(buttonsmuxlatchpin, output);
pinmode(buttonsmuxclockpin, output);
pinmode(buttonsmuxdatapin, input);
}

// ******************************************
// main loop
// ******************************************
void loop() {
// butons mux
// function handles digital mux buttons
startreadingbuttonmux();


}

// ******************************************
// buttons mux
// ******************************************
// activate digital mux buttons
void startreadingbuttonmux() {
//  pulse latch pin:
//  set 1 collect parallel data
digitalwrite(buttonsmuxlatchpin, high);
// .... collecting data digital inputs of mux ... wait ...
delaymicroseconds(20);
// set 0 transmit data serially  
digitalwrite(buttonsmuxlatchpin, low);

// while shift register in serial mode
//collect each shift register byte
//the register attached chip comes in first
muxexpression1 = shiftin(buttonsmuxdatapin, buttonsmuxclockpin);

analyzeexpressionmux(muxexpression1, midichannel1);
}

// shiftin function
// needs location of data pin , clock pin
// returns byte each bit in byte corresponding
// pin on shift register. leftbit 7 = pin 7 / bit 0= pin 0
byte shiftin(int mydatapin, int myclockpin) {
int i;
int temp = 0;
 // int pinstate;
byte mydatain = 0;

pinmode(myclockpin, output);
pinmode(mydatapin, input);

// holding clock pin high 8 times (0,..,7) @ the
// end of each time through loop

// @ begining of each loop when set clock low, will
// doing necessary low high drop cause shift
// register's datapin change state based on value
// of next bit in serial information flow.
// register transmits information pins pin 7 pin 0
// why our function counts down

// loop pins of shift reg.
(i=7; i>=0; i--)
{
  digitalwrite(myclockpin, low);
  delaymicroseconds(2);
  temp = digitalread(mydatapin);
  if (temp) {
    // pinstate = 1;
    // set bit 1 no matter what
    mydatain = mydatain | (1 << i);
  }
  else {
    //turn off -- necessary debuging
    //print statement since mydatain starts 0
    // pinstate = 0;
  }

digitalwrite(myclockpin, high);
}
//debuging print statements whitespace
//serial.println();
//serial.println(mydatain, bin);
return mydatain;
}

void analyzeexpressionmux(byte muxexpression1,  int midichannel) {
   
 if (stage != muxexpression1){    //only send midi if input has changed
     
   switch (muxexpression1){
   
     case b00000000:
       volume = 20 ;
       break;
     case b00000001:
       volume = 33 ;
       break;
     case b00000011:
       volume = 46 ;
       break;
     case b00000111:
       volume = 59 ;
       break;
     case b00001111:
       volume = 72 ;
       break;
     case b00011111:
       volume = 85 ;
       break;
     case b00111111:
       volume = 98 ;
       break;
     case b01111111:
       volume = 111 ;
       break;
     case b11111111:
       volume = 127 ;
       break;
     default:
     stage = muxexpression1;
   }
                                           
     sendmidi(cc , volume, 7, midichannel);
     stage = muxexpression1;                    //set current stage
     
     }
     else{                                      //input same before nothing
       if(stage == muxexpression1){
       
       }
     }
 }  



   
//void controlchange(byte channel, byte controller, byte value)


// ******************************************
// midi functions
// ******************************************
// send midi note-on/off message.  
void sendmidi(char cmd, char data1, char data2, int midichannel) {
// based on midi library
midi.send(cmd, data1, data2, midichannel);
}


code: [select]
void analyzeexpressionmux(byte muxexpression1,  int midichannel) {
 int volume;
 byte stage;


the definition of 'stage' should outside definition of analyzeexpressionmux global variable , persists 1 calling of function next. stands not remembering previous value of muxexpression1 because 'stage' being reset every time.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > stuck in loop


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