I'm Stuck - any ideas?

So I fell in love with the idea of a laser harp. I decided to embark on building one. Probably too much of a project for a first time programmer; but I’m a quick study and I think I’m going about the project the correct way. I’ve just hit a stumbling block I’ve not quite figured out.

First off; I knew I was jumping into the deep end and barely able to tread water. I’m not pushing to get this done in any timeframe so I have been taking it one step at a time. First connecting up just a stepper and writing code to control it. Once I had that working I moved on to the next step, then next, etc…

I’m now trying to detect when a beam is broken. Here’s my problem. I have this code that when I run it with the circuit buit it works like a champ. I can adjust sensitivity, and have gotten a reflected beam to register every time. (using the Arduino pin 13 led). And, yes, a lot of this code is cobbled together from other sources and is not 100% my own.

int beambroken  = 4;      // beam interrupt indicator
int sensorState = 0; 
const int sensorPin = 2;  // sensor pin

void setup()
{
pinMode(beambroken, OUTPUT);  
digitalWrite(beambroken, HIGH); 
delay(200);
digitalWrite(beambroken, LOW); 
delay(200);
} // end void setup

void loop()
{
sensorState = digitalRead(sensorPin);
// Beam interupted
if (sensorState == HIGH)
   {
    digitalWrite(beambroken, HIGH); // Beam interupt indicator
   }
// Beam not interupted   
if (sensorState == LOW)
   {
    digitalWrite(beambroken, LOW);  // Beam interupt indicator
   }    
} // end void loop

So this works. but in my main harp program I want to run a function while the beam is on to tell if the beam has been detected. Main reason for this is once the detection is working correctly; I can work in the midi control into the function much easier while keeping the main program loop as simple as possible.

Here is the code I have for the whole program. Please don’t critique too much! LOLOL I’ve got a TON of comments in here just for notes to myself. This code works - I get 10 unique beams; I have been able to ensure the beams are positioned at the same location every time the program starts (that one took me a while to get right!); and frankly I’ve been pretty happy with what I have done.

In this code below; after the laser is turned on I delay then run the BeamRead() function. At this stage all I want to do is light the Arduino LED. Like in my test code, I can adjust the sensitivity of the detectors and over drive the circuit and force the LED to light, but when I back it off I have to shine a light directly on the detector to get it the LED lit. That just doesn’t make a lot of sense to me. Why the code itself would work to detect when run by itself, but not when I merge it into the main program.

#include <Stepper.h>

const int stepsPerRevolution = 200;  // per motor specifications

Stepper myStepper(stepsPerRevolution, 3, 4, 5, 6); // initialize the stepper library on pins 3 through 6:

int beam              = 0;
int StepCount         = 0;  // number of steps the motor has taken
int StepSpeed         = 1;  // Delay between steps - lower # = faster
int LaserPin          = 7;  // Laser On/Off Control
int delaylaser        = 2;  // If you increase this, the laser will be brighter, but the harp will be less fluid
int MirrorPositioned  = 0;  // Test if Mirror has been positioned
int MirrorPin         = 8;  // Mirror Switch Test Pin
int MirrorPinRead     = 0;
int x                 = 0;  //counter
int z                 = 0;  //counter
int beambroken        = 13; // beam interrupt indicator  .... Will light Arduino LED Pin13
int sensorState       = 0;
const int sensorPin   = 2;  // sensor pin
int myNote[10];             // Array for Beam Detection

/////////////////////////////////////////////////////////////
//
//  Best 10 beam steady is StepSpeed 7 and delaylaser 1
//  Using 2 steps per beam, use 2 and 7
//  Step 1 delay 2 produced decent fan effect for fog
//
/////////////////////////////////////////////////////////////

void setup() {

  pinMode(13, OUTPUT);
  Serial.begin(9600);             // initialize the serial port:
  pinMode(LaserPin, OUTPUT);      // Setup for laser.
  pinMode(MirrorPin, INPUT);      // Setup for Mirror Test
  digitalWrite(LaserPin, LOW);
  pinMode(beambroken, OUTPUT);
  digitalWrite(beambroken, HIGH);
  delay(100);
  digitalWrite(beambroken, LOW);
  delay(100);
  for (int beam = 0; beam < 12; beam++)
  {
    myNote[beam] = 0;
  }
}

// THIS SECTION WORKS - 10 Steps Forward/Backward - Try adjusting StepSpeed and stepsPerRevolution for speed/spacing

void loop()
{

  MirrorCenter();

  for (int beam = 0; beam < 9; beam++) {   // Move Mirror 10 steps forward
    myStepper.step(1);
    delay(StepSpeed);
    digitalWrite(LaserPin, HIGH);          // Turn on the laser
    delay(delaylaser);
    
    //Insert reading of LDR here
    sensorState = digitalRead(sensorPin);
    BeamRead();
    
    digitalWrite(LaserPin, LOW);           // Turn off the Laser
  }

  for (int beam = 9; beam > 0; beam--) {   // Move Mirror 10 steps forward
    myStepper.step(-1);
    delay(StepSpeed);
    digitalWrite(LaserPin, HIGH);          // Turn on the laser
    delay(delaylaser);
  
    //Insert reading of LDR here
    sensorState = digitalRead(sensorPin);
    BeamRead();

    digitalWrite(LaserPin, LOW);           // Turn off the Laser
    }
}
/////////////////////
//                 //
//    FUNCTIONS    //
//                 //
/////////////////////

// Center Stepper Motor Function //
void MirrorCenter()
{
  digitalWrite(LaserPin, LOW);
  MirrorPinRead = digitalRead(MirrorPin);
  while ((MirrorPositioned == 0) && (MirrorPinRead == LOW))
  {
    myStepper.step(-1);
    delay(400);
    MirrorPinRead = digitalRead(MirrorPin);
    x = x + 1;
  }
  while (z < 28) {
    myStepper.step(1);
    delay(100);
    z = z + 1;
  }
  MirrorPositioned = 1;
}
 
// Beam Detection Function //
void BeamRead()
{
// Beam interupted
if (sensorState == HIGH)
   {
    digitalWrite(beambroken, HIGH);      // Beam interupt indicator (Lights Arduino Pin13 LED)
   }
// Beam not interupted   
if (sensorState == LOW)
   {
    digitalWrite(beambroken, LOW);       // Beam interupt indicator
   }  
}

I’m open to any and all suggestions here; but I would really just like to get the detector working.
One thought I had (and will have to try next time I get time to work on the program, is to move the delay(delaylaser) AFTER the BeamRead() function. In my mind I don’t see that making a difference; but I’ve been proven wrong too often not to give it a try…

TIA!

Like in my test code, I can adjust the sensitivity of the detectors and over drive the circuit and force the LED to light, but when I back it off I have to shine a light directly on the detector to get it the LED lit. That just doesn't make a lot of sense to me.

It doesn't make much sense to me either, because you haven't described your physical setup, the nature of the detector, how the detectors are wired, how the sensitivity is "adjusted", etc.

You have to make all that clear, with diagrams and photos, before you can expect someone to understand your description of the problem. See the "How to use this forum" post for more advice on posting.

Thanks for posting the code correctly, though!

Sorry. I tried as best I could to be as descriptive as possible. I have no real setup photos or drawings to share. As I mentioned I have been piecing parts of other examples/descriptions/etc together. That's one reason I take an example and work out the wiring and code by itself before integrating it into the main program where/how I think it should work.

Here is a snapshot look at the schematic I'm following. Best I could do on this computer is to get it to a Word document. Hope that helps.

Basically it's 3 TSL12S through a LM358 to essentially give me a HIGH/LOW output to pin2 on the arduino. The TSL12S detectors face upwards but are shadowed from the main beam. When putting my hand into the laser beam the reflected light is enough that the detector's outputs exceed the threshold set by the 10k resistor and the output of the LM358 goes high.

When the laser is shining one beam (not turning on/off or rotated by the stepper motor) and the only code running is my first example above - the circuit and code works as it should. Once integrated into the whole program, beam is on/off and rotated, I cannot get the detection circuit to register anything.

The only way I can get any light to trigger the LM358's output to switch high is to directly shine a lamp onto the detectors while running in the 2nd code example above.

I'll go back and read through the how to post again. Thx

Well… lets try that attachment again…

Image from Reply #3 so we don’t have to download it. See this Image Guide

d06e6e51277270e722ef6b56067e1c2eca2e34f5.png

…R

@ac5ff you seem to have a peculiar talent for writing a lot but not focusing on the important bits.

Are you saying that the output of the LM358 is normally LOW and you would prefer it to be HIGH - in which case you should be able to make that correction with your Arduino code.

When the laser is shining one beam (not turning on/off or rotated by the stepper motor) and the only code running is my first example above - the circuit and code works as it should. Once integrated into the whole program, beam is on/off and rotated, I cannot get the detection circuit to register anything.

You have not provided any information to show whether this is a hardware problem - in other words the moving beam is not being detected properly. Or whether it is a programming problem - i.e. the program is not properly detecting correct behaviour by the external hardware.

A simple description of how the machine is intended to work - especially a diagram of it - would make it much easier to help. You were asked for that back in Reply #1. If you haven't got diagrams or photos then make them.

...R

Hi, I gather;

  • You have an array of sensors each representing the sting of a harp.
  • You have one laser.
  • You have a stepper motor.
  • The laser is mounted on a stepper driven apparatus.
  • The moving stepper makes the laser sweep across the array of sensors.
  • When you detect that a sensor should be activated by the laser, by using the stepper count to indicate position.
  • And it is activated, then no note sounded.
  • Not Activated, so must be covered, then note is sounded.

You have only briefly mentioned the stepper, once I think!

If that is wrong, can you tell us, in this point form and a diagram what you have? Draw a diagram by hand and post a picture of it.

Thanks.. Tom.. :)

Okay. I've typed now 4 different replies to this thread. Each one has only helped to further my ... how do I say this ... lost faith? Anger? not sure. Maybe my frame of mind has been off the past couple days. Don't know.

I've also looked trying to find a way to mark this thread solved - or delete it completely. Haven't found a way to do either.

If someone can - please do. or let me know and I'll delete it.

ac5ff: Okay. I've typed now 4 different replies to this thread. Each one has only helped to further my ... how do I say this ... lost faith? Anger? not sure.

Are you suggesting that the reason you did not get the perfect answer after your first question is our fault?

As far as I can see everyone here is taking a lot of trouble to help you - especally @TomGeorge

...R

Hi, If you are frustrated, think of it from our side.

You have the project infront of you, we don't. You know how it is wired, we don't. You know how you are powering the project, we don't. You know how you are scanning the sensors, we don't. You know how you detect the beams, we don't.

A picture, a circuit diagram and a sketch is worth a thousand words and can save a thousand ill informed posts.

I would have thought my post, itemizing what I thought you had, would have got you to reply with a suitable explanation of your project. You seem to think we don't understand you project, my post was to try and get everybody on the same page.

We are not payed to be here, in fact its 3:55am where I am. I can't sleep, I don't have to work in the morning, so I decided to come onto the forum to see if anybody needed some help. To keep my mind occupied.

What do I see? Your reply to a simply layed out question. If I have the time to give you my time,right now at 3:55am, I would think you would have the time to read and answer my posting.

Tom... :o .

Everyone...

My apologies - I'm not trying to argue/point fingers/etc. I think i'm in a bit of a funk. When I thought I laid out a description of my problem/etc to include code and what I see happening I thought I fully described everything.

Please trust me - I 100% understand people here are answering posts/helping others out of pure kindness!! While I'm new to Arduino - I'm not new to forums and helping people. As a matter of fact I have been quite active as a source of help on several other forums. I know how it goes with needing information to solve problems.

That may be part of my problem with this post. I honestly don't know. But I do sincerely apologize to everyone here. My deal with deleting the thread is likely due to my funk more than anything else.

Tom;

I did read your initial reply - but by that point I think I was just flustered. I have no drawings/photos/etc that would help in this situation. It's quite involved - and while I can see it in my head I have a very hard time putting that picture on paper to share.

Robin;

Thank you for showing how to post a photo so it appears in the thread. I've never tried that or looked into how others have done it in the past. I'll definatly be using this information in the future.

I'm going to go back through a bunch of threads here and look for a good example to model my post off of - as well as people that have posted photos and/or drawings of their project. At that point I will put something together again and see how it goes.

Truly - I appreciate people here and the efforts they go through to help not just myself - but everyone. I'm going to get out of this funk - get some better data together - and then come back and re-address my problems. Until then please accept my apologies and thanks!

AC

I'm coming back to this thread for a few reasons.

First - I have to apologize again. Second - I wanted to post that this thread will be my reminder - and maybe to others that read it - that there IS a reason needed to post drawings/photos/etc.

I finally had a chance to look work on this project again tonight. I had a few ideas I wanted to run through and tests I wanted to try. Long story short - the code was ALMOST good and the wiring was perfect. However there was NO way to discover that via seeing ONLY the code.

I am controlling my laser via a NPN transistor turning the current to it on/off with the command " digitalWrite(LaserPin, HIGH); " As it turned out, I thought the laser was ON when it was actually off (and visa-versa). What was odd is that the delays it was creating 10 near perfect beams as they were expecting to.

Long story short (too late) ... I was trying to detect the beams when the laser was OFF. Flipping my control around solved the detection issues.

I'm next to POSITIVE that had I included a drawing - even a hand sketch (which I'm working on!) - someone here would have identified the problem right away.

Again people - sorry!!!

AC

Thanks for the update. Glad to hear you have made progress.

...R

I'm still working on 'drawing' out a schematic of what I've done.. I think I am better at writing code than I am at drawing a straight line! HA!