Automated Crab Claw; sketch works but would like it to be more simple

Dear all,

I am working on an automated crab claw: see Eibert Draisma
(Sorry the page is in Dutch, will change that soon)
Right now, you can push a button and it will "snap". But I really would like it to function autonomously, like a 'plant'.
The claw is operated by a solenoid.

It should work like this:

  • Every once in a while, the claw should 'snap' one or more times: lets call this a snap-sequence
  • One snap, means that the solenoid should be activated for 0,1 second (So the claw is only shortly closed)
  • The number of snaps should be random, but always between 1 and 7
  • The time between two subsequent snaps in a snap sequence, should be random but short
  • The time between subsequent snap sequences, should also be random, but long

This is my first Arduino project, but thanks to the Arduino Cook Book I was able to write a working sketch: see below.
The pin13 LED is simulating a solenoid (although in this case it is activated for 1 sec, not 0.1 sec)

I am really happy the sketch is working: as I have enough experience with electronics, the next step (connecting a solenoid) shouldn't be too difficult.
BUT: I would prefer the sketch to be much shorter.
However, I don't know which approach I should follow, as I lack programming experience.
I hope one of you can help me or comment on my sketch!

Thank you in advance, Eibert.

/*
 * Blink pin13 LED randomly 1 to 7 times, with longer random-length pauses between the blink-sequences
 *
*/

long randPause = 0;               // Initialize a variable for a short Pause time between subsequent blinks
long randOff = 0;                 // Initialize a variable for the OFF time between blink sequences

int randNumber;

void setup()                      // run once, when the sketch starts
{
pinMode(13, OUTPUT);              // sets the digital pin as output
randomSeed (analogRead (0));      // read from an analog port with nothing connected
}

void loop()                       // run over and over again
{
randPause = random (200, 1000);   // generate Pause time between 0,2 and 1 seconds (time between blinks in a sequence)
randOff = random (5000, 1000);    // generate OFF time between 5 and 10 seconds (time between sequences)
randNumber = random(8);           // 
if (randNumber==1)                // blink once if random number is 1
{
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randOff);                   // waits for a random time while LED is OFF
}
else if (randNumber==2)           // blink twice if random number is 2
{
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randOff);                   // waits for a random time while LED is OFF
}
else if (randNumber==3)           // blink three times if random number is 3
{
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randOff);                   // waits for a random time while LED is OFF
}
else if (randNumber==4)           // blink four times if random number is 4
{
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randOff);                   // waits for a random time while LED is OFF
}
else if (randNumber==5)           // blink five times if random number is 5
{
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randOff);                   // waits for a random time while LED is OFF
}
else if (randNumber==6)           // blink sx times if random number is 6
{
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randOff);                   // waits for a random time while LED is OFF
}
else if (randNumber==7)           // blink seven times if random number is 7
{
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randPause);                 // Pause time between 0,2 and 1 seconds
digitalWrite(13, HIGH);           // set the LED on
delay(1000);                      // wait for one second while LED is ON
digitalWrite(13, LOW);            // sets the LED off
delay(randOff);                   // waits for a random time while LED is OFF
}
}

Moderator edit: code tags added

I recommend learning "for" loops. http://arduino.cc/en/Reference/For

This is part of the code that would go after your "if" statement.

for (int i=0; i <= randNumber; i++){
/*
 * Blink pin13 LED randomly 1 to 7 times, with longer random-length pauses between the blink-sequences
 *
 */

long randPause = 0;               // Initialize a variable for a short Pause time between subsequent blinks
long randOff = 0;                 // Initialize a variable for the OFF time between blink sequences
int randNumber;

void setup(){
  pinMode(13, OUTPUT);              // sets the digital pin as output
  randomSeed (analogRead (0));      // read from an analog port with nothing connected
}

void loop(){
  randPause = random (200, 1000);   // generate Pause time between 0,2 and 1 seconds (time between blinks in a sequence)
  randOff = random (5000, 10000);    // generate OFF time between 5 and 10 seconds (time between sequences)
  randNumber = random(8);
  blinkLed();
}

void blinkLed(){
  for(int i=0;i<randNumber;i++){
    digitalWrite(13, HIGH);           // set the LED on
    delay(1000);                      // wait for one second while LED is ON
    digitalWrite(13, LOW);            // sets the LED off
    delay(randPause);
  }
  delay(randOff);
}

Dear Pejo and Cyclegadget, thank you for your comments and help!

Pejo,
The sketch you wrote is a lot shorter, but seems to be doing something else:

  • The pauses between blinks in one sequence, now all seem to have the same duration;
  • The OFF time between sequences is varying ok
    Like this:

- - - -- - - - - -- - -- - -____

But what I am trying to accomplish is this:

- - - --- - -- -- - --- -____

(So also the pauses between blinks have a random duration between 0,2 and 1 seconds)

I don't quite understand what should be changed in your sketch to achieve this.
Maybe you know how to do this?

Best regards, Eibert.

Another option is to put the sequences in an array, then randomly call out the sequence to be performed.

Hi CrossRoads,

That could be an option, but than I'll have to come up with the sequences myself. I would prefer the software to surprise me with ever changing sequences :slight_smile:

Eibert_Draisma:
Hi CrossRoads,

That could be an option, but than I'll have to come up with the sequences myself. I would prefer the software to surprise me with ever changing sequences :slight_smile:

So move the code that generates a random pause value into your loop, right before the call to delay(), that way a new pause length is generated on each call.

So move the code that generates a random pause value into your loop, right before the call to delay(), that way a new pause length is generated on each call.
[/quote]

Should I do this in the sketch suggested by Pejo?
I don't understand what you mean...
(the good news is: tomorrow I will be following a C++ course at MediaMatic in Amsterdam)

Here is a picture that shows what I am trying to achieve:

http://dl.dropbox.com/u/12349225/Blinking%20Led.jpg

  • LED (pin13) should blink randomly 1 to 7 times (this is one sequence)
  • The "HIGH" time of the LED is always 1 second
  • The "Pause" time between two blinks (that are part of one sequence) should vary between 0,2 and 1 second
  • The time between two sequences, should vary randomly between 5 and 10 seconds

I tried and I tried but cannot get the solution suggested by CrossRoads working....

Hi all,

Jraskell, thanks for your advice; the sketch is working perfectly now :slight_smile:
Connecting a solenoid instead of the blinking LED is no big deal, so I am really happy.

This is the sketch:

/*
Randomly lights a LED for 1 to 10 times (blink sequence)
When lit, the LED stays on for 0,2 seconds, than is turned off for a SHORT period, randomly between 0,1 to 2 seconds until the next blink
After the last blink of a sequence, the LED is turned off for a LONG period, randomly between 5 and 10 seconds.
*/

int led = 13;
long randOffSHORT = 0;
long randOffLONG = 0;

void setup() {
pinMode(led, OUTPUT); // initialize the digital pin as an output.
// Pin 13 has a LED connected on most Arduino boards:
}

void loop() {
randOffLONG = random(5000,10000);
int blinkNumber = random (1,11);

for (int a = 0; a < blinkNumber; a ++){
randOffSHORT = random (100, 2000);
digitalWrite(led, HIGH); // set the LED on
delay(200); // wait for specified duration
digitalWrite(led, LOW); // turn the LED off
delay(randOffSHORT); // wait for short random period
}

delay(randOffLONG); // wait for long random period
}