Go Down

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

Eibert_Draisma

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 pm Last 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 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


cyclegadget


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++){

pejo

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

/pejo

Eibert_Draisma

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.

Go Up