Help request: combining sketches

Very excited to join the community! I’m a newb (charitably) with no programming experience, but would like to create a singular sketch from 4 separate sketches and would really appreciate some help. I have an Arduino UNO and am trying to use a break in an IR eye beam to randomly trigger one of five servo arms to rotate 120 degrees and back three times and finally time-out for 60 minutes before returning to a “trigger-able” state.

I’ve started to modify the code below, but would appreciate help with syntax and to let me know if I’m moving in the right direction. Thank you!!

IR Breakbeam
/*
IR Breakbeam sensor demo!
// Arduino | IR Breakbeam Sensors | Adafruit Learning System
*/

#define LEDPIN 13
// Pin 13: Arduino has an LED connected on pin 13
// Pin 11: Teensy 2.0 has the LED on pin 11
// Pin 6: Teensy++ 2.0 has the LED on pin 6
// Pin 13: Teensy 3.0 has the LED on pin 13

#define SENSORPIN 4

// variables will change:
int sensorState = 0, lastState=0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup

Serial.begin(9600);
}

void loop(){
// read the state of the pushbutton value:
sensorState = digitalRead(SENSORPIN);

// check if the sensor beam is broken
// if it is, the sensorState is LOW:
if (sensorState == LOW) {
// turn LED on:
digitalWrite(LEDPIN, HIGH);
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
}

Random Number Generation
long randNumber;

void setup() {
Serial.begin(9600);

// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
}

void loop() {
// print a random number from 0 to 4
randNumber = random(5);
Serial.println(randNumber);

Servo Movement
// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = randNumber;
// Create a servo object
Servo Servo1;
void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}
void loop(){
// Make servo go to 0 degrees
Servo1.write(0);
delay(500);
// Make servo go to 120 degrees
Servo1.write(120);
delay(1000);
// Make servo go to 0 degrees
Servo1.write(0);
delay(500);
// Make servo go to 120 degrees
Servo1.write(120);
delay(1000);
// Make servo go to 0 degrees
Servo1.write(0);
delay(500);
// Make servo go to 120 degrees
Servo1.write(120);
delay(1000);

}

60 minute timeout
//60 minute timeout

delay(3600000)

/////////////////////
whatever you have before setup put here, from both sketches
/////////////////////

void setup() {
put here whatever you have in void setup() from both sketches

}

void loop() {
put here whatever you have in void loop() from both sketches

}

1 Like

Hello there.
It will be a bit difficult to help you there with such a great task, but you can start with the aproximation that 79galinakorczak gave you and you can ask later whatever syntax difficulties you encounter, cause with that explanation it is a bit hard to understand exactly what do you want to achieve.

Greetings : D

Before you begin combining sketches, I would recommend you experiment with a couple of basic examples. The most important is to unlearn the use of delay(). Delay stops your sketch from executing code. You must learn how to do several things at the same time. Have a look at the following example.

File → Examples → 02.Digital → BlinkWithoutDelay

Rewrite your individual sketches first to not use delay(). This will make it a lot easier to combine them. The same is true for other constructs that make your code wait e.g., while loops.

You will need to add some variables to remember your state. In your example the state would be which of the Servo1.write you were executing last and the delay after that. Have a look at the switch…case statement to help you with that.

Also start moving your code inside the loop() into its own function and then call it from loop(). This will look silly for a single example but will prepare you for combining stuff.

Stop trying to “write code” and copy stuff you found out on the street.

Start here!

You have a really good start of a framework as to what you want. Break this down in “human” to steps you would like to do. Set this up as a set of HUMAN instructions and work out the logic. Then, all you will have left to do is just tie in the bits that make the processor do those actions.

Sounds crazy, but it works.

-jim lee

1 Like

When you get to the combining part:

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

https://arduinoinfo.mywikis.net/wiki/CombiningArduinoSketches

1 Like

Thank you for all your feedback! I took as much as I could to craft a human instruction list, update the sketch and post a schematic. The sketch compiles in the Arduino software. I would appreciate feedback (especially on if the random number generation can be used in this way to randomly trigger 1 of the 5 servos and if sketch should do what the human instruction list describes).

Human mapping of what I want to do:
· Big picture: randomly trigger 1 of 5 servo motors to move when breaking an IR beam, then initiate a 60 minute time-out so the effect happens at most, 1 time per hour
· Steps:

  1. Start an IR beam to act as a sensor for the program
  2. When the IR beam is broken, initiate the following sequential effects:
    a. Turn on onboard LED
    b. Generate a random number
    c. Use the random number to define which servo pin the movement signal is sent to
    d. Send signal/generate movement in random servo
    i. Movement:
    1. Servo arm moves to 120 degrees
    2. Servo arm moves to 0 degrees
    3. Servo arm moves to 120 degrees
    4. Servo arm moves to 0 degrees
    5. Servo arm moves to 120 degrees
    6. Servo arm moves to 0 degrees
    e. Delay completion of the loop by 60 minutes (3,600,000 ms)

Sketch
//////////////////////////////////////////////////

//define sensor pin for IR
#define SENSORPIN 7

//define LED to test IR
#define LEDPIN 13

// IR variables will change:
int sensorState = 0, lastState=0; // variable for reading the IR status

// Random number setup
long randNumber;

// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = randNumber;
// Create a servo object
Servo Servo1;

//////////////////////////////////////////////////

void setup() {

// initialize the IR LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// initialize the IR sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup
Serial.begin(9600);

// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));

// We need to attach the servo to the used pin number
Servo1.attach(servoPin);

}

void loop() {

// read the state of the IR value:
sensorState = digitalRead(SENSORPIN);
// check if the IR sensor beam is broken
// if it is, the IR sensorState is LOW:
if (sensorState == LOW) {
// turn IR LED on:
digitalWrite(LEDPIN, HIGH);

//trigger random number sequence and `print a random number from 2 to 6
randNumber = random(2, 7);
Serial.println(randNumber);

// Make servo go to 120 degrees
Servo1.write(120);
//comment out for now: delay(1000);
// Make servo go to 0 degrees
Servo1.write(0);
//comment out for now: delay(500);
// Make servo go to 120 degrees
Servo1.write(120);
//comment out for now: delay(1000);
// Make servo go to 0 degrees
Servo1.write(0);
//comment out for now: delay(500);
// Make servo go to 120 degrees
Servo1.write(120);
//comment out for now: delay(1000);
// Make servo go to 0 degrees
Servo1.write(0);

// 1-hour timeout
delay(3600000);

}

else {

// turn IR LED off:
digitalWrite(LEDPIN, LOW);

}

}

Schematic diagram drawing (try not to laugh too hard…)

Don’t power the servos through the Arduino, get a separate power supply capable of providing the rated current for the servo.

Read @PerryBebbington’s post on why you need a ground.

This is the tutorial @dougp is referring to Common ground and why you need one

Thanks, Perry. I forgot its location and was too lazy to search for it.

Pint of Peroni please :smile:

No Peroni’s on hand. Would you settle for a

1 Like

I saw post #13 marked as solution - then retracted.

Is a YTL preferable?

Thank you for all of your feedback and support through my first sketch!

I’m guessing the initial approach to randomly select a servo to move was the issue. I defined a variable, set the variable using randomnumber and an array containing my pin numbers and did a series of if statements.

I included the sketch below if anyone is interested. Perhaps not the most elegant, but it works!

//////////////////////////////////////////////////

//define sensor pin for IR
#define SENSORPIN 7
//define LED to test IR
#define LEDPIN 13
// Include the Servo library
#include <Servo.h>

// IR variables will change:
int sensorState = 0, lastState=0; // variable for reading the IR status

//this variable will hold a random number generated by the random() function
long randomNumber;

// Create a servo objects
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;

int i;

//////////////////////////////////////////////////

void setup() {

Serial.begin(9600);

// initialize the IR LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// initialize the IR sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup

// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(A0));

// We need to attach the servo to the used pin number
servo1.attach(2);
servo2.attach(3);
servo3.attach(4);
servo4.attach(5);
servo5.attach(6);
}

void loop() {

// read the state of the IR value:
sensorState = digitalRead(SENSORPIN);

// check if the IR sensor beam is broken
// if it is, the IR sensorState is LOW:
if (sensorState == LOW) {
// turn IR LED on:
digitalWrite(LEDPIN, HIGH);

//generate a random number
i = (randomNumber = random(2,7));

//display the random number on the serial monitor
Serial.print("The Random Number is = ");
Serial.println(randomNumber);

if (i == 2) {
// Make servo go to 70 degrees
servo1.write(70);
delay(750);
// Make servo go to 0 degrees
servo1.write(0);
delay(750);
// Make servo go to 70 degrees
servo1.write(70);
delay(750);
// Make servo go to 0 degrees
servo1.write(0);
delay(750);
// Make servo go to 70 degrees
servo1.write(70);
delay(750);
// Make servo go to 0 degrees
servo1.write(0);
delay(750);
};

if (i == 3) {
// Make servo go to 70 degrees
servo2.write(70);
delay(750);
// Make servo go to 0 degrees
servo2.write(0);
delay(750);
// Make servo go to 70 degrees
servo2.write(70);
delay(750);
// Make servo go to 0 degrees
servo2.write(0);
delay(750);
// Make servo go to 70 degrees
servo2.write(70);
delay(750);
// Make servo go to 0 degrees
servo2.write(0);
delay(750);
};

if (i == 4) {
// Make servo go to 70 degrees
servo3.write(60);
delay(750);
// Make servo go to 0 degrees
servo3.write(0);
delay(750);
// Make servo go to 70 degrees
servo3.write(70);
delay(750);
// Make servo go to 0 degrees
servo3.write(0);
delay(750);
// Make servo go to 70 degrees
servo3.write(60);
delay(750);
// Make servo go to 0 degrees
servo3.write(0);
delay(750);
};

if (i == 5) {
// Make servo go to 70 degrees
servo4.write(70);
delay(750);
// Make servo go to 0 degrees
servo4.write(0);
delay(750);
// Make servo go to 70 degrees
servo4.write(70);
delay(750);
// Make servo go to 0 degrees
servo4.write(0);
delay(750);
// Make servo go to 70 degrees
servo4.write(70);
delay(750);
// Make servo go to 0 degrees
servo4.write(0);
delay(750);
};

if (i == 6) {
// Make servo go to 70 degrees
servo5.write(70);
delay(750);
// Make servo go to 0 degrees
servo5.write(0);
delay(750);
// Make servo go to 70 degrees
servo5.write(60);
delay(750);
// Make servo go to 0 degrees
servo5.write(0);
delay(750);
// Make servo go to 70 degrees
servo5.write(70);
delay(750);
// Make servo go to 0 degrees
servo5.write(0);
delay(750);
};

}
else {
// turn IR LED off:
digitalWrite(LEDPIN, LOW);
}

}