count 0-170 then back

ok so it is getting hot down under so i will be using a servo to turn a fan from left to right.
this is the code i have come up with

#include <Servo.h>

Servo fan;

int arm;                           //will be used later
int val = 0;                      //value that will be sent to servo

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting");
  
  fan.attach(9);               // servo called fan attached to pin 9
  delay(1000);
}

void loop()
{
  if (val < 170)                 //this is where i have my problem
    up();
  fan.write(val);
}

void up()
 {
   if (val < 170)
   val ++; 
   Serial.println(val);
   delay(50);
 }
 
 void down()
 {
   if (val == 179)
   val --;
   Serial.println(val);
   delay(50);
 }

now i need it to run “up” when “val” is below 170 and then after it has reached 170 it needs to run “down” until “val” reaches 0 then repeat.

i dont think i explained it well but i hope you understand
please note: this is my first time programing

You only need one function, step(), not two. Instead of using ++ and --, use +=, and add an increment. The increment value will be either +1 or -1.

Start with increment = +1.

When val is 180, change increment to -1. When val is 0, change increment to +1.

A for loop is a much simpler way of doing this, and is less likely to lead to hair loss than your approach. http://arduino.cc/en/Reference/For

ok so this is my new code

#include <Servo.h>
Servo fan;

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting");
  fan.attach(9);
  delay(1000);
}

void loop()
{
  for(int val = 0; val <= 170; val++){
    Serial.println(val);
    fan.write(val);
    delay(50);
  }
  
  for(int val = 170; val >= 0; val--){
    Serial.println(val);
    fan.write(val);
    delay(50);
  }
  
}

now it does its 180º anti-clockwise and then 180º clockwise, but when it gets back to 0º i jumps to 90º. i can not find what part of the code is doing this.

P.S. i have it going and it did the loop 5-10 times and then it just stopped and i needed to restart the arduino… i have it plugged in through the USB and wall socket. could the heat from the voltage regulator be the problem

but when it gets back to 0º i jumps to 90º.

Odd because when I run this the printed output looks fine. Is your printed output fine?

could the heat from the voltage regulator be the problem

Put your finger on it is it hot? If you can stand to hold your finger on for longer than 10 seconds then it is not too hot.

Most likely is interference from the motor and fan causing the arduino to reset or crash.

How are you powering the servos? Not from the Arduino, I hope.

Grumpy_Mike:

but when it gets back to 0º it jumps to 90º.

Odd because when I run this the printed output looks fine. Is your printed output fine? yer mine is fine

could the heat from the voltage regulator be the problem

Put your finger on it is it hot? If you can stand to hold your finger on for longer than 10 seconds then it is not too hot.

Most likely is interference from the motor and fan causing the arduino to reset or crash. i don't think so it was sitting on my desk next to the laptop

PaulS: How are you powering the servos? Not from the Arduino, I hope. yes i was, i do have a 5v regulator but i don't have any caps or perfboard to get it working. and it's only the one servo

and it's only the one servo

And that one servo sucks a lot more current than the Arduino can safely supply. But, hey, it's your Arduino.

PaulS:

and it's only the one servo

And that one servo sucks a lot more current than the Arduino can safely supply. But, hey, it's your Arduino.

ok good point. looks like i will be down at the electronics store in the morning

NOTE: completely untested!

Following PaulS suggestion you could do something along the line of:

#include <Servo.h>

const uint8_t   k_pinSERVO  = 9;

Servo fan;

void updateFanWithBounds(int min = 0, int max = 170)
{
// UNCOMMENT FOLLOWING LINE TO DEBUG
//#define DEBUG_FAN

    static int val = 0;
    static int inc = 1;

#ifdef DEBUG_FAN
    Serial.println(val);
    delay(50);
#endif

    fan.write(val);

    val += inc;

    // IF WE'VE REACHED EITHER END BOUNDARY CONDITION REVERSE 'inc' DIRECTION
    if ( (val == min) || (val == max) )
    {
        // NEGATE INCREMENT AMOUNT: 1 or -1
        inc = -inc;
    }

#ifdef DEBUG_FAN
#undef DEBUG_FAN
#endif
}

void loop()
{
    updateFanWithBounds(0, 170);
}

void setup()
{
    Serial.begin(9600);
    Serial.println("Starting");
    
    fan.attach(k_pinSERVO);
    delay(1000);
}

ok so the code below is the one i am using and i am now using a 5v regulator and it has fixed the problem of the servo jumping to 90º

#include <Servo.h>
Servo fan;

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting");
  fan.attach(9);
  delay(1000);
}

void loop()
{
  for(int val = 0; val <= 170; val++){
    Serial.println(val);
    fan.write(val);
    delay(150);
  }
  for(int val = 170; val >= 0; val--){
    Serial.println(val);
    fan.write(val);
    delay(150);
  }
  
}

Since comments and debug code seem to confuse some people I’ll repost with them all removed.

#include <Servo.h>

Servo fan;

int val = 0;
int inc = 1;

void loop()
{
    fan.write(val);

    val += inc;
    if ( (val == 0) || (val == 170) )
        inc = -inc;
}

void setup()
{
    fan.attach(9);
    delay(1000);
}