Go Down

Topic: Using the Loop function (Read 529 times) previous topic - next topic

SohanP

I am new to Arduino programing and I have attempted to create my first project but I have encountered a few issues. For your understand here is what I want the out come of my project to be: When the photocell detects light, I want the motor to spin clockwise for 10000 milliseconds and then completely stop spinning and wait for when the photocell detects no light and when it does, I want it to spin counter-clockwise for 10000 milliseconds and then completely stop spinning. These commands should be in a loop so that the 2 different outcomes occur when the photocell changes from detecting light to not.

With what I have written to far, the motor just keeps spinning one way with small pauses every 10000 milliseconds and doesn't stop unless it detects light. When it does detect light, it spins in the opposite direction and continues to spin in the opposite direction with small pauses until it doesn't detect light anymore. When it doesn't detect light anymore, it just goes back to spinning the other way with small pauses.

I need help with any necessary steps needed to get from where I am to where I want to be. Thank you.

(My code)

/*

*/
int In1 = 7;
int In2 = 8;
int ENA = 5;
int SPEED = 255;
int Photocell = A1;
int analogValue = 0;

void setup() {
    pinMode(In1, OUTPUT);
    pinMode(In2, OUTPUT);
    pinMode(ENA, OUTPUT);
}

void loop() {
    analogValue = analogRead(Photocell);
    if(analogValue >= 50){
   
    digitalWrite(In1, HIGH);
    digitalWrite(In2, LOW);
    delay(10000);
    digitalWrite(In1, LOW);
    digitalWrite(In2, LOW);
    analogWrite(ENA, SPEED);
    }
    if(analogValue < 50){
   
    digitalWrite(In1, LOW);
    digitalWrite(In2, HIGH);
    delay(10000);
    digitalWrite(In1, LOW);
    digitalWrite(In2, LOW);
    analogWrite(ENA, SPEED);
    }
    delay(200);
    digitalWrite(In1, LOW);
    digitalWrite(In2, LOW);
}

sterretje

#1
Jul 01, 2017, 06:17 am Last Edit: Jul 01, 2017, 06:19 am by sterretje
Please edit your post and add code tags
Before your code type [code]
After your code type [/code]

For your problem, you will have to remember that you moved the motor forward (or backward). The simplest is possible the implementation of a state machine using a switch statement. In your case it will have four states (steps).

In the first state, you wait for light to be detected; once light is detected, you go to the next state and move the motor forward for the given duration. Once this is completed you go to the state where you wait for the light to disappear; once that is detected, you move the motor backwards.


The code below can be used as a framework; you will have to fill in the dots. Place the following at the top of your code
Code: [Select]
// states for state machine
#define WAITFORLIGHT 0
#define MOVEFORWARD 1
#define WAITFORNOLIGHT 2
#define MOVEBACKWARD 3

// current statemachine state; start with waiting for light to be detected
byte currentState = WAITFORLIGHT;


And modify loop
Code: [Select]
void loop()
{
  analogValue = analogRead(Photocell);

  switch (currentState)
  {
    case WAITFORLIGHT:
      if (analogValue >= 50)
      {
        currentState = MOVEFORWARD;
      }
      break;
    case MOVEFORWARD:
      ...
      ...
      currentState = WAITFORNOLIGHT;
      break;
    case WAITFORNOLIGHT:
      if (analogValue < 50)
      {
        currentState = MOVEBACKWARD;
      }
      break;
    case MOVEBACKWARD:
      ...
      ...
      currentState = WAITFORLIGHT;
      break;
  }
}


Notes:
1)
Your first 5 integers in your original post should actually be preceded by the 'const' keyword so the compiler will warn you if you try to modify them.
2)
You should get rid of delay and use a millis() based timing if you want to do more things at the same time (e.g. wait for a button to be pressed or stop the motor if the light level changes etc); understand the blink-without-delay example to achieve this (if needed).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

PaulS

You don't really need a state machine. Look at the state change detection example. When the sensor changes state, from not detecting light to detecting light, spin the motor for ten seconds. When the sensor changes state, from detecting light to not detecting light, spin the motor the other way for 10 seconds.

Do NOT expect the motor to end up EXACTLY where it started, or to get whatever it is moving to the same spot each time.

Because you are using an analog sensor, the state change is not as easy to determine, but, it isn't that hard, either. Detecting light means that the value is above some threshold. Not detecting light means that the value is below some threshold. To avoid banging back and forth as the light level nears the threshold, use two different thresholds (a higher one for "there is light" and a slightly lower one for "there is dark").
The art of getting good answers lies in asking good questions.

johnwasser

Your sketch is equivalent to:
Code: [Select]
void loop() {
    if(analogRead(Photocell) >= 50)
        SpinForward();
    else
         SpinBackward();
    delay(10000);  // Continue for 10 seconds
    Stop();
    delay(200);  // Stop for 0.2 seconds
}

It's going to be spinning Forward or Backward for about 10000 out of every 10200 milliseconds.  To detect a change in the light you need to remember the previous state:

Code: [Select]
boolean PreviousLightOn = false;
void loop() {
    boolean lightOn = analogRead(Photocell) >= 50;
    if (lightOn != PreviousLightOn) {  // State has changed
        PreviousLightOn = lightOn;
        if(lightOn) 
            SpinForward();
        else
             SpinBackward();
        delay(10000);  // Continue for 10 seconds
    }
    Stop();
    delay(1000);  // Stop for one second
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

MarkT

You don't really need a state machine. Look at the state change detection example. When the sensor changes state, from not detecting light to detecting light, spin the motor for ten seconds. When the sensor changes state, from detecting light to not detecting light, spin the motor the other way for 10 seconds.
But that is exactly a state machine.  One with two states and two transistions.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up