how to latch a value

I am doing a laser harp project and i am having a little trouble with one detail. I get my music and all that but for some reason i can't get my octave value to stay changed i am just wondering where in the code do i put the input so that when it loops it doesn't change the value back to what it was. I have it right now in the loop as int octave and when it reads one of the two sensors i have changing its value i use octave -- or octave ++.

Code?

To latch a value, assign it to a variable and don't change the variable subsequently.

brainkj007:
wondering where in the code do i put the input so that when it loops it doesn't change the value back to what it was.

I have it right now in the loop as int octave and when it reads one of the two sensors i have changing its value i use octave -- or octave ++.

Put it right there. In your code. You do have code, don't you? It's hard to see.

You are probably forgetting to check for a change of input state:

void loop ()
{
  if (digitalRead (pin))
    octave ++ ;
  ..
}

Will increment octave at 100kHz or similar whenever the pin is high.

You meant to increment it once - so you have to check for the input
actually changing to HIGH, not being HIGH:

boolean prev = false ;
void loop ()
{
  boolean current = digitalRead (pin) ;
  if (current && !prev)  // test for a transition from LOW to HIGH
    octave ++ ;
  prev = current ; // maintain prev for next time round loop
  ..
}

Search the Arduino site for the words port manipulation or buy SPI-bus parallel to serial shift register chip(s).
On an UNO you have 3 sets or 6 pins/bits open. On a MEGA you have a couple or three full 8 bit ports open.
Using shift registers and external power, you could latch and read 64 bits in under 1/10th of a millisecond, and handle tasking in the other 36 milliseconds (at 16000 CPU cycles per millsecond, you can run a few calculations and throw a few switches in less) to give a 10,000+ per second read and react time. You'd be watching fingers cross the beams taking many dozen readings start to finish for 64 beams.

If you have less than 20 beams then an UNO should do. You wouldn't have to read the registers. Even pin by pin you could cover the beams at 20,000+ full scan and reacts per second, as long as the react isn't blocking/insane (like, using floats, delay()'s or a lot of serial out).

That's if you code it right. You can always make it slower.

#define sensor1 3
#define sensor2 4
#define sensor3 5
#define sensor4 6
#define sensor5 7
#define sensor6 8
#define sensor7 9
#define sensor8 10
#define sensor9 11
#define sensor10 12

#define sensor11 13
#define sensor12 A5
#define sensor13 2
#define sensor14 A4
#define cmd 0x90
#define velocity 0x7E
#define timedelay 100
void setup(){
  Serial.begin(57600);
  pinMode(sensor1,INPUT);
  pinMode(sensor2,INPUT);
  pinMode(sensor3,INPUT);
  pinMode(sensor4,INPUT);
  pinMode(sensor5,INPUT);
  pinMode(sensor6,INPUT);
  pinMode(sensor7,INPUT);
  pinMode(sensor8,INPUT);
  pinMode(sensor9,INPUT);
  pinMode(sensor10,INPUT);
  pinMode(sensor11,INPUT);
  pinMode(sensor12,INPUT);
  pinMode(sensor13,INPUT);
  pinMode(sensor14,INPUT);
 ;
}
void loop(){
 int octave=5 ;
  
  char laser11,laser2,laser3,laser4,laser5,laser6,laser7,laser8,laser9,laser10,laser12,laser13;
  char laser1,laser14;
laser1=digitalRead(sensor1);
laser2=digitalRead(sensor2);
laser3=digitalRead(sensor3);
laser4=digitalRead(sensor4);
laser5=digitalRead(sensor5);
laser6=digitalRead(sensor6);
laser7=digitalRead(sensor7);
laser8=digitalRead(sensor8);
laser9=digitalRead(sensor9);
laser10=digitalRead(sensor10);
laser11=digitalRead(sensor11);
laser12=digitalRead(sensor12);
laser13=digitalRead(sensor13);
laser14=digitalRead(sensor14);
if(laser14 == HIGH){
  octave++;
  delay(timedelay);
}
if(laser1 == HIGH){
  octave--;
  delay(100);
}


if(octave == -1){
  
if(laser1== HIGH){
  Serial.write(cmd);
  Serial.write(0x00);
  Serial.write(velocity);
  delay(timedelay);
}
if(laser2== HIGH){
    Serial.write(cmd);
  Serial.write(0x01);
  Serial.write(velocity);
  delay(timedelay);
}
if(laser3== HIGH){
    Serial.write(cmd);
  Serial.write(0x02);
  Serial.write(velocity);
  delay(timedelay);
}
if(laser4== HIGH){
    Serial.write(cmd);
  Serial.write(0x03);
  Serial.write(velocity);
  delay(timedelay);
}
if(laser5== HIGH){
  Serial.write(cmd);
  Serial.write(0x04);
  Serial.write(velocity);
  delay(timedelay);
}
if(laser6== HIGH){
  Serial.write(cmd);
  Serial.write(0x05);
  Serial.write(velocity);
  delay(timedelay);
}
if(laser7== HIGH){
  Serial.write(cmd);
  Serial.write(0x06);
  Serial.write(velocity);
  delay(timedelay);
}
if(laser8== HIGH){
  Serial.write(cmd);
  Serial.write(0x07);
  Serial.write(velocity);
  delay(timedelay);
}
if(laser9== HIGH){
  Serial.write(cmd);
  Serial.write(0x08);
  Serial.write(velocity);
  delay(timedelay);
}
if(laser10== HIGH){
  Serial.write(cmd);
  Serial.write(0x09);
  Serial.write(velocity);
  delay(timedelay);
}
if(laser11== HIGH){
  Serial.write(cmd);
  Serial.write(0x0A);
  Serial.write(velocity);
  delay(timedelay);
}
if(laser12== HIGH){
  Serial.write(cmd);
  Serial.write(0x0B);
  Serial.write(velocity);
  delay(timedelay);
}

this is only part of my code but what is happening is that it increments or decrements the octave once but then the value goes back to what it was before

int octave=5 ;Sets octave to 5 each time through the loop() function. Either make octave a global variable or declare it as a static local variable.

that seems to have fixed it . I had tried putting it into void setup thinking that would make it Global but guess i needed to put it up where i have my defines.

Soneone badly needs to learn about arrays and loops.

I tried doing that but it doesn't like to work that way .

#include <SoftwareSerial.h>


#define sensor0 3
#define sensor1 4
#define sensor2 5
#define sensor3 6
#define sensor4 7
#define sensor5 8
#define sensor6 9
#define sensor7 10
#define sensor8 11
#define sensor9 12
#define sensor10 13
#define sensor11 A5
#define sensor12 2  // octave changer
#define sensor13 A4 // octave changer
#define cmd 0x90 // command control code for midi  sets midi for channel one, note on
#define velocity 0x7E  //volume control code for midi   set to highest volume
int note;
int sensor[14];
int laser[12]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B};  //-1 octave note values
int sensor_value[14];
int octal[]={0x00, 0xC,0x18,0x24,0x30,0x3C, 0x48, 0x54, 0x60,0x6C, 0x78};  // the change in octave
int octave = 6;
void setup(){
  Serial.begin(57600); // midi runs at 31250 but another program requires 57600 to deal with delay when changing serialtomidi
for(int i=0; i<14; i++)
{
  pinMode (sensor[i], INPUT);

}
}
void loop(){
sensor_value[12]= digitalRead(sensor[12]);
 if(sensor_value[12] == HIGH){
     octave++;}
     sensor_value[13]= digitalRead(sensor[13]);
     if(sensor_value[13] == HIGH){
       octave--;}

  for(int s=0; s<12; s++)
  {
    sensor_value[s]=digitalRead(sensor[s]);
    if(sensor_value[s] == HIGH){
       note = laser[s];
       note = note + octal[octave];
      Serial.write(cmd);
  Serial.write(note);
  Serial.write(velocity);
  delay(100);
       }
       
}
  }

this is the other code I am working on to get this code to do the same thing but without writing 884 lines of code but it doesn't want to change octaves at all but it does do a little bit of the music.

have made a slight change that will probably work noticed i didn't put into the octal[] that i needed the array to look at what octave I am on.

Look at the code you posted in reply #10.
Can you read it ?

Why don't you use code tags as requested in the stickies of this forum ?

It reads just fine to me. I follow standard coding rules in that each variable has a name that tells you what it is for. Laser literally tells me what laser i am breaking to get me my signal and it corresponds to a note to send out to the midi.

Does any of the code look odd when you view it in your post here on the forum ?

Not really except someone tried to edit my first code.

'Someone' added code tags after I pointed out the problem with the display of the code.

« Last Edit: 04-03-2014, 22:23:01 by brainkj007 »

brainkj007:
I tried doing that but it doesn't like to work that way .

 // suggestions to fix this added

#include <SoftwareSerial.h>

/* do you ever use these? they have no connection to the later declared sensor[] array.
#define sensor0 3
#define sensor1 4
#define sensor2 5
#define sensor3 6
#define sensor4 7
#define sensor5 8
#define sensor6 9
#define sensor7 10
#define sensor8 11
#define sensor9 12
#define sensor10 13
#define sensor11 A5
#define sensor12 2  // octave changer
#define sensor13 A4 // octave changer
*/
#define cmd 0x90 // command control code for midi  sets midi for channel one, note on
#define velocity 0x7E  //volume control code for midi   set to highest volume
int note;
byte sensor[14] = { 3,4,5,6,7,8,9,10,11,12,13,A5,2,A4 }; // try that for starts
int laser[12]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B};  //-1 octave note values
int sensor_value[14];
int octal[]={0x00, 0xC,0x18,0x24,0x30,0x3C, 0x48, 0x54, 0x60,0x6C, 0x78};  // the change in octave
int octave = 6;
void setup(){
 Serial.begin(57600); // midi runs at 31250 but another program requires 57600 to deal with delay when changing serialtomidi
for(int i=0; i<14; i++)
{
 pinMode (sensor[i], INPUT);

}
}
void loop(){
sensor_value[12]= digitalRead(sensor[12]);
if(sensor_value[12] == HIGH){
    octave++;}
    sensor_value[13]= digitalRead(sensor[13]);
    if(sensor_value[13] == HIGH){
      octave--;}

for(int s=0; s<12; s++)
 {
   sensor_value[s]=digitalRead(sensor[s]);
   if(sensor_value[s] == HIGH){
      note = laser[s];
      note = note + octal[octave];
     Serial.write(cmd);
 Serial.write(note);
 Serial.write(velocity);
 delay(100);
      }
     
}
 }





this is the other code I am working on to get this code to do the same thing but without writing 884 lines of code but it doesn't want to change octaves at all but it does do a little bit of the music.

It is a move in a better direction.

The code works fine now after i made that change as to what i want the octal to look at.

There are basic tools and techniques you could really benefit from learning, just in time saved.
Arrays and loops are just one tool, have you tried enums?
Do you know switch-case and the 2 different while loops?
if-else if-else if-else?
logical bit manipulation?