Go Down

### Topic: Automated Crab Claw; sketch works but would like it to be more simple (Read 1 time)previous topic - next topic

#### Eibert_Draisma

##### Apr 20, 2012, 07:19 pm
Dear all,

I am working on an automated crab claw: see http://www.eibertdraisma.nl/?pageAlias=art&curId=44&pageId=61
(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.

#### Eibert_Draisma

#1
##### Apr 20, 2012, 07:20 pmLast Edit: Apr 20, 2012, 09:19 pm by AWOL Reason: 1
Code: [Select]
`/* * 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 blinkslong randOff = 0;                 // Initialize a variable for the OFF time between blink sequencesint randNumber;void setup()                      // run once, when the sketch starts{pinMode(13, OUTPUT);              // sets the digital pin as outputrandomSeed (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 ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(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 ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(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 ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(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 ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(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 ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(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 ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(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 ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randPause);                 // Pause time between 0,2 and 1 secondsdigitalWrite(13, HIGH);           // set the LED ondelay(1000);                      // wait for one second while LED is ONdigitalWrite(13, LOW);            // sets the LED offdelay(randOff);                   // waits for a random time while LED is OFF}}`

Moderator edit: code tags added

#2
##### Apr 20, 2012, 07:25 pm

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

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

Code: [Select]
`for (int i=0; i <= randNumber; i++){`
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

#### pejo0003

#3
##### Apr 20, 2012, 07:45 pm
Code: [Select]
`/* * 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 blinkslong randOff = 0;                 // Initialize a variable for the OFF time between blink sequencesint 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);}`

#### Eibert_Draisma

#4
##### Apr 20, 2012, 09:13 pm
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.

#5
##### Apr 20, 2012, 09:23 pm
Another option is to put the sequences in an array, then randomly call out the sequence to be performed.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### Eibert_Draisma

#6
##### Apr 20, 2012, 09:35 pm

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

#7
##### Apr 20, 2012, 09:39 pm

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

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.

#### Eibert_Draisma

#8
##### Apr 20, 2012, 10:13 pm

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)

#### Eibert_Draisma

#9
##### Apr 20, 2012, 11:42 pm
Here is a picture that shows what I am trying to achieve:

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

#### Eibert_Draisma

#10
##### Apr 21, 2012, 06:44 pm
Hi all,

Jraskell, thanks for your advice; the sketch is working perfectly now :-)
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
}

Go Up