Arduino Forum

International => India => Topic started by: sainikshipth on Jun 25, 2018, 08:35 am

Title: Need help with logic
Post by: sainikshipth on Jun 25, 2018, 08:35 am
Hey, I am working a water sensor, but I cant seem to make it 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
Title: Re: Need help with logic
Post by: AWOL on Jun 25, 2018, 01:53 pm
Code: [Select]
do
    {
      ser.write(pos);
      delay(50);
      pos--;
    } while (pos <= 45);
maybe you're just not waiting long enough
Title: Re: Need help with logic
Post by: sainikshipth on Jun 26, 2018, 10:24 am
So ill try changing to time to 500, ill get back
Title: Re: Need help with logic
Post by: AWOL on Jun 26, 2018, 10:26 am
So ill try changing to time to 500, ill get back
Need help with logic.
Title: Re: Need help with logic
Post by: sainikshipth on Jun 26, 2018, 10:51 am
Need help with logic.
So my logic isn't right? Could you be more specific where i went wrong? It'd be a great help.
Title: Re: Need help with logic
Post by: sainikshipth on Jun 27, 2018, 07:33 am
Need help with logic.
Didn't help