Go Down

Topic: analog values stop at threshold (Read 753 times) previous topic - next topic

ifugaopapercraft

My pro mini is writing values from a photocell to the serial monitor, reading correctly until exceeding the threshold, then stops and will not restart, even when the cell is returned to darkness. Assuming this is a software error. Would appreciate advise. Thanks.

Code: [Select]


#include <Stepper.h>

#define STEPS1 200
#define STOP 0

int rightSwitchPin =7;
int leftSwitchPin =8;
int sonarPin = A0;
int sonarValue = 0;

Stepper stepper1(STEPS1, 3,5,4,6);//bipolar

void setup()
{
  stepper1.setSpeed(30);
  Serial.begin(9600);
 
}
void loop()
{
  sonarValue = analogRead (sonarPin);
  Serial.println (sonarValue);
  delay(500);

  if (sonarValue >900)
  {
    while (digitalRead(rightSwitchPin) == LOW) //keep going until limit switch reached
    {
      stepper1.step(-1);  //move right
    }
    while (digitalRead(leftSwitchPin) == LOW) //keep going until limit switch reached
    {
      stepper1.step(1);  //move left
     
    }
  }
  else
  {
    stepper1.step (STOP);
  }
}



PaulS

Your photocell is attached to the pin named sonarPin? The value is being stored in sonarValue? What does sonar have to do with photocell data?

How are the limit switches wired? Do they ever get pressed? Does the motor actually step?

ifugaopapercraft

Sorry, should have named them appropriately for the forum.  I have a bunch of pro mini boards, each with a photocell installed within a short length of pipe on one end and an LED on the other. The LED responds to a Maxsonar sensor. I need everything; servos, dc motors, LED's, sound, and steppers, to all fire at the same time. I am sure this can be accomplished programmatically but after months of advise and experiment, I opted for this not so elegant method. The moment the LED lights, the whole shebang cranks up.

The sketch worked fine before writing the "if statement", the stepper responding perfectly to the switches as it passes back and forth (a printer salvage). 

PaulS

Quote
The sketch worked fine before writing the "if statement", the stepper responding perfectly to the switches as it passes back and forth (a printer salvage).

I can understand your frustration, but it doesn't help if you don't describe what now happens. As the light level goes up, the motor starts moving back and forth. When the light level drops, what happens?

No more serial prints? No more stepper movement? The stepper doesn't stop at the limit switches?

There is nothing obviously wrong with the code, that I can see.

Quote
The LED responds to a Maxsonar sensor.

What LED? What Max Sonar sensor? Neither of these appear to be part of your sketch.

ifugaopapercraft

At this point, nothing happens. The serial monitor reads the values right up until it arrives at the threshold, then stops, gets stuck at 900, and ceases to continue reading. Only after exceeding the threshold is the stepper supposed to begin which, of course, is impossible without current data. I have noticed similar behavior with the other boards but the intended action occurs, even though the data stops at the threshold. When the photocells are covered, the serial monitor once again lists the values.

PaulS

Adding a Serial.print() inside the if block would be a good idea, then. As would answering the question about how the switches are wired.

Having the stepper step 0 times when the light level is low is unnecessary. The stepper will do nothing if not told to do anything.

ifugaopapercraft

The switches are wired on each end of the carriage with 10K pulldown resistors. Again, these were working fine without the "if". I'll try the Serial.print() tomorrow. Thanks for the help.

ifugaopapercraft

Now the serial monitor displays nothing until the threshold has been surpassed, then just a single digit and locks up as before. If the marked out Serial.println is included, the lesser values are listed up to the threshold but stop. Whew! This would seem a simple analog problem, no different that the previous five boards with what seems to be the exact same setup.




Code: [Select]


#include <Stepper.h>

#define STEPS1 200
#define STOP 0

int rightSwitchPin =7;
int leftSwitchPin =8;
int sonarPin = A0;
int sonarValue = 0;

Stepper stepper1(STEPS1, 3,5,4,6);//bipolar

void setup()
{
  stepper1.setSpeed(30);
  Serial.begin(9600);
 
}
void loop()
{
  sonarValue = analogRead (sonarPin);
  //Serial.println (sonarValue);
  delay(500);

  if (sonarValue >900)
  {
    Serial.print("sonarValue ");
    Serial.println(sonarValue);
    while (digitalRead(rightSwitchPin) == LOW) //keep going until limit switch reached
    {
      stepper1.step(-1);  //move right
    }
    while (digitalRead(leftSwitchPin) == LOW) //keep going until limit switch reached
    {
      stepper1.step(1);  //move left
    }
  }
}



ifugaopapercraft

Here is the sketch for a mini pro board driving a unipolar motor with what would seem to be the exact same method.  The analog values are displayed without a problem.

Code: [Select]

#include <Stepper.h>

//stepper
#define STEPS1 200
#define STOP 0

const int stepsPerRevolution = 200;

Stepper stepper1(STEPS1, 9,8,7,6);

int sonarPin = A0;
int sonarVal =0;

void setup()
{
  stepper1.setSpeed(30);
  Serial.begin(9600);
  pinMode(sonarPin,INPUT);
}
void loop()
{
  sonarVal = analogRead (sonarPin);
  Serial.println(sonarVal);
  delay(500);
  {   
    if (sonarVal > 800)
    {
      stepper1.step(stepsPerRevolution); 
    }
    else
    { 
      stepper1.step(STOP);
      delay(50);
      // stepper1.step(-300);
    }
  }
}







ifugaopapercraft

Forgot to mention, this sketch, when uploaded to the board in question, displays values as intended. Would seem it doesn't like the "while" code?

ifugaopapercraft

Finally!  The Serial.println in each "while" statement was necessary.  Thanks for the help. 

#include <Stepper.h>

#define STEPS1 200
#define STOP 0

int rightSwitchPin =7;
int leftSwitchPin =8;
int sonarPin = A0;
int sonarValue=0;

Stepper stepper1(STEPS1, 3,5,4,6);//bipolar

void setup()
{
  stepper1.setSpeed(30);
  Serial.begin(9600);

}
void loop()
{

  sonarValue = analogRead (sonarPin);
  Serial.println (sonarValue);

  if (sonarValue >900)
  {
    while (digitalRead(rightSwitchPin) == LOW) //keep going until limit switch reached
    {   
      stepper1.step(-1);  //move right
      Serial.println (sonarValue); 
    }

    while (digitalRead(leftSwitchPin) == LOW) //keep going until limit switch reached
    {
      stepper1.step(1);  //move left
      Serial.println (sonarValue);

      {
      }
    }
  }
}








PeterH


The Serial.println in each "while" statement was necessary.


It should not have been necessary, and if the problem has 'gone away' when you added these then you are just avoiding it by a coincidence of timing and have not really fixed the problem. The print statements were intended to let you know what was happening inside your sketch, not to change the behaviour.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up