Pages: [1]   Go Down
Author Topic: randoms servos and a display  (Read 617 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

* randomgunwerkend.ino (1.55 KB - downloaded 11 times.)
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8822
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

 val = random(potpin); 

That's not going to do anything useful.

Code:
if (randNumber = 0666)
That's doing an assignment, not a comparison.


Code:
  myservo2.write(500);
500 is outside the range 0 to 180.

If anyone else wants to point out other possible problems:
Code:
// 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);
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
  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:
  // 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:
  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).
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18742
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

This is better written as:

Code:
SoftwareSerial mySerial (rxPin, txPin);

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

Code:
  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:
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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

// 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]
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

 }
 
}

 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18742
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

 }
 
}
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: