Show Posts
Pages: [1]
1  Using Arduino / Motors, Mechanics, and Power / Re: Different Setup/code for a Parallax Servo vs a GWS s35 STD? on: April 24, 2014, 12:46:26 am
Thanks everyone, I appreciate the feedback! With the help of a programming buddy I changed my Stop and Fwd variables to 1520 and 1300 respectively and it now functions. I am hopeful that eventually I will stop stumping myself completely but we'll see.
2  Using Arduino / Motors, Mechanics, and Power / Different Setup/code for a Parallax Servo vs a GWS s35 STD? on: April 23, 2014, 06:19:36 pm
Hello! I was using the GWS S35 STD continuous rotation motor but eventually it wore itself out with the load that I had. I swapped it for a Parallax motor and it runs great - the only problem is that the new motor doesn't respond to my code - it just runs continuously instead of the start/stop/pause that I have in my code . So now I'm at a loss about what do to get it to pay attention to the code. I'm assuming I need to change something in my language (i.e. the parallax requires a different language than the S35)? The serial monitor shows that the code is doing what its supposed to (i.e. no weird values) but the motor doesn't respond to any of the commands. I am fairly new to this game and my troubleshooting skills are fairly limited. Any help is welcome! Thank-you! smiley

I've included my code which works great with the old motor (no problems). Note: the code for the light gets written out with the bool declaration at the start. 
Code:
#include "Servo.h"

// if you are putting this code in the light piece, set to true
// if you are putting this code in the motor piece, set to false
bool lightPiece = false;


// 20 and 80 for motor
// 10 and 25 for light
int trigger = 35; // This is how close someone needs to be to set off
int resetTrigger = 80; // This is arbitrary and only matters for resetting after someone has
// triggered maxRepeat times
//  could be anything above trigger

// variables declared here will be global and accesible by all loops(scopes) from here down
// I always set loop or sensor variables to zero unless antother default is needed, why?
// Because it ensures that they are set to something initially and not some random value
// in memory.
// Motor code
Servo exMotor;
const int motorPin    = 9;  // motor on pin 9
const int PWPinMotor  = 5;  // sensor on analog 5
const int motorStop   = 90; // may need some adjusting to find stop-point.
const int motorFwd    = 45; // or 135 for the opposite direction

// LED code
const int LED = 9; //LED on pin 9
const int PWPinLED = A0; // sensor on analog 0

// code for logic
int i=0;
int samples = 0;
int sdist = 0;      // sensor distance, raw value returned fron the sensor
int nsamp = 20;   // number of samples to read an average
int pause = 3000;  // pause for 3 seconds before reading any more values, should delay constant going off if standing in front of sensor
bool empty = true;

// Constants
int maxRepeat = 5; // The maximum number of times you want to set off before shutting down
int nRepeat = 0; // A counter for how many times it has been set off

void setup()
{
  if (lightPiece)
  {
    setupLight();
  }
  else
  {
    setupMotor();
  }
  delay(3000);
  // gives you 3 seconds to get your posterior out of the way of the sensor before I calibrate
  // default readings are hard coded

}

/////////////////////////////
// Setup the motor
////////////////////////////
void setupMotor()
{
  exMotor.write (motorStop);
  exMotor.attach (motorPin);
  Serial.begin(9600);
  Serial.println("Arduino Duemilanove ATmega328 Starting");
}

/////////////////////////////
// Setup the light
///////////////////////////
void setupLight()
{
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Arduino Duemilanove ATmega328 Starting");
}


void loop()
{
  samples=0;
  // read nsamp number of samples from the sensor and average them when done
  // this will avoid being set off by a couple bad values
  int totalSamplesTaken = 0;
  for (i=0; i<nsamp; i++)
  {
    if (lightPiece)
    {
      sdist = analogRead(PWPinLED);
    }
    else
    {
      sdist = analogRead(PWPinMotor);
    }
    samples+=sdist;
  }
  sdist = samples/nsamp;


  Serial.println();
  Serial.print("Sensor Distance sampled (sdist) ");
  Serial.println(sdist, DEC);


  // if the room is empty, and there is someone close enough to trigger
  //  but only trigger five times in a row
  if (empty)
  {
    if(sdist < trigger)
    {
      Serial.print("Triggered: ");
      Serial.println(sdist);
      if (nRepeat < maxRepeat)   // run it five times 
      {
        // Run the motor or turn on the light
        if (lightPiece)
        {
          changeLight(true);
        }
        else
        {
          changeMotor(true);
        }
        nRepeat++;
        Serial.print("Repeat: ");
        Serial.println(nRepeat);
      }
      else // we have reached our maximum repeat, turn it all off
      //  until someone moves farther away
      {
        if (lightPiece)
        {
          changeLight(false);
        }
        else
        {
          changeMotor(false);
        }
        Serial.println("Max repeats reached");
        Serial.println("OFF"); 
        empty = false;
      }
    }
    else // there was no trigger, so reset our maximum count
    {
      nRepeat = 0;
    }
  }

  // if the room is full, test to see if the person has moved away
  if (!empty)
  {
    Serial.println("Full");
    if (sdist > resetTrigger)
    {
      empty = true;
      nRepeat = 0;
    }
  }
  delay(1000);  //checks sensor every 1 second for person/object 
}

/////////////////////////////
// This will turn the light
// on and off depending on
// true false passed in
////////////////////////////
void changeLight(bool change)
{
  if (change)
    {
    Serial.println("ON");
    digitalWrite(LED, HIGH);
    delay(2000);
    digitalWrite (LED, LOW);
    }
  else
  {
    Serial.println("OFF");       
    digitalWrite(LED, LOW);
  }
}


/////////////////////////////
// This will turn the motor
// on and off dependon on
// true false passed in
////////////////////////////
void changeMotor(bool change)
{
  if (change)
  {
    Serial.println("ON");
    exMotor.attach (motorPin);
    exMotor.write (motorFwd);   
    delay(2000);
    exMotor.write (motorStop);
  }
  else
  {
    exMotor.detach();
  }
}


3  Using Arduino / Sensors / Re: slight change in sketch means sensor won't let servo run with the +5v connected on: January 27, 2013, 06:30:19 pm
The specs for the sonar says that it can work from 0 - 255 inches but I will try plugging in a bit longer distance.
4  Using Arduino / Sensors / slight change in sketch means sensor won't let servo run with the +5v connected on: January 26, 2013, 02:25:26 am
Hi hi! I will preface this by saying that I am an artist and don't have a great depth of knowledge with electronics but I am working away at it anyways (what better way to learn?). I have a show coming up where I am using my arduinos and I have them set up with a sketch where a servo runs when my maxsonar EZ sensor is tripped. I have one sketch that I run on most of them and I have modified it slightly for my opening (where the room will be crowded). Basically, I have only changed two variables in my sketch (distance where the sensor will be tripped, from two feet to two inches) and the time between pings from the sonar (from .5 seconds to 1 second) so that they don't run constantly if the room is crowded. Both versions of the sketch upload and have been verified and where it gets weird is when I plug everything together. Using the sketch with the shorter sensor distance, the motor won't run if the sensor is connected to the +5v on my board (I have the adafruit motor shield) but it runs just fine when its only connected to the ground and pin. The initial sketch runs whether its connected to the +5V or not. I don't know enough to know why (other than maybe with the shorter distance it draws less power and the 5v is too much?) so maybe you guys can help! Two questions:
1. Any ideas as to why this happens?
2. Do I need to attach my EZ sensor to the +5v on my shield or can I just leave it unconnected (and have it only connected to the ground and analog pin)?

Thanks for any help and I apologize for any wrong language (or if this doesn't make sense). 
5  Using Arduino / Programming Questions / Re: Help with servo code to make servo run for a few seconds when sensor is tripped on: July 02, 2011, 02:05:21 am
Thanks!  I also changed the code from servoStop to exServo.detach(); (and added exServo.attach (servoPin); into the start of the loop section) and that seems to have stopped the buzzing. I figured the stop just held it in place whereas the detach would actually shut it off.  I will also run your code, I need to start understanding the Serial info as well. 
6  Using Arduino / Programming Questions / Re: Help with servo code to make servo run for a few seconds when sensor is tripped on: July 01, 2011, 01:41:09 pm
Thanks so much for your help!  It is working now (the motor kind of hums or buzzes gently when the sensor isn't tripped - I'm going to assume this is normal?). I'm not surprised I messed up the code and wasn't using the servo library very well.  Is there a site/book with some good servo tutorials that I can use (that go beyond the servo ones on the arduino site)?  I can't seem to figure out the library by just looking at what I have on my computer, I guess a lack of experience.  Many of the examples I found didn't use the commands that you've written so I'm guessing I'm not finding the right stuff.  I have gone through many tutorials with LEDs and have gone through the Getting Started With Arduino book but was really having trouble with servo code.  It is a bit overwhelming to be jumping into something that I have no experience with but its super interesting and I am enjoying figuring things out and being able to get help when I can't.   Thanks again, I really appreciate it.
7  Using Arduino / Programming Questions / Help with servo code to make servo run for a few seconds when sensor is tripped on: July 01, 2011, 02:14:27 am
Hello!  I am super new to programming, electronics and arduinos!  I am trying to figure out the code to run my project but I am stuck.  I am trying to run a servo for five seconds when a sensor is tripped and then stop when it is not tripped.  Currently, my servo runs when the sensor is tripped but does not stay on for five seconds as I would like (it turns off the instant that I am out of sensor range).  What am I missing in my code to make it stay on for five seconds?  I have run this program for a LED and it works fine but I am obviously missing something with the servo code.  I am having trouble understanding some of the code as I am seeing either really basic code (which I understand) or overly complex code.  I am using:
Arduino Duemilanove
Adafruit mshield
GWS S35/STD continuous rotation servo
MaxSonar Sensor

Thanks for any help!  

Here is my code:
Code:
#include "WProgram.h"
#include "Servo.h"
int servoPin = 9;  //motor on pin 9
int pin_sensor  =0;  //sensor on analog 0
int inchesAway;    //inches away from maxsonar sensor
int pos=0; //variable to store the servo position
static void refresh();

void setup()
{
pinMode(servoPin,OUTPUT);
digitalWrite(servoPin,HIGH);
delay(5000);
digitalWrite(servoPin, LOW);

Serial.begin(9600);
}


void loop()
{
inchesAway = analogRead(pin_sensor) /2;
Serial.print(inchesAway, DEC);
  
  if(inchesAway <24) {  //if something is 24" or less away, motor on
    digitalWrite(servoPin,HIGH);
    delayMicroseconds(5000);  //stays on for five seconds THIS IS NOT HAPPENING
    digitalWrite(servoPin,LOW);
    delay(20); //20ms
    
  }
  
  else
  {
      Serial.print("OFF");        
      digitalWrite(servoPin, pos);  //when sensor isn't tripped, motor is off
  }
  delayMicroseconds(300);  //checks sensor every .3 seconds for person/object  

}
Pages: [1]