using analog sensors to work as digital? &more

Hey I'm making a basic midi controller for my friend's intallation and I have a few questions.
First, I'll show you guys the sketch and the code I've made so far:

(sketches might be attached on below of this article!)

int switchPin1 = A0;      
int midi = 1;



char note1 = 60; //C
char note2 = 62; //D



int switchState1 = LOW;
int currentSwitchState1 = LOW;


void setup() {
         

  pinMode(switchPin1, INPUT);
  pinMode(midi,OUTPUT);


  Serial.begin(31250); 
}

void loop() {

  int pitch = analogRead(0);
  currentSwitchState1 = digitalRead(switchPin1);
  if( currentSwitchState1 == LOW && switchState1 == HIGH ) // push
    noteOn(0x90, note1, 0x45);
    delay(100);
  if( currentSwitchState1 == HIGH && switchState1 == LOW ) // release
    noteOn(0x90, note2, 0x45);
    delay(100);
  switchState1 = currentSwitchState1;
}


void noteOn(char cmd, char pitch, char velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

We're trying to play music automatically in ableton live when we touch the sensors.
We'll map midi keys with Play button to middle C(note1) and Pause button to D(note2)
So, we're trying to make the musics play when someone's touching installation and goes off when they take their hands off.

the question starts:

  1. is it possible to gather 3 FSRs in one analog input and make them to work as one sensor with the sketch and the code that I've made??

  2. currentswitchstate and switchstate starts in LOW status. and then be goes to high status. Do you guys know any solution to make analog sensors work in digital way? For example, if we set analog sensor state to "0~1023", like analoginoutserial, I want to make it digitally on in its 100 status and goes off for below of that. i would really be perfect to understand the solution if anyone knows how to solve it in map.

  3. when the digital status of this sensor goes HIGH from LOW, it'll start to work as middle C note. but I want it to stop automatically after 100 miliseconds so I can make the play button to work clearly. Also, when his sensor goes LOW from HIGH, it'll work as middle D note, and automatically stop after milliseconds. But that does not mean the program's gonna ignore the energy what sensor's getting, right? How can I make it automatically stop? is it OK to just use delay(100);?

  4. if anything looks weird in midi codes, please point it out. I'll love you guys for just reading this long post. thank you!

??.JPG

First, I'll show you guys the sketch and the code I've made so far

Posted incorrectly. There is a sticky at the top of this forum. I suggest that you take the trouble to read it, if you want us to take the trouble to read your post.

is it possible to gather 3 FSRs in one analog input

If you don't care which one was pressed, and you only ever press one at a time, yes.

make them to work as one sensor

Depends on what this means.

Do you guys know any solution to make analog sensors work in digital way?

Like on or off? How would that work. Is pressed a little on or off? Is pressed moderately on or off. Is pressed hard on or off? I'll bet that the series of questions gives you a clue.

when the digital status of this sensor goes HIGH from LOW

Of what sensor?

but I want it to stop automatically after 100 miliseconds

It? What is it? Stop doing what? You can't force an input to change state. You could pretend that it has, and ignore the fact that it really hasn't.

is it OK to just use delay(100);?

Won't bother me. But, I'm sure that it will bother you.

Please read the sticky at the top of this section - especially the part about how to post code.

Janbanlee:

  1. is it possible to gather 3 FSRs in one analog input and make them to work as one sensor with the sketch and the code that I've made??

If you only want the three FSRs to act as a single conceptual input (i.e. it doesn't matter which one you press) then yes, you can connect them in series to achieve that. If you want to be able to tell which FSR has been pressed then you would need to connect them to separate inputs and read them separately.

Janbanlee:
2. currentswitchstate and switchstate starts in LOW status. and then be goes to high status. Do you guys know any solution to make analog sensors work in digital way? For example, if we set analog sensor state to "0~1023", like analoginoutserial, I want to make it digitally on in its 100 status and goes off for below of that. i would really be perfect to understand the solution if anyone knows how to solve it in map.

The analogRead gives you a number in the range 0 - 1023. You can compare that to a threshold to convert that to a simple active/inactive status e.g.

if(analogRead(FSR_PIN) > 100)
{
    // it's active
}
else
{
    // it's inactive
}

Janbanlee:
3. when the digital status of this sensor goes HIGH from LOW, it'll start to work as middle C note. but I want it to stop automatically after 100 miliseconds so I can make the play button to work clearly. Also, when his sensor goes LOW from HIGH, it'll work as middle D note, and automatically stop after milliseconds. But that does not mean the program's gonna ignore the energy what sensor's getting, right? How can I make it automatically stop? is it OK to just use delay(100);?

Using delay() would work, but would mean that your sketch can't do anything else (such as responding to other input events) while the delay completes. That might be acceptable in very simple cases but usually it's better to perform timed actions asynchronously so that your sketch can continue to do other things. The blink without delay example sketch demonstrates how to do that.

Hello pals,
Thank you for lots of opinions and troubleshooting.
I've been using boolean code and if/else code but still having some troubles.

first, this it the code I made w one of my friend.

int switchPin1= A0;
int switchPin2= A1;
int switchPin3= A2;
int midi = 1;

char note1 = 60 ;
boolean running = false;

int threshold = 100; // sensor threshold


void setup()
{
  pinMode(midi, OUTPUT); //midi port
  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);
  pinMode(switchPin3, INPUT);
  Serial.begin(31250);
}

void loop()
{
  // check sensor Value
  if( switchPin1 >= threshold || switchPin2 >= threshold || switchPin3 >= threshold)
  {
    running = true;
  }

  else{
    running = false;
  }

  // action
  if (running)
    noteOn();
  else
    noteOff();
}

void noteOn() {
  Serial.write(0x90);
  Serial.write(note1);
  Serial.write(0x45);
}


void noteOff() {
  Serial.write(0x90);
  Serial.write(note1);
  Serial.write(0x00);
}

it did not work on midi, by midimonitor it didn't show any keys and the midi cable itself is not responding.
so I thought maybe I should check analog inputs, so I made the source that if 1 of any 3 analog sensors is getting energy, an LED pin would work as a digital output.

const int sensorPin1 = A0;
const int sensorPin2 = A1;
const int sensorPin3 = A2;
int ledPin = 13;
int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
boolean running = false;
int threshold = 100;

void setup() {
pinMode(ledPin, OUTPUT);
analogRead(sensorPin1 == 0);
analogRead(sensorPin2 == 0);
analogRead(sensorPin3 == 0);
  Serial.begin(9600); 
}

void loop() {
  if( analogRead(sensorPin1) >= threshold || analogRead(sensorPin2) >= threshold || analogRead(sensorPin3) >= threshold)
  {
  digitalWrite(ledPin, HIGH);
  }

  else{
    digitalWrite(ledPin, LOW);
  }
}

if I touch any 1 of these sensors, the LED lights on, but it goes immediately although I'm still touching it. guess something's wrong with digitalWrite...? Also, the LED does not light on quite bright.
At this source, not moves without analogRead(sensorPin1 == 0); and analogRead(sensorPin1) >= threshold so I thought the trouble I've been making's caused by analogRead, so I re-wrote the source on the top.

int switchPin1= A0; 
int switchPin2= A1; 
int switchPin3= A2;
int midi = 1;
int threshold = 100;
char note1 = 60;


boolean running = false;

void setup()
{
  pinMode(midi, OUTPUT);
  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);
  pinMode(switchPin3, INPUT);
  analogRead(switchPin1 == 0);
  analogRead(switchPin2 == 0);
  analogRead(switchPin3 == 0);
  Serial.begin(31250);
}

void loop()
{
  if( analogRead(switchPin1) >= threshold || analogRead(switchPin2) >= threshold || analogRead(switchPin3) >= threshold)
  {
    noteOn(0x90, note1, 0x45);
  }
  else
  {
    noteOn(0x90, note1, 0x00);
  }
}
  
  void noteOn(char cmd, char pitch, char velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

I also made the source without boolean.

int switchPin1 = A0;      
int switchPin2 = A1;
int switchPin3 = A1;
int midi = 1;


char note1 = 60;


int switchState1 = LOW;
int switchState2 = LOW;
int switchState3 = LOW;

int currentSwitchState1 = LOW;
int currentSwitchState2 = LOW;
int currentSwitchState3 = LOW;



void setup() {

  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);
  pinMode(switchPin3, INPUT);
  
  pinMode(midi, OUTPUT);
  
  Serial.begin(31250); 


}

void loop() {
  currentSwitchState1 = analogRead(switchPin1);
  currentSwitchState2 = analogRead(switchPin2);
  currentSwitchState3 = analogRead(switchPin3);
  
  if( currentSwitchState1 >= 100 || currentSwitchState2 >= 100 || currentSwitchState3 >=100 ) // push
    noteOn(0x90, note1, 0x45); 
  if( currentSwitchState1 == 0 && currentSwitchState2 == 0 && currentSwitchState3 == 0) // release
    noteOn(0x90, note1, 0x00);
  switchState1 = currentSwitchState1;
  switchState2 = currentSwitchState2;
  switchState3 = currentSwitchState3;

}

void noteOn(char cmd, char pitch, char velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

both this source did not work. Also I'm thinking about something's wrong with the second code because I've setted switchState1 = LOW; and currentSwitchState1 = LOW;, those actually have to move as analog, right?
how can I set the state of the pin at the start as 0, and the current state of the pin as 0 in analog?

the both two codes that I've made---- wonder what's wrong with midi. also the Inputs. I'll attach the sketch again for the circuits and try again with the sources.

feel free to say anything about the sources. thank you again!!

You still have not read the post that is at the top of this section on how to post code --> How to use this forum - please read - Website and Forum - Arduino Forum (you can edit your earlier entries to fix it.). Sorry, to difficult to read the code as it is now.

Code tags added by moderator.

if( currentSwitchState1 == 100 || currentSwitchState2 == 100 || currentSwitchState3 ==100 )

Have you any reason to suppose that any of the SwitchState variables have a value of exactly 100 when a switch is pressed ? I suggest that you print them before testing them so that you can see the actual values.

Incidentally, the code above is in code tags which is what you have been asked to do but have not done.

thank you for the code tag! I modified my article. Now I know how to make the sources look clean!

UKHeliBob:

if( currentSwitchState1 == 100 || currentSwitchState2 == 100 || currentSwitchState3 ==100 )

Have you any reason to suppose that any of the SwitchState variables have a value of exactly 100 when a switch is pressed ? I suggest that you print them before testing them so that you can see the actual values.

Incidentally, the code above is in code tags which is what you have been asked to do but have not done.

I found my error at this code right now I'm sorry!! that should be :

if( currentSwitchState1 >= 100 || currentSwitchState2 >= 100 || currentSwitchState3 >=100 )

I should've setted as any 1 of 3 sensors is being touched more than 100 not exatcly 100.