Need help with Logic

Hey, I am working a water sensor and servo motor, but I cant seem to make the servo motor stop after one loop no matter what i do.
here is the code


#include <SPI.h>
#include <Servo.h>

Servo ser;
void setup() {
 // put your setup code here, to run once:
 pinMode(A0, INPUT);
 pinMode(6, OUTPUT);
 ser.attach(A4);
 Serial.begin(9600);
}
void ser1(int, int);
void serp(int);
void serm(int);
int pos = 0;
int strt = 0;
void loop() {
 int count = 0;
 // put your main code here, to run repeatedly:
 int a = analogRead(A0);
 // print out the value you read:
 Serial.println(a);
 if (a != 0)
 {
   count = 1;
   Serial.println("serp");
   serp(count);
   
 }
 else if (a == 0)
 {
   count = 1;
   Serial.println("serm");
   serm(count);
 }

 delay(300000);


}


void ser1(int pos, int count)
{
 Serial.println("ser1");
 if (pos == 1)
 {
   Serial.println("servo++");
   do
   {
     ser.write(pos);
     delay(50);
     pos++;
   } while (pos <= 45);
   count = 0;
 }
 else
 {
   Serial.println("servo--");
   do
   {
     ser.write(pos);
     delay(50);
     pos--;
   } while (pos <= 45);
   count = 0;
 }
return;
}

void serp(int count)
{
 if (count != 0)
 {
   int pos = 0;
   digitalWrite(9, HIGH);
   ser1(pos, count);
   delay(500);
 }
 else 
 {
   delay(1000);
 }
  return;
}

void serm(int count)
{

 if (count != 0)
 {
   int pos = 45;
   digitalWrite(9, LOW);
   ser1(pos, count);
   delay(500);
 }
 else
 {
   delay(1000);
 }
 return;
}

The motor stats off just fine and keeps rotating in that direction. I've tried everything( from my knowledge)

I've also tried including serial.print(); its always struck at "servo--", and won't budge.

I want the arduino to check for the sensor inputs every 5 minutes and iterate only once .
and repeat the same.

How do I do that? and it'd be great if someone could correct my logic/code.

P.S- I've never tried anything like this and I'm out of ideas

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

It sounds as if you are using a continuous rotation servo - is that correct? Have you figured out what value for Servo.write() or Servo.writeMicroseconds() is necessary to make the motor stop?

It will be close to 90deg or 1500 microsecs but it will vary slightly for different servos.

...R

That code is a bit of a mess. You include SPI.h and then never use SPI. You set pinMode() for pin 6 to output and then never use pin 6.

And it's very odd doing an analogRead() and then only checking the result for zero and not zero. If you have a digital sensor you should be using digitalRead() and if it's an analog sensor the output will almost never be exactly zero.

So basics...exactly what "water sensor" and "servo motor" are you using and what is the program supposed to be doing with them?

Steve

count is not declared as a global

count = 0 ;

void serm(int count)
{

  if (count != 0)
  {
    int pos = 45;
    digitalWrite(9, LOW);
    ser1(pos, count);
    delay(500);
  }
  else
  {
    delay(1000);
  }
  return;
}

here is a tip

use double slash to add notes.

void serm(int count)  // this bit does ....  
{

  if (count != 0)  // count is set to 1 in order to reach this point
  {
    int pos = 45;
    digitalWrite(9, LOW);
    ser1(pos, count);
    delay(500);
  }
  else
  {
    delay(1000);
  }
  return;
}

slipstick:
That code is a bit of a mess. You include SPI.h and then never use SPI. You set pinMode() for pin 6 to output and then never use pin 6.

And it's very odd doing an analogRead() and then only checking the result for zero and not zero. If you have a digital sensor you should be using digitalRead() and if it's an analog sensor the output will almost never be exactly zero.

So basics...exactly what "water sensor" and "servo motor" are you using and what is the program supposed to be doing with them?

Steve

Um actually I made this template for something else, but changed it mid way, so didn't really check the header files, also the water sensor is an analog type and I have a habit of checking stuff midway, if they're working or not, so this isn't the complete sketch.... sorry about that.

About the analog and digital read, something is wrong with my sensor module, the reading doesn't go beyond 330 for what-so-ever reason. That's why I tried taking the value as non- zero.

Thanks for the reply, so any other suggestions?

Robin2:
To make it easy for people to help you please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

It sounds as if you are using a continuous rotation servo - is that correct? Have you figured out what value for Servo.write() or Servo.writeMicroseconds() is necessary to make the motor stop?

It will be close to 90deg or 1500 microsecs but it will vary slightly for different servos.

...R

Sorry for the trouble Robin.

Yes, I'm having a continuous rotation problem, and when I tried debugging, its struck in this line

Serial.println("servo--");

So, where did I go wrong?

Look at this code

 else
 {
   Serial.println("servo--");
   do
   {
     ser.write(pos);
     delay(50);
     pos--;
   } while (pos <= 45);
   count = 0;
 }

Shouldn't the line be

  } while (pos >= 45);

...R

dave-in-nj:
count is not declared as a global

count = 0 ;

void serm(int count)

{

if (count != 0)
 {
   int pos = 45;
   digitalWrite(9, LOW);
   ser1(pos, count);
   delay(500);
 }
 else
 {
   delay(1000);
 }
 return;
}




here is a tip

use double slash to add notes.



void serm(int count)  // this bit does ....  
{

if (count != 0)  // count is set to 1 in order to reach this point
 {
   int pos = 45;
   digitalWrite(9, LOW);
   ser1(pos, count);
   delay(500);
 }
 else
 {
   delay(1000);
 }
 return;
}

Sorry for the trouble, i'll keep that in mind

And as of now, I'm not with my arduino, so once I'm back, ill do it.

Many thanks.

sainikshipth:
Sorry for the trouble Robin.

Yes, I'm having a continuous rotation problem, and when I tried debugging, its struck in this line

Serial.println("servo--");

So, where did I go wrong?

you closed the bracket for
void ser1(int pos, int count) {
and then started with a new } bracket that has no starting bracket
your other mistake is that you did not post the error message you got when trying to validate your sketch.

Try using example sketches to test if the sensors and motors are working.

Also, please post a picture of the project.

And try with another Arduino.

dave-in-nj:
you closed the bracket for
void ser1(int pos, int count) {
and then started with a new } bracket that has no starting bracket
your other mistake is that you did not post the error message you got when trying to validate your sketch.

Hello, I'm not sure where you're pointing, like i checked the whole ser1() function, as all the braces are properly opened and closed. And about the error message, I never got one so i didn't post it.

thanks

Robin2:
Look at this code

 else

{
  Serial.println("servo--");
  do
  {
    ser.write(pos);
    delay(50);
    pos--;
  } while (pos <= 45);
  count = 0;
}




Shouldn't the line be


} while (pos >= 45);




...R

Hey Robin,
Thanks for the correction, nevertheless the problem still persists

I've tried using millis(), tried goto statement and a bunch of others, didn't seem to work.
What shall i try next?

Unsigned_Arduino:
Try using example sketches to test if the sensors and motors are working.

Also, please post a picture of the project.

And try with another Arduino.

Yep, all of them are working

sainikshipth:
I've tried using millis(), tried goto statement and a bunch of others, didn't seem to work.
What shall i try next?

You need to post the latest version of your program so we can see where you have got to.

...R