-1

I have added static variables to this code and instead of getting repeated notes whilst dwelling on green, I get a single note instead. This is what I wanted. However, my loop isn't allowing for a new note to be fired either when the sensor returns to a green colour. I'm afraid I'm not getting the hang of this!

  /*
  // TCS230 color recognition sensor
  // Sensor connection pins to Arduino are shown in comments

  Color Sensor      Arduino
  -----------      --------
  VCC               5V
  GND               GND
  s0                8
  s1                9
  s2                12
  s3                11
  OUT               10
  OE                GND
*/
#include <MIDI.h>

const int s0 = 8;
const int s1 = 9;
const int s2 = 12;
const int s3 = 11;
const int out = 10;

// LED pins connected to Arduino
int redLed = 5;
int greenLed = 6;
int blueLed = 7;
// Variables
int red = 0;
int green = 0;
int blue = 0;
int indeks = 0;
const int rgbr = 7;
const int rgbg = 6;
const int rgbb = 5;

MIDI_CREATE_DEFAULT_INSTANCE();

void setup()
{
  MIDI.begin();                      // Launch MIDI and listen to channel 4

  Serial.begin(9600);
  pinMode(s0, OUTPUT);
  pinMode(rgbr, OUTPUT);
  pinMode(rgbg, OUTPUT);
  pinMode(rgbb, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(out, INPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);
}

void loop()
{
  color();

  int tmpr = (((20 - red) - 10) * 30); //int tmpr=(((20-red)-10)*30);
  int tmpg = (((20 - green) - 10) * 30); //int tmpg=(((20-green)-10)*30);
  int tmpb = (((20 - blue) - 10) * 30); //  int tmpb=(((20-blue)-10)*30);

  if (tmpr < 0)
    tmpr = 0;

  if (tmpg < 0)
    tmpg = 0;

  if (tmpb < 0)
    tmpb = 0;

  if (tmpg > tmpb && tmpg > tmpr)
  { tmpb = 0;
    tmpr = 0;
  }
  analogWrite(rgbr, tmpr );
  analogWrite(rgbg, tmpg );
  analogWrite(rgbb, tmpb );

  static uint8_t noteOn;

  if (tmpg > 149 && tmpg < 151) {
    static uint8_t noteOn = 0;
    if (!noteOn) {
      noteOn = true;
      MIDI.sendNoteOn(79, 127, 1);    // Send a Note (pitch 79, velo 127 on channel 1)
      MIDI.sendControlChange(64, 127, 1);

    }
  }
  else {
    static uint8_t noteOn = 1;
    if (noteOn) {
      noteOn = false;
      MIDI.sendNoteOff(79, 0, 1);     // Stop the note
      MIDI.sendControlChange(64, 0, 1);

    }
  }
}

void color()
{
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  //count OUT, pRed, RED


  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);

  digitalWrite(s3, HIGH);
  //count OUT, pBLUE, BLUE
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);
  //count OUT, pGreen, GREEN
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);

}
  • 1
    please include your attempts, so that we can see in which area you need help – jsotola Oct 5 '18 at 17:14
  • Hello, I've replaced my code with new suggestions but still to no avail... – robbie Oct 5 '18 at 19:03
  • please format your code correctly ..... highlight all of the code text and click {} button – jsotola Oct 6 '18 at 1:42
  • you are keeping track of only one note (any note) – jsotola Oct 6 '18 at 1:52
  • problem is, that I don't know how to keep track of the note that I have – robbie Oct 6 '18 at 10:53
2

Because the MIDI library for Arduino does not keep track of which notes are on or off, you will have to do that yourself.

Add a static variable called noteOn that will hold a 0 or a 1, keeping track of whether you have a note turned on or not.

Then, when your loop() determines that it should turn on a note, it first checks if a note is already on. If the note is not already on (!noteOn), then send note on command, and set noteOn=true (same as noteOn=1) so that next time around, you won't send the note on command again.

void loop()
{
  static uint8_t noteOn;

  [...]

  if (tmpg > 149 && tmpg < 151) {
    if (!noteOn) {
      noteOn = true;
      MIDI.sendNoteOn(79, 127, 1);    // Send a Note (pitch 79, velo 127 on channel 1)
      MIDI.sendControlChange(64, 127, 1);
    }
  } else {
    if (noteOn) {
      noteOn = false;
      MIDI.sendNoteOff(79, 0, 1);     // Stop the note
      MIDI.sendControlChange(64, 0, 1);
    }
  }
}
  • Thanks jose can u c! Can you tell me what this means ( [...]) Also, what do you mean by keeping track of which notes are on or off? I have tried you code but unfortunately i still have the same problem of repeated notes... – robbie Oct 5 '18 at 18:35
  • [...] is just where I omitted some of your code that was unrelated to the relevant additions to answer the question. – jose can u c Oct 5 '18 at 18:43
  • I edited the answer to have a more explanation of what I mean by keeping track of which notes are on or off. – jose can u c Oct 5 '18 at 18:46
  • I thought I should attach my code with your additional code but i don't understand how or where I should post that! – robbie Oct 5 '18 at 18:51
  • Possibly should I edit my original post and replace it with the new code? – robbie Oct 5 '18 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.