Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Beating Heart Project.
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Beating Heart Project.

by baddspella on Wed Feb 09, 2011 4:55 am

so iv built a proto tupe of a plushy heart that glows its heart beat. It has a potentiometer to controll the rate of wich the heart beats.
it works fine and all, but now i want to add a small servo to make the heart physicaly beat, with the glowing LED, but i also want the servo's beating to be controlled by the same POT that controlls the led.

im new to coding but i managed to merge my original code with the sweep example code.
but it just wont work.

LED heart beat on
Code: Select all | TOGGLE FULL SIZE
int sensorPin = 0;    // select the input pin for the potentiometer
 int ledPin = 9;   // select the pin for the LED
 int sensorValue = 0;  // variable to store the value coming from the sensor

 void setup()  {
   // nothing happens in setup
 }

 void loop()  {
   // This part need to stay as it was in the original code
   for(int fadeValue = 0; fadeValue <= 255; fadeValue +=5 ) {
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, fadeValue);
     // sets the delay with the pot value
     delay(analogRead(sensorPin));
   }

   // fade out from max to min in increments of 5 points:
   for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, fadeValue);
     // sets the delay with the pot value
   delay(analogRead(sensorPin));
   }
 }
 


LED heart beat + servo
Code: Select all | TOGGLE FULL SIZE
#include <Servo.h>

Servo myservo; // create servo object to control a servo
int pos = 1;  // variable to store the servo position

int sensorPin = 0;    // select the input pin for the potentiometer
 int ledPin = 9;   // select the pin for the LED
 int sensorValue = 0;  // variable to store the value coming from the sensor

 void setup()  {
  myservo.attach(10);  // attaches the servo on pin 10 to the servo object
}

 void loop()  {
   // This part need to stay as it was in the original code
   for(int fadeValue = 0; fadeValue <= 255; fadeValue +=5 ) {
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, fadeValue);
     // sets the delay with the pot value
     delay(analogRead(sensorPin));
   }

   // fade out from max to min in increments of 5 points:
   for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, fadeValue);
     // sets the delay with the pot value
   delay(analogRead(sensorPin));
   }
   
    {
  for(pos = 1; 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(analogRead(sensorPin));                       // waits ??? 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'
    (analogRead(sensorPin));                       // waits ??? for the servo to reach the position
  }
 }
 }
 



please if some one can help me and point out what iv done wrong?
or if some one can fix the code for me, im willing to sent $5 over paypal as a last resort.

-Gab
baddspella
 
Posts: 1
Joined: Wed Feb 09, 2011 4:45 am

Re: Beating Heart Project.

by adafruit_support_bill on Wed Feb 09, 2011 7:09 am

but it just wont work.

You should be more specific about what doesn't work.

Looking at the code, my guess would be that you get alternating 'beats' from the LED and the servo. You have 4 'for' loops that run sequentially. If you want the servo to move synchronously with the LED, you need to merge your servo code into the fade loops - something like this:

Code: Select all | TOGGLE FULL SIZE
   // This part need to stay as it was in the original code
   for(int fadeValue = 0; fadeValue <= 255; fadeValue +=5 ) {
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, fadeValue);
     int servoValue = map(fadeValue, 0, 255, 0, 180);  // map the fade value to the servo range
     myservo.write(servoValue);
     // sets the delay with the pot value
     delay(analogRead(sensorPin));
   }

   // fade out from max to min in increments of 5 points:
   for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, fadeValue);
     int servoValue = map(fadeValue, 0, 255, 0, 180);  // map the fade value to the servo range
     myservo.write(servoValue);
     // sets the delay with the pot value
   delay(analogRead(sensorPin));
   }

adafruit_support_bill
 
Posts: 78630
Joined: Sat Feb 07, 2009 10:11 am

Re: Beating Heart Project.

by jdubrow on Wed Feb 09, 2011 11:05 am

What arduwino said. You may also want to have a delay after each beat as well to make it more life like. I always think of a heart beat occuring like: ___-_-___-_-___ (where a - is a beat and _ is idle).

jdubrow
 
Posts: 24
Joined: Tue Dec 21, 2010 1:49 pm

Please be positive and constructive with your questions and comments.


cron