Pages: [1]   Go Down
Author Topic: Beating Heart Project.  (Read 965 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so iv built a prototype 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
Code:
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:
#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
Logged

Romania
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i'm not sure if I understood correctly but you need to store the value of the pot to a int and use that int for the heart and servo movement as well.
like:
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);
    myservo.write(fadeValue);
     // sets the delay with the pot value
     delay(analogRead(sensorPin));
   }
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yer  i want to use the same int to controll the sweep rate of the servo and the glow rate of the LED.
i have it working for the LED just fine, but i cant get the servo part to work.

not to be rude or un-thankfull, but  you edited the wrong part of the code, the part you edited was the section that glows the LED
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46058
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
not to be rude or un-thankfull, but  you edited the wrong part of the code
Well, you are being rude and unthankful. You don't want the LED to flash and then the heart to beat. You want to do them at the same time. So, you don't want to two separate loops.

Quote
but i cant get the servo part to work.
Stop whining and tell us what happens. "It doesn't work" could cover a whole range of issues, from hardware not working or not connected correctly to software. Specifically, what does the code do that you don't want and what does it not do that you do want?
Logged

Pages: [1]   Go Up
Jump to: