Go Down

Topic: Uno + XL MaxSonar EZ1 MB1210 + Director 11.5 + Serial Xtra... (Read 2463 times) previous topic - next topic

Pol23

Hi.

This is my first venture into Arduino so I'm a total n00b although I have messed with simple electronics many years ago. I'm trying to create a simple ultrasound sensor to drive computer-based installation. I got an Uno and a XL MaxSonar EZ1 MB1210 sensor and set it up as directed. It works! My code is below (adapted from Bruce Allen's original code):

Code: [Select]
//Digital pin 7 for reading in the pulse width from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.

const int pwPin = 7;
//variables needed to store values
long pulse, cm;

void setup() {
  //This opens up a serial connection to shoot the results back to the PC console
  Serial.begin(9600);
  //Set up the input pin.
  pinMode(pwPin, INPUT);
}
void loop() {
  //Used to read in the pulse that is being sent by the MaxSonar device.
  //Pulse Width representation with a scale factor of 58uS per centimetre.
  pulse = pulseIn(pwPin, HIGH);
  //58uS per cm
  cm = pulse/58;
  Serial.println(cm);
  delay(10);
}


I'm reading it from Adobe Director 11.5 using the Serial Xtra. The set up code is below:
Code: [Select]
on setUpSensor
  if objectP(gXtra) then
    gXtra.closePort()
    gXtra = VOID
  end if
  put new(xtra "SerialXtra","foo@whatever.com","XXXXXXXXXX",90) into gXtra
  if objectP(gXtra) then
    gxtra.OpenPort("COM3")  -- arduino
    gXtra.setProtocol(9600, "n", 8, 1)
  end if
end


The problem is that I have to press the reset button on the Arduino every time I run the Director code. It seems that as I am setting up comms to the board in this code it stops the Arduino board from running the sensor program on board. Anyone any ideas why? Sorry if this is simple - I couldn't find anything on this particular issue.

Thanks,  Paul.

nawikre

Hi there, i want to use this script with a ultrasoon sensor HC-SR04. I changed the script a bit and added a library etc, but it only gives the distances in cm, but does not changes the variables ' if' in brightness...

What am i doing worng? I am still a noob with these things...But i am trying.
I hope someone can help me in the right direction.

thanks.

This is the code i have made now.
Code: [Select]
#include <NewPing.h>
#define TRIGGER_PIN  9   // Arduino pin verbonden met trigger pin op de ultrasonic sensor.
#define ECHO_PIN     8   // Arduino pin verbonden met echo pin op de ultrasonic sensor.
#define MAX_DISTANCE 250 // Maximale afstand die ik wil meten (in centimeters).
#define REDPIN 5         // Rood van de rgb strip
#define GREENPIN 6       // Groen van de rgb strip
#define BLUEPIN 3        // Blauw van de rgb strip


NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup voor de pins en maximale afstand.

long cm;
long old_cm = cm;

//bewaard oude afstanden in de cyclus
long old_b;
long new_b; 
 
 
void setup() {
  Serial.begin(9600); // Open serial monitor op 9600 baud voor resultaat.
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  delay(200);                      // wacht 75ms tussen metingen (ong. 15 pings/sec).
  unsigned int uS = sonar.ping(); // Stuur meting, ontvangst meting in microseconde(uS).
  Serial.print("afstand: ");
  Serial.print(sonar.convert_cm(uS)); // Zet meettijd om in afstand en print resultaat (0 = maximale afstands bereik, geen meet resultaat)
  Serial.println("cm");
  Serial.print(new_b);
  Serial.println(": helderheid");
  Serial.print(cm);
  Serial.println(": cm");

  if (cm < 16) {
    cm = 0;
    Serial.println(" 0");
  } else if (cm < 20) {
    Serial.println(" 1");
    cm = 20;
  } else if (cm < 30) {
    Serial.println(" 2");
    cm = 30;
  } else if (cm < 40) {
    Serial.println(" 3");
    cm = 40;
  } else if (cm < 50) {
    Serial.println(" 4");
    cm = 50;
  } else if (cm < 60) {
    Serial.println(" 5");
    cm = 60;
  } else if (cm < 70) {
    Serial.println(" 6");
    cm = 70;
  } else if (cm < 80) {
    Serial.println(" 7");
    cm = 80;
  } else if (cm < 90) {
    Serial.println(" 8");
    cm = 90;
  } else if (cm < 100) {
    Serial.println(" 9");
    cm = 100;
  } else if (cm < 110) {
    Serial.println(" 10");
    cm = 110;
  } else if (cm < 120) {
    Serial.println(" 11");
    cm = 120;
  } else if (cm < 130) {
    Serial.println(" 12");
    cm = 130;
  } else if (cm < 140) {
    Serial.println(" 13");
    cm = 140;
  } else if (cm < 150) {
    Serial.println(" 14");
    cm = 150;
  } else if (cm < 160) {
    Serial.println(" 15");
    cm = 160;
  } else if (cm < 170) {
    Serial.println(" 16");
    cm = 170;
  } else if (cm < 180) {
    Serial.println(" 17");
    cm = 180;
  } else if (cm < 190) {
    Serial.println(" 18");
    cm = 190;
  } else {
    Serial.println(" 19");
    cm = 200;
}

new_b = map(cm, 20, 200, 255, 0);
old_b = map(old_cm, 20, 200, 255, 0);


 
  if (old_b <= new_b){
    for (long z = old_b; z < new_b; z++) {
        analogWrite(REDPIN, z);
        analogWrite(BLUEPIN, z);
        analogWrite(GREENPIN, z);
        delay(2);
    }
  }
  else {
    for (long z = old_b; z > new_b; z--) {
       analogWrite(REDPIN, z);
       analogWrite(BLUEPIN, z);
       analogWrite(GREENPIN, z);
       delay(2);
    }
 
  }
 
  old_cm = cm;
 

}

jeannot92

Hallo,

Is het jouw gelukt om een ledstrip te dimmen aan de hand van een HC-SR04 sensor. Ik zou het waarderen als je me verder kon helpen. Mijn project is namelijk: als de sensor dicht tegen de muur staat moet er geen licht zijn. hoe groter de afstand wordt tussen de muur en de sensor, hoe scherper het licht van de RGB ledstrip. Ik krijg maar niet de juiste code. Ook weet ik niet zeker of mijn schakeling goed is.

Graag hoor ik van je ?

met vriendelijke groet,

jeannot

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy