Show Posts
Pages: 1 2 [3] 4 5 ... 8
31  Forum 2005-2010 (read only) / Syntax & Programs / Re: slowing down a rotary encoder data output on: September 30, 2010, 04:27:35 am
thanks Korman - that did it. the only problem i face now is that the counter does not reset back to zero when 127 is reached. this used to happen when i used the other type of counter, but not anymore. the video files being controlled by the crank loop seamlessly and i wanted to allow the user to turn the crank endlessly and wind the video loop on, being able to speed up and slow down the playback as they wish. do you have any suggestion how i could make the code loop back to 0 once it gets to 127?

thanks again
32  Forum 2005-2010 (read only) / Syntax & Programs / Re: slowing down a rotary encoder data output on: September 29, 2010, 09:12:10 am
re: Paul
the counter does not exceed 255 - once it gets to 255 it goes back to 0 and just keeps on looping through this cycle. As for the incorrect use of the metro function, i was not really sure how to use it.

re: Korman
thank you for the suggested code. the printing_byte is what is fed out over midi to the computer  and that is mapped in the software to the playback head of the video. your code did exactly what i was looking for, thank you. however it only counts up as far as 63 - which is basically half way. the speed is perfect, but i am not sure how to make it count all the way up to 127. this is important as 0 is the beginning of the video and 127 is the end (in midi cc values range between 0 and 127)
33  Forum 2005-2010 (read only) / Syntax & Programs / slowing down a rotary encoder data output on: September 29, 2010, 06:16:33 am
hello

i am in the middle of finishing a project which uses a shaft rotary encoder to control video playback and a sequence of strobes. the rotary encoder is being driven by a wooden crank which the public is encouraged to turn. The rotary encoder data is converted to midi and that is fed into a software to control the video parameters - creating the effect that the person is winding on the video as if though it is an old projector.  It all works great but i just want to slow down the data coming out of the rotary encoder a bit, at the moment it is turning about 4 times quicker than i want it to (due to the nature of the wooden crank and handle used to drive it). i tried putting in a delay but that gave very jerky results, a friend also recommended the metro library but i am not fully sure how to implement that - was hoping someone might have some suggestions or opinions.
thanks..

here is the code as it is at the moment:
Code:
#include <Metro.h>

/* Rotary encoder read example */

#define ENC_A 14
#define ENC_B 15
#define ENC_PORT PINC
 byte printing_byte = 0;
 byte incomingByte;
byte note;
byte velocity;
int pot;

int strobe1 = 3;
int strobe2 = 4;
int strobe3 = 5;
int strobe4 = 6;
byte byte1;
byte byte2;
byte byte3;

Metro serialMetro = Metro(1000);

void setup()
{
  /* Setup encoder pins as inputs */
  pinMode(ENC_A, INPUT);
  digitalWrite(ENC_A, HIGH);
  pinMode(ENC_B, INPUT);
  digitalWrite(ENC_B, HIGH);
  pinMode(strobe1, OUTPUT);
  pinMode(strobe2, OUTPUT);
  pinMode(strobe3, OUTPUT);
  pinMode(strobe4, OUTPUT);
  Serial.begin (31250);

}
 
void loop()
{
 static uint8_t counter = 0;      //this variable will be changed by encoder input      
 int8_t tmpdata;
 /**/
  tmpdata = read_encoder();
  if( tmpdata ) {
    counter += tmpdata;
    
    printing_byte = map(counter, 0, 255, 0, 127);
    if (serialMetro.check() == 1);
    Midi_Send(0xB0, 7, printing_byte);
  if((printing_byte >= 0) && (printing_byte < 3)){
    digitalWrite(strobe1, HIGH);
  }
    else{
      digitalWrite(strobe1, LOW);
    }
    
     if((printing_byte >= 30) && (printing_byte < 33)){
    digitalWrite(strobe2, HIGH);
  }
    else{
      digitalWrite(strobe2, LOW);
    }
    
     if((printing_byte >= 60) && (printing_byte < 63)){
    digitalWrite(strobe3, HIGH);
  }
    else{
      digitalWrite(strobe3, LOW);
    }
      if((printing_byte >= 90) && (printing_byte < 93)){
    digitalWrite(strobe4, HIGH);
  }
    else{
      digitalWrite(strobe4, LOW);
    }
}
  
}
 
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
  int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
  static uint8_t old_AB = 0;
  /**/
  old_AB <<= 2;                   //remember previous state
  old_AB |= ( ENC_PORT & 0x03 );  //add current state
  return ( enc_states[( old_AB & 0x0f )]);  
  int8_t tmpdata;
 
}

void Midi_Send(byte cmd, byte data1, byte data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);

}
34  Forum 2005-2010 (read only) / Syntax & Programs / Re: help with rotary shaft encoder midi input on: September 20, 2010, 09:29:01 am
never mind - managed to get it to work - was sending too much information over serial.

Code:
/* Rotary encoder read example */

#define ENC_A 14
#define ENC_B 15
#define ENC_PORT PINC
 byte printing_byte = 0;
 byte incomingByte;
byte note;
byte velocity;
int pot;

byte byte1;
byte byte2;
byte byte3;
void setup()
{
  /* Setup encoder pins as inputs */
  pinMode(ENC_A, INPUT);
  digitalWrite(ENC_A, HIGH);
  pinMode(ENC_B, INPUT);
  digitalWrite(ENC_B, HIGH);
  Serial.begin (31250);

}
 
void loop()
{
 static uint8_t counter = 0;      //this variable will be changed by encoder input      
 int8_t tmpdata;
 /**/
  tmpdata = read_encoder();
  if( tmpdata ) {
    counter += tmpdata;
    printing_byte = map(counter, 0, 1020, 0, 127);
    Midi_Send(0xB0, 7, printing_byte);
  }
}
 
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
  int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
  static uint8_t old_AB = 0;
  /**/
  old_AB <<= 2;                   //remember previous state
  old_AB |= ( ENC_PORT & 0x03 );  //add current state
  return ( enc_states[( old_AB & 0x0f )]);  
  int8_t tmpdata;
 
}

void Midi_Send(byte cmd, byte data1, byte data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);

}
35  Forum 2005-2010 (read only) / Syntax & Programs / help with rotary shaft encoder midi input on: September 20, 2010, 04:49:02 am
hello

i am in the process of building a wooden crank for winding on a video file - a bit like controlling the playhead of the video file but just having a big wooden handle for it (this will be attached to a pair of rollers across which the video will be wound on). I bought a simple rotary encoder with a fairly low resolution but it will do. it counts from 0 to 255 which is great because the way i plan to control the video is through a program called Modul8 and using midi. the playhead is a variable parameter in modul8 that can be mapped to a midi CC value.
I have been playing aroudn with some code I found and bits i have written before, and at the moment the code outputs a count and i can see that it is sending it over MIDI (my midi to usb interface shows an led pulse that matches the rate at which i turn the encoder) however i am not actually receiving any midi into my computer. does anyone have any suggestions of where i might be going wrong?
all help is greatly appreciated
Code:
/* Rotary encoder read example */
#define ENC_A 14
#define ENC_B 15
#define ENC_PORT PINC
 byte printing_byte = 0;
 byte incomingByte;
byte note;
byte velocity;
int pot;

byte byte1;
byte byte2;
byte byte3;
void setup()
{
  /* Setup encoder pins as inputs */
  pinMode(ENC_A, INPUT);
  digitalWrite(ENC_A, HIGH);
  pinMode(ENC_B, INPUT);
  digitalWrite(ENC_B, HIGH);
  Serial.begin (31250);

}
 
void loop()
{
 static uint8_t counter = 0;      //this variable will be changed by encoder input      
 int8_t tmpdata;
 /**/
  tmpdata = read_encoder();
  if( tmpdata ) {
    Serial.print("Counter value: ");
    Serial.println(counter, DEC);
    counter += tmpdata;
  }
}
 
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
  int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
  static uint8_t old_AB = 0;
  /**/
  old_AB <<= 2;                   //remember previous state
  old_AB |= ( ENC_PORT & 0x03 );  //add current state
  return ( enc_states[( old_AB & 0x0f )]);  
  int8_t tmpdata;
  tmpdata = read_encoder();
  printing_byte = map(tmpdata, 128, 255, 0, 127);{
  Midi_Send(0xB0, 7, printing_byte);
  }
}

void Midi_Send(byte cmd, byte data1, byte data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);

}
36  Forum 2005-2010 (read only) / Syntax & Programs / help with multiple MAX7219 chips on: June 04, 2010, 07:26:19 am
hello

i am in the process of putting together a 5 x 5 x 5 led matrix. i am using 2 max7219 chips to control the led's but have run into some trouble with the code. i was hoping to just have two matrix's running alongside each other controlled over midi - so that notes 1-64 control the first matrix and then 65-125 control the second. but it doesn't want to work. has anyone had much experience with using multiple max 7219 chips? any thoughts would be really appreciated.

thanks

(here is the code I have so far)
Code:
#include <Sprite.h>
#include <Matrix.h>
#include <MIDI.h>

Matrix myMatrix = Matrix(4, 3, 2);
Matrix newMatrix = Matrix(7, 6, 5);


int midiNote;
int x;
int y;



void setup() {
  MIDI.begin();
  myMatrix.clear();
  newMatrix.clear();

}

void loop() {
  if (MIDI.read()) {
    midiNote = MIDI.getData1();
    // if the midiNote is 64 or under send it to the led driver
    if (midiNote <= 128) {
      convertMidiToCoordinates(midiNote);

      switch (MIDI.getType()) {
        case NoteOn:
          myMatrix.write(x, y, HIGH);
          break;
        case NoteOff:
          myMatrix.write(x, y, LOW);
          break;
      }
    } else {
      midiNote -= 64;
      convertMidiToCoordinates(midiNote);

      switch (MIDI.getType()) {
        case NoteOn:
          newMatrix.write(x, y, HIGH);
          break;
        case NoteOff:
          newMatrix.write(x, y, LOW);
          break;
      }
    }
  }
 }

void convertMidiToCoordinates(int midiNote) {
  x = midiNote / 8;
  y = midiNote % 8;
}
37  Forum 2005-2010 (read only) / Syntax & Programs / PING / MIDI delay_time issues on: September 23, 2009, 06:24:41 pm
hello

I have been trying to solve a problem regarding having my arduino code read a PING ultrasonic sensor and also output a midiOUT signal. The code I have managed to cobble together at the moment does not read the PING sensor quickly enough to give an accurate read out - and when I have it reading the sensor accurately it does not output midi -
anyone have any thoughts?
It is for a project which has to be up and running on the 26th so really working against the clock here!
any help would be eternally appreciated.

the code below - outputs MIDI but I have not figured out any way of putting the delay_time outside of the loop and hence the whole thing seems to take too long.


Code:
/* New Variables */
int delay_time = 10; // delay for this amount each write cycle.
byte channel = 1;

byte printing_byte = 0;

/* Ultrasound Sensor
 *------------------
 *
 * Reads values (00014-01199) from an ultrasound sensor (3m sensor)
 * and writes the values to the serialport.
 *
 * http://www.xlab.se | http://www.0j0.org
 * copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
 *
 */

 // CODE EDITED BY SEBASTIAN TOMCZAK 23 APRIL 2008
const int pingPin = 12; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0;
int brightness = 0;// Echo counter
int ledPin1 =  8;
int ledPin2 =  9;
int ledPin3 =  10;
int ledPin4 =  11;
int ledPin5 = 13;
int whitePin = 2;
const int fan =  7;
int light1 = 6;
int light2 = 5;
int light3 = 4;
int light4 = 3;

int fanState = LOW;             // ledState used to set the LED
long previousMillis = 0;  

long interval = 5000;

int ledPin1State = LOW;             // ledState used to set the LED
long previousMillis1 = 10;  

long interval1 = 60;

int ledPin2State = LOW;             // ledState used to set the LED
long previousMillis2 = 20;  

long interval2 = 60;

int ledPin3State = LOW;             // ledState used to set the LED
long previousMillis3 = 30;  

long interval3 = 80;

int ledPin4State = LOW;             // ledState used to set the LED
long previousMillis4 = 10;  

long interval4 = 80;// LED connected to digital pin 13

void setup() {
  Serial.begin(31250);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);  
  pinMode(ledPin3, OUTPUT);  
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
  pinMode(light4, OUTPUT);
  pinMode(light3, OUTPUT);
  pinMode(whitePin, OUTPUT);            // Sets the digital pin as output

}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  /* Writing out values to the serial port
   * -------------------------------------------------------------------
   */

  
 // Append echo pulse time to ultrasoundValue
  if((inches >= 15) && (inches < 20)){
     midiOUT(0x90, 38, 127);
    
    digitalWrite(light3, HIGH);
    delay(1000);
    digitalWrite(light3, LOW);
    midiOUT(0x90, 39, 127);
    digitalWrite(light4, HIGH);
    delay(3000);
    digitalWrite(light4, LOW);
 }
 
 else{
  digitalWrite(light3, LOW);
  digitalWrite(light4, LOW);
 }
 
 
 if((inches >= 60) && (inches < 66)){
    midiOUT(0x90, 37, 127);
    digitalWrite(light2, HIGH);
    delay(1000);
    digitalWrite(light2, LOW);
 }
 
 else{
  digitalWrite(light2, LOW);
 }
 
  if((inches >= 93) && (inches < 128)){
    midiOUT(0x90, 36, 127);
    digitalWrite(light1, HIGH);
    delay(1000);
    digitalWrite(light1, LOW);
 }
 
 else{
  digitalWrite(light1, LOW);
 }
 
               // wait for a second;

if (millis() - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = millis();  

    // if the LED is off turn it on and vice-versa:
    if (fanState == LOW)
      fanState = HIGH;
    else
      fanState = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(fan, fanState);
  }
  
  if (millis() - previousMillis1 > interval1) {
    // save the last time you blinked the LED
    previousMillis1 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin1State == LOW)
      ledPin1State = HIGH;
    else
      ledPin1State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin1, ledPin1State);
  }
  
  if (millis() - previousMillis2 - 20 > interval2) {
    // save the last time you blinked the LED
    previousMillis2 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin2State == LOW)
      ledPin2State = HIGH;
    else
      ledPin2State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin2, ledPin2State);
  }
  
  if (millis() - previousMillis3 - 30 > interval3) {
    // save the last time you blinked the LED
    previousMillis3 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin3State == LOW)
      ledPin3State = HIGH;
    else
      ledPin3State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin3, ledPin3State);
  }

 if (millis() - previousMillis4 - 10 > interval4) {
    // save the last time you blinked the LED
    previousMillis4 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin4State == LOW)
      ledPin4State = HIGH;
    else
      ledPin4State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin4, ledPin4State);
  }
delay(40);
}

  /* BEGIN EDITED CODE */

void midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);}
 

  /* END EDITED CODE */

  /* Lite up LED if any value is passed by the echo pulse
   * -------------------------------------------------------------------
   */

  /* Delay of program
   * -------------------------------------------------------------------
   */





long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
38  Forum 2005-2010 (read only) / Syntax & Programs / Re: midi error syntax issues on: September 22, 2009, 05:57:07 pm
where as before I had it looking like this:


Code:
oid midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);}


  /* END EDITED CODE */

  /* Lite up LED if any value is passed by the echo pulse
   * -------------------------------------------------------------------
   */

  /* Delay of program
   * -------------------------------------------------------------------
   */

  delay(delay_time);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
39  Forum 2005-2010 (read only) / Syntax & Programs / Re: midi error syntax issues on: September 22, 2009, 05:56:08 pm
hello -
tried the code out in situ today and there is an issue with the delay - it seems the way I have had to place the delay back into the code rather than being after serialPrint the PING sensor is not working correctly and is not getting a read out fast enough to work.

Below is the way code I have at the moment:


Code:
/* New Variables */
int delay_time = 10; // delay for this amount each write cycle.
byte channel = 1;

byte printing_byte = 0;

/* Ultrasound Sensor
 *------------------
 *
 * Reads values (00014-01199) from an ultrasound sensor (3m sensor)
 * and writes the values to the serialport.
 *
 * http://www.xlab.se | http://www.0j0.org
 * copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
 *
 */

 // CODE EDITED BY SEBASTIAN TOMCZAK 23 APRIL 2008
const int pingPin = 12; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0;
int brightness = 0;// Echo counter
int ledPin1 =  8;
int ledPin2 =  9;
int ledPin3 =  10;
int ledPin4 =  11;
int ledPin5 = 13;
int whitePin = 2;
const int fan =  7;
int light1 = 6;
int light2 = 5;
int light3 = 4;
int light4 = 3;

int fanState = LOW;             // ledState used to set the LED
long previousMillis = 0;  

long interval = 5000;

int ledPin1State = LOW;             // ledState used to set the LED
long previousMillis1 = 10;  

long interval1 = 60;

int ledPin2State = LOW;             // ledState used to set the LED
long previousMillis2 = 20;  

long interval2 = 60;

int ledPin3State = LOW;             // ledState used to set the LED
long previousMillis3 = 30;  

long interval3 = 80;

int ledPin4State = LOW;             // ledState used to set the LED
long previousMillis4 = 10;  

long interval4 = 80;// LED connected to digital pin 13

void setup() {
  Serial.begin(31250);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);  
  pinMode(ledPin3, OUTPUT);  
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
  pinMode(light4, OUTPUT);
  pinMode(light3, OUTPUT);
  pinMode(whitePin, OUTPUT);            // Sets the digital pin as output

}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  /* Writing out values to the serial port
   * -------------------------------------------------------------------
   */

  
 // Append echo pulse time to ultrasoundValue
  if((inches >= 15) && (inches < 20)){
     midiOUT(0x90, 38, 127);
    
    digitalWrite(light3, HIGH);
    delay(1000);
    digitalWrite(light3, LOW);
    midiOUT(0x90, 39, 127);
    digitalWrite(light4, HIGH);
    delay(3000);
    digitalWrite(light4, LOW);
 }
 
 else{
  digitalWrite(light3, LOW);
  digitalWrite(light4, LOW);
 }
 
 
 if((inches >= 60) && (inches < 66)){
    midiOUT(0x90, 37, 127);
    digitalWrite(light2, HIGH);
    delay(1000);
    digitalWrite(light2, LOW);
 }
 
 else{
  digitalWrite(light2, LOW);
 }
 
  if((inches >= 93) && (inches < 128)){
    midiOUT(0x90, 36, 127);
    digitalWrite(light1, HIGH);
    delay(1000);
    digitalWrite(light1, LOW);
 }
 
 else{
  digitalWrite(light1, LOW);
 }
 
               // wait for a second;

if (millis() - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = millis();  

    // if the LED is off turn it on and vice-versa:
    if (fanState == LOW)
      fanState = HIGH;
    else
      fanState = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(fan, fanState);
  }
  
  if (millis() - previousMillis1 > interval1) {
    // save the last time you blinked the LED
    previousMillis1 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin1State == LOW)
      ledPin1State = HIGH;
    else
      ledPin1State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin1, ledPin1State);
  }
  
  if (millis() - previousMillis2 - 20 > interval2) {
    // save the last time you blinked the LED
    previousMillis2 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin2State == LOW)
      ledPin2State = HIGH;
    else
      ledPin2State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin2, ledPin2State);
  }
  
  if (millis() - previousMillis3 - 30 > interval3) {
    // save the last time you blinked the LED
    previousMillis3 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin3State == LOW)
      ledPin3State = HIGH;
    else
      ledPin3State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin3, ledPin3State);
  }

 if (millis() - previousMillis4 - 10 > interval4) {
    // save the last time you blinked the LED
    previousMillis4 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin4State == LOW)
      ledPin4State = HIGH;
    else
      ledPin4State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin4, ledPin4State);
  }
delay(40);
}

  /* BEGIN EDITED CODE */

void midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);}
 

  /* END EDITED CODE */

  /* Lite up LED if any value is passed by the echo pulse
   * -------------------------------------------------------------------
   */

  /* Delay of program
   * -------------------------------------------------------------------
   */





long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

as you can see the delay is now inside the code before the Serial.print, where as before I had it after the Serial.print which gave an accurate read out but meant that the midiOUT did not work.
any suggestions or help would be greatly appreciated.
40  Forum 2005-2010 (read only) / Syntax & Programs / Re: midi error syntax issues on: September 21, 2009, 06:36:41 pm
that is some speedy responses and of course solved the problem.
It has been some long days and spotting a rogue } here and there is proving to be difficult.
thanks
41  Forum 2005-2010 (read only) / Syntax & Programs / Re: midi error syntax issues on: September 21, 2009, 05:57:10 pm
and here is an example of the same midiOUT method working on another code:
Code:
 /*
 * Optical Tachometer
 *
 * Uses an IR LED and IR phototransistor to implement an optical tachometer.
 * The IR LED is connected to pin 13 and ran continually. A status LED is connected
 * to pin 12. Pin 2 (interrupt 0) is connected across the IR detector.
 *
 *
 */
//#define DEBUG      

int ledPin = 13;                // IR LED connected to digital pin 13
int statusPin = 12;  // LED connected to digital pin 12
int lightPin1 = 6;
int lightPin2 = 9;
int lightPin3 = 10;
int lightPin4 = 11;
int brightness = 0;
int delay_time = 40; // delay for this amount each write cycle.
int noteDown = LOW;
byte MIDI_channel = 0;
byte cc_number = 0;
byte incomingByte;
byte button;
byte note;
byte printing_byte = 0;
int Value = 0;
int midi_pitch = 0;
int mappedrpm = 0;
int state=0;
#ifdef DEBUG
const int DEBUG_RATE = 9600;        // Serial debugging communicates at 9600 baud
const int SERIAL_PORT_RATE = DEBUG_RATE;
#else
const int MIDI_BAUD_RATE = 31250;   // MIDI communicates at 31250 baud
const int SERIAL_PORT_RATE = MIDI_BAUD_RATE;
#endif



volatile byte rpmcount;
volatile int status;

unsigned int rpm;

unsigned long timeold;

// flag switching stuff
boolean top_speed_reached = false;
int on_threshold = 120;
int off_threshold = 90;

void rpm_fun()
{
  //Each rotation, this interrupt function is run twice, so take that into consideration for
  //calculating RPM
  //Update count
  rpmcount++;

  //Toggle status LED  
  if (status == LOW) {
    status = HIGH;
  }
  else {
    status = LOW;
  }
  digitalWrite(statusPin, status);
}

void setup()
{
  state = 0;

  Serial.begin(SERIAL_PORT_RATE);
  //Interrupt 0 is digital pin 2, so that is where the IR detector is connected
  //Triggers on FALLING (change from HIGH to LOW)
  attachInterrupt(0, rpm_fun, FALLING);

  //Turn on IR LED
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);

  //Use statusPin to flash along with interrupts
  pinMode(statusPin, OUTPUT);

  rpmcount = 0;
  rpm = 0;
  timeold = 0;
  status = LOW;


}

void loop()
{
  //Update RPM every second
  delay(800);
  //Don't process interrupts during calculations
  detachInterrupt(0);
  //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
  //happened once per revolution instead of twice. Other multiples could be used
  //for multi-bladed propellers or fans
  rpm = 3*1000/(millis() - timeold)*rpmcount;
  timeold = millis();
  rpmcount = 0;
  attachInterrupt(0, rpm_fun, FALLING);
  brightness = map(rpm, 0, 50, 0, 255);
  analogWrite(lightPin1, brightness);
  printing_byte = map(rpm, 0, 50, 0, 127);{
  midiOUT(0xB0, 7, printing_byte);
  }

  if (!top_speed_reached) {
    if (printing_byte >= on_threshold) {
      top_speed_reached = true;
      midiOUT(0x90, 65, 127);
    }
  } else {
    if (printing_byte <= off_threshold) {
      top_speed_reached = false;
      midiOUT(0x90, 45, 127);
    }
  }
  
}




  //Write it out to serial port
  //Restart the interrupt processing
  


void midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);

}




42  Forum 2005-2010 (read only) / Syntax & Programs / midi error syntax issues on: September 21, 2009, 05:52:12 pm
hello
I am struggling with trying to finalise some code I am working on. I am using a PING sensor to trigger different midi notes depending on proximity (3 notes in total) and although the method I am using seems to work in other MIDI enabled arduino code i have used for some reason with this one I keep getting the following error message:


 In function 'void loop()':
error: 'midiOUT' was not declared in this scope

I have always used this method of midiOUT and I am stuck as to what I am doing wrong this time around. Any suggestions?


Code:
/* New Variables */
int delay_time = 30; // delay for this amount each write cycle.
byte channel = 1;

byte printing_byte = 0;

/* Ultrasound Sensor
 *------------------
 *
 * Reads values (00014-01199) from an ultrasound sensor (3m sensor)
 * and writes the values to the serialport.
 *
 * http://www.xlab.se | http://www.0j0.org
 * copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
 *
 */

 // CODE EDITED BY SEBASTIAN TOMCZAK 23 APRIL 2008
const int pingPin = 12;
int val = 0;
int ultrasoundValue = 0;
int timecount = 0;
int brightness = 0;// Echo counter
int ledPin1 =  8;
int ledPin2 =  9;
int ledPin3 =  10;
int ledPin4 =  11;
int ledPin5 = 13;
int whitePin = 2;
const int fan =  7;
int light1 = 6;
int light2 = 5;
int light3 = 4;
int light4 = 3;

int fanState = LOW;             // ledState used to set the LED
long previousMillis = 0;  

long interval = 5000;

int ledPin1State = LOW;             // ledState used to set the LED
long previousMillis1 = 10;  

long interval1 = 60;

int ledPin2State = LOW;             // ledState used to set the LED
long previousMillis2 = 20;  

long interval2 = 60;

int ledPin3State = LOW;             // ledState used to set the LED
long previousMillis3 = 30;  

long interval3 = 80;

int ledPin4State = LOW;             // ledState used to set the LED
long previousMillis4 = 10;  

long interval4 = 80;// LED connected to digital pin 13

void setup() {
  Serial.begin(31250);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);  
  pinMode(ledPin3, OUTPUT);  
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
  pinMode(light4, OUTPUT);
  pinMode(light3, OUTPUT);
  pinMode(whitePin, OUTPUT);            // Sets the digital pin as output

}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  /* Writing out values to the serial port
   * -------------------------------------------------------------------
   */

  
 // Append echo pulse time to ultrasoundValue
  if((inches >= 15) && (inches < 20)){
     midiOUT(0x90, 85, 127);
    
    digitalWrite(light3, HIGH);
    delay(1000);
    digitalWrite(light3, LOW);
    digitalWrite(light4, HIGH);
    delay(3000);
    digitalWrite(light4, LOW);
 }
 
 else{
  digitalWrite(light3, LOW);
  digitalWrite(light4, LOW);
 }
 
 
 if((inches >= 60) && (inches < 66)){
    midiOUT(0x90, 85, 127);
    digitalWrite(light2, HIGH);
    delay(1000);
    digitalWrite(light2, LOW);
 }
 
 else{
  digitalWrite(light2, LOW);
 }
 
  if((inches >= 93) && (inches < 128)){
    midiOUT(0x90, 85, 127);
    digitalWrite(light1, HIGH);
    delay(1000);
    digitalWrite(light1, LOW);
 }
 
 else{
  digitalWrite(light1, LOW);
 }
 
               // wait for a second;

if (millis() - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = millis();  

    // if the LED is off turn it on and vice-versa:
    if (fanState == LOW)
      fanState = HIGH;
    else
      fanState = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(fan, fanState);
  }
  
  if (millis() - previousMillis1 > interval1) {
    // save the last time you blinked the LED
    previousMillis1 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin1State == LOW)
      ledPin1State = HIGH;
    else
      ledPin1State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin1, ledPin1State);
  }
  
  if (millis() - previousMillis2 - 20 > interval2) {
    // save the last time you blinked the LED
    previousMillis2 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin2State == LOW)
      ledPin2State = HIGH;
    else
      ledPin2State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin2, ledPin2State);
  }
  
  if (millis() - previousMillis3 - 30 > interval3) {
    // save the last time you blinked the LED
    previousMillis3 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin3State == LOW)
      ledPin3State = HIGH;
    else
      ledPin3State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin3, ledPin3State);
  }

 if (millis() - previousMillis4 - 10 > interval4) {
    // save the last time you blinked the LED
    previousMillis4 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin4State == LOW)
      ledPin4State = HIGH;
    else
      ledPin4State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin4, ledPin4State);
  }

  /* BEGIN EDITED CODE */

void midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);}
 

  /* END EDITED CODE */

  /* Lite up LED if any value is passed by the echo pulse
   * -------------------------------------------------------------------
   */

  /* Delay of program
   * -------------------------------------------------------------------
   */

  delay(delay_time);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

43  Forum 2005-2010 (read only) / Syntax & Programs / Re: tachometer programming help needed on: August 18, 2009, 01:51:12 pm
Hi Dustin

you are correct - but the code you are referring to is from a previous version and it did not work. The code I have posted up subsequently (the one I put up today) does. The problem with it is that it continually sends a noteON message whilst the rpm count is above 120 as opposed to just sending a single noteON the first time it detects rpm>=120
44  Forum 2005-2010 (read only) / Syntax & Programs / Re: tachometer programming help needed on: August 18, 2009, 09:15:49 am
thanks for the help - sometimes I just miss the little details and remain totally blind to them.
I actually ended up going about writing the code in a different way, just because the way that I was sending the midi data did not work (i.e. when I wanted to send both midi CC and NoteON / OFF ). The new code (as can be seen below) works great, but i have reached a new stumbling block  - as is usually the case.
I need the code to send a single NotON event when the rpm count goes over 120, this triggers a midi sequence of av events (Ableton and Modul8), however as the rpm count is constatly feeding out data the code continually sends the NoteON message for as long as the rpm count is above 120. Is there a way to make it so that it sends just one noteON event when the rpm count goes over 120 first time?

any help is greatly appreciated

Code:
/*
 * Optical Tachometer
 *
 * Uses an IR LED and IR phototransistor to implement an optical tachometer.
 * The IR LED is connected to pin 13 and ran continually. A status LED is connected
 * to pin 12. Pin 2 (interrupt 0) is connected across the IR detector.
 *
 *
 */
//#define DEBUG      

int ledPin = 13;                // IR LED connected to digital pin 13
int statusPin = 12;
int lightPin = 11;// LED connected to digital pin 12
int brightness = 0;
int delay_time = 40; // delay for this amount each write cycle.
int noteDown = LOW;
byte MIDI_channel = 0;
byte cc_number = 0;
byte incomingByte;
byte button;
byte note;
byte printing_byte = 0;
int Value = 0;
int midi_pitch = 0;
int mappedrpm = 0;
int state=0;
#ifdef DEBUG
const int DEBUG_RATE = 9600;        // Serial debugging communicates at 9600 baud
const int SERIAL_PORT_RATE = DEBUG_RATE;
#else
const int MIDI_BAUD_RATE = 31250;   // MIDI communicates at 31250 baud
const int SERIAL_PORT_RATE = MIDI_BAUD_RATE;
#endif



volatile byte rpmcount;
volatile int status;

unsigned int rpm;

unsigned long timeold;

void rpm_fun()
{
  //Each rotation, this interrupt function is run twice, so take that into consideration for
  //calculating RPM
  //Update count
  rpmcount++;

  //Toggle status LED  
  if (status == LOW) {
    status = HIGH;
  }
  else {
    status = LOW;
  }
  digitalWrite(statusPin, status);
}

void setup()
{
  state = 0;

  Serial.begin(SERIAL_PORT_RATE);
  //Interrupt 0 is digital pin 2, so that is where the IR detector is connected
  //Triggers on FALLING (change from HIGH to LOW)
  attachInterrupt(0, rpm_fun, FALLING);

  //Turn on IR LED
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);

  //Use statusPin to flash along with interrupts
  pinMode(statusPin, OUTPUT);

  rpmcount = 0;
  rpm = 0;
  timeold = 0;
  status = LOW;


}

void loop()
{
  //Update RPM every second
  delay(800);
  //Don't process interrupts during calculations
  detachInterrupt(0);
  //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
  //happened once per revolution instead of twice. Other multiples could be used
  //for multi-bladed propellers or fans
  rpm = 3*1000/(millis() - timeold)*rpmcount;
  timeold = millis();
  rpmcount = 0;
  attachInterrupt(0, rpm_fun, FALLING);
  brightness = map(rpm, 0, 50, 0, 255);
  analogWrite(lightPin, brightness);
  printing_byte = map(rpm, 0, 50, 0, 127);{
  midiOUT(0xB0, 7, printing_byte);
  }

    if(printing_byte >= 120){
    midiOUT(0x90, 65, 127);
    }
  
}




  //Write it out to serial port
  //Restart the interrupt processing
  


void midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);

}

45  Forum 2005-2010 (read only) / Syntax & Programs / Re: tachometer programming help needed on: August 12, 2009, 04:40:47 am
thank you for all the replies - afraid the 'Auto format' did not help. I was not aware of that function but in this instance I could not see what changes it may have made to the structure (layout) of the code. will persevere. I am aware that this is out of my comfort zone so to speak, but I believe that by setting yourself hard challenges in fields you are not fully familiar with is a good way of learning.
Pages: 1 2 [3] 4 5 ... 8