need a little help here, I attached my patch, could somebody have a look at it?
what I am trying to do is this:
generating random numbers and showing them on the display
make one servo move according to other random numers
make the servo move from one point to another and back IF the randomnumber is 0666
what happens is:
the random numbers are displayed at the display
but the random servo only moves one time, then it stops....
I cant figure out what I am doing wrong, could somebody give me some advice?
int potpin = 0; // analog pin used to connect the potentiomete
val = random(potpin);
That's not going to do anything useful.
if (randNumber = 0666)
That's doing an assignment, not a comparison.
myservo2.write(500);
500 is outside the range 0 to 180.
If anyone else wants to point out other possible problems:
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiomete
int val; // variable to read the value from random
// library
#include <SoftwareSerial.h>
// Pin for 7 segment display
#define rxPin 2
#define txPin 3
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
long randNumber;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(11); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// randomSeed(analogRead(0));
}
void loop()
{
// print a random number from 0 to 9999
randNumber = random(10000);
mySerial.print(randNumber);
delay(500);
randNumber = random(1023);
val = random(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(30); // waits for the servo to get there
if (randNumber = 0666)
myservo2.write(0);
delay (500);
myservo2.write(500);
delay (500);
myservo.write(0);
}
You have a mySerial attached to those two pins? What IS a mySerial? Meaningful names are not only allowed, they are strongly recommended.
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(11); // attaches the servo on pin 9 to the servo object
It is NOT necessary to use PWM pins for servos. Don't these servos have any physical function that would lend better names? Did you name your kids myChild and myChild2?
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
But, you've told the SoftwareSerial instance that these are its pins. Why are you messing with them?
val = random(potpin); // reads the value of the potentiometer (value between 0 and 1023)
The code does NOT do what the comment says. Fix or delete the comment or the code (or both).
@Nick Gammon fixed the softwareserial mumbojumbo
now indeed I want all those lines done if the randomnumber is 0666
number 0666 is not picked intentional
so then the code is:
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int val; // variable to read the value from the analog pin
// library
#include <SoftwareSerial.h>
// Pin for 7 segment display
#define rxPin 2
#define txPin 3
SoftwareSerial mySerial (rxPin, txPin);
long randNumber;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
mySerial.begin(9600);
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// randomSeed(analogRead(0));
}
void loop()
{
randNumber = random(1023);
val = random(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
mySerial.print(randNumber);
}
{
if randNumber == (666)
myservo.write(0);
delay (500);
myservo.write(90);
delay (500);
myservo.write(0);
}
[code]
now this is what I have, and everything works
except the second servo...
I have it on a seperate powersource via the vin-pin
strange no?
any more ideas?
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
int val;
// library
#include <SoftwareSerial.h>
// Pin for 7 segment display
#define rxPin 2
#define txPin 3
SoftwareSerial mySerial (rxPin, txPin);
long randNumber;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(11); // attaches the servo on pin 9 to the servo object
mySerial.begin(9600);
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// randomSeed(analogRead(0));
}
void loop()
{
randNumber = random(1023);
val = randNumber;
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(500); // waits for the servo to get there
mySerial.print(randNumber);
if (randNumber == 666)
{ delay (2000);
myservo2.write(0);
myservo.write(0);
delay (2000);
myservo2.write(90);
myservo.write(90);
delay (2000);
myservo2.write(0);
myservo.write(0);
}
}
as long as it works, it works
point is that it doesnt
but why?
here's my latest code till now
only the myservo2 (sry paulS) stuff does not work
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
int val;
// library
#include <SoftwareSerial.h>
// Pin for 7 segment display
#define rxPin 2
#define txPin 3
SoftwareSerial mySerial (rxPin, txPin);
long randNumber;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(11); // attaches the servo on pin 9 to the servo object
mySerial.begin(9600);
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// randomSeed(analogRead(0));
}
void loop()
{
randNumber = random(1023);
val = randNumber;
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(500); // waits for the servo to get there
mySerial.print(randNumber);
if (randNumber == 6666)
{ delay (2000);
myservo2.write(0);
myservo.write(0);
delay (2000);
myservo2.write(90);
myservo.write(90);
delay (2000);
myservo2.write(0);
myservo.write(0);
}
}