Go Down

Topic: midi error syntax issues (Read 707 times) previous topic - next topic

kazimier

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

kazimier

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: [Select]
/* 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.

kazimier

where as before I had it looking like this:


Code: [Select]
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;


Go Up