Show Posts
Pages: [1]
1  Using Arduino / LEDs and Multiplexing / 2 fading LED's and a sweeping Servo controlled via Serial inputs on: May 11, 2014, 01:38:11 pm
Hi folks,

I try to sweep a servo and 2 LED's via input by the serial monitor (just to let you know, in reality I control the arduino via a python script and a pygame implemantation..) so if you send the number 1 to the arduino it should fade LED no.1 and so on.. on no.3 the servo. Here is my code so far. The servo sweeps correctly but the LED's are blinkin only for a short time. I also tried to get rid of the delay function using millis() but it do not have to be a parallel process:

Code:
#include <Servo.h>
Servo myservo; 
 
int pos = 0;   
int ledPin = 10; 
int val = 0;       
int ledPin2 = 9;   

void setup() {
  pinMode(ledPin,OUTPUT);   
  pinMode(ledPin2,OUTPUT);
  Serial.begin(9600);       
  myservo.attach(11); 
}

void loop () {
  val = Serial.read();   
 
if (val == '3' ) {
   bewege();
  }

if (val == '1' ) { 
    for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    analogWrite(ledPin, fadeValue);       
    delay(10);                           
  }

  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    analogWrite(ledPin, fadeValue);           
    delay(10);                           
  }
     }
   
if (val == '2' ) {
    for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    analogWrite(ledPin2, fadeValue);       
    delay(10);                           
  }
    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    analogWrite(ledPin2, fadeValue);         
    delay(10);                           
  }
   
    //Serial.println();
  }
  delay(1);
}

void bewege(){
    for(pos = 0; pos < 180; pos += 1) 
  {                               
    myservo.write(pos);           
    delay(15);                       
  }
  for(pos = 180; pos>=1; pos-=1)     
  {                               
    myservo.write(pos);             
    delay(15);                   
  }
 }

Thank you guys a lot for helping me out here! Sorry for my bad english skills, I've learned it only through the internets;)

2  Topics / Robotics / Re: Ping))) and Servo on: May 02, 2012, 09:46:43 am
THX a lot for my servo (futabas3003) it was 96degrees!
3  Topics / Robotics / Re: Ping))) and Servo on: May 02, 2012, 08:52:56 am
Hello it's me again,

now I modified my servo to turn 360 degrees like in this video:
and I can't stop the servo turning all the time
myservo.write(pos+1); spins it, but how can I stop the servo?

thx for your help! 
4  Topics / Robotics / Re: Ping))) and Servo on: April 25, 2012, 11:22:14 am
I'm sorry for that, but I'm german and it's a little bit difficult to express myself, sorry. yeah exactly thats what I meant:
Quote
It sounds like you want to move the servo, measure a distance, move the servo, measure a distance, etc. That you can do. You just need a different for loop.
How must be the for loop then?

Thanks a lot you really helped me!
5  Topics / Robotics / Re: Ping))) and Servo on: April 25, 2012, 07:49:31 am
Thank you guys but it was not the power, everything is powered up right. The thing was that I had to wait the rotation of the servo after that the ping sends signals again. but not parallel as I wanted to do. so it is a programming issue here.

now there is the problem to achive that, can I call a function or just turn the servo only plus one° (startpoint is indifferent) and not the whole 180° and messure after that with the ping everytime. thx a lot for your time!!! and help!

here is the code again without all the comments:

Code:

const int pingPin = 7;

#include <Servo.h>
 
Servo myservo; 
 
int pos = 0;   


void setup() {

  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
 
  myservo.attach(9); 
 
}

void loop()
{
 
  long duration, inches, cm;

 
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

 
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

 
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
 
    if (cm < 120) {
   
    for(pos = 0; pos < 180; pos += 1)
      {                                 
    myservo.write(pos);             
    delay(200);                     
      }
    for(pos = 180; pos>=1; pos-=1)     
    {                               
    myservo.write(pos);             
    delay(200);                     
    }
   
    digitalWrite(13, HIGH);
    digitalWrite(12, LOW);
   
     
    } else {
    // Anweisungsblock für falsch
    digitalWrite(13, LOW);
    digitalWrite(12, HIGH);
    }
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
6  Topics / Robotics / Re: Ping))) and Servo on: April 24, 2012, 05:04:22 pm
The servo is powered by the 5v pin on the arduino board (also the ping)) I splitted the 5v on a breadboard but seems ok), the serial shows only "16cm" but is not reacting. the ping))) activity led is blinking once at start but not all the time as usually...mhh any other suggestions? thx for your reply!
7  Topics / Robotics / Ping))) and Servo on: April 24, 2012, 02:59:12 pm
Hello arduinolovers, I'm new in this Forum and want to say hello to all you guy's. I'm a beginner in programming the arduino so don't throw stones...smiley-wink but maybee you can help me with my project.

First, What do I try to accomplish? - I got an ultrasonic sensor (Ping))) from parallax) and a servo motor(cheap one) and I try to make them talk!

Second, where is the problem? - They don't talk together. The idea is the following: if you're staying closer then 1,20m in front of the ping)))sensor the servomotor should rotate otherwise not, two led's are indicating that (red/green).

What I have had achived so far: ultrasonic ping + arduino = works!, servo + arduino = works, ping + servo + arduino = doesn't work.

Here is my sourcecode (copy&paste style):

Code:
/* Ping))) Sensor
 
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
    
   The circuit:
    * +V connection of the PING))) attached to +5V
    * GND connection of the PING))) attached to ground
    * SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping
  
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe
 
   This example code is in the public domain.

 */

// this constant won't change.  It's the pin number
// of the sensor's output:


#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
 
int pos = 0;    // variable to store the servo position
const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
  
    if (cm < 120) {
    // Anweisungsblock für wahr
    digitalWrite(13, HIGH);
    digitalWrite(12, LOW);
    
     for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(200);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(200);                       // waits 15ms for the servo to reach the position
  }
    
    } else {
    // Anweisungsblock für falsch
    digitalWrite(13, LOW);
    digitalWrite(12, HIGH);
    }
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

So maybee you guys, can help me. It's my second arduino project.

With best regards Borner!
Pages: [1]