Go Down

Topic: randoms servos and a display (Read 710 times) previous topic - next topic

artvantriest

hi people

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?



thanx a lot

art

johnwasser

Code: [Select]

int potpin = 0;  // analog pin used to connect the potentiomete

val = random(potpin); 


That's not going to do anything useful.

Code: [Select]

if (randNumber = 0666)

That's doing an assignment, not a comparison.


Code: [Select]

  myservo2.write(500);

500 is outside the range 0 to 180.

If anyone else wants to point out other possible problems:
Code: [Select]

// 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);
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

PaulS

Code: [Select]
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
You have a mySerial attached to those two pins? What IS a mySerial? Meaningful names are not only allowed, they are strongly recommended.

Code: [Select]
  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?

Code: [Select]
  // 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?

Code: [Select]
  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

Code: [Select]
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

This is better written as:

Code: [Select]
SoftwareSerial mySerial (rxPin, txPin);

Your method (perhaps not obviously) makes a second unnamed SoftwareSerial object and copies that into mySerial.

Code: [Select]

  if (randNumber = 0666)
  myservo2.write(0);
  delay (500);
  myservo2.write(500);
  delay (500);
  myservo.write(0);


Are you expecting to do one thing after the "if" or all those lines?

Code: [Select]
0666

That is octal 666 (438 in decimal). Is that intentional?

Also, as johnwasser said, you need == rather than =.


Next time post your code into the message, inside [code] tags.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

artvantriest

thnx you all for the advice:

@johnwasser and PaulS , got rid of the potpin

@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:

Code: [Select]


// 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]
[/code]

artvantriest

ok I made some adjustments

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?

Code: [Select]


// 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);

}
 
}


PaulS

Quote
now this is what I have, and everything works
except the second servo...

Which does what? It doesn't appear that you took most of what I said seriously.

Nick Gammon


Did you name your kids myChild and myChild2?


Doesn't New York has 42nd Street and 43rd Street?

I'm trying to work PaulS1 and PaulS2 into a funny line, but can't quite do it.

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

artvantriest

hehehe

enough with the namecalling! ;-)

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


Code: [Select]


// 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);

}
 
}

dxw00d

Quote
Doesn't New York has 42nd Street and 43rd Street?

http://www.brooklyn.com/streetsindex.html#numbered

Quote
only the myservo2 (sry paulS) stuff does not work

Code: [Select]
randNumber = random(1023);
...
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);

}


I don't think that a random number, in the range 0 to 1022, will ever be equal to 6666.

artvantriest

true, I noticed that
and I also noticed that the uploading is not right, ill try to fix that and come back to uguys!

art

Go Up