Need help combining sketches

Here's the ir breakbeam website Arduino | IR Breakbeam Sensors | Adafruit Learning System , and here's the code

/*
  IR Breakbeam sensor demo!
*/

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

  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  }
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;
}

codes works great , so when the sensor state is low I would like for the stepper to turn on for a certain amount of revolutions/steps which stepsPerRevolution = 2048 then stop and go counterclockwise the same amount of steps . So for a start I would like after ir breakbeam sensor state is low , the ir breakbeam sensor doesn't detect until 60 seconds. Here's the code I'm using rightnow for the motor , it rotates one way only at the moment.

#include <Stepper.h>

const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
const int RevolutionsPerMinute = 17; // Adjustable range of 28BYJ-48 stepper is 0~17 rpm

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
myStepper.setSpeed(RevolutionsPerMinute);
// initialize the serial port:
Serial.begin(9600);
}

void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
;

}

if you want the stepper motor datasheets here are they
Stepper motor basic.pdf (189.8 KB)
Stepper-Motor-28BJY-48-Datasheet.pdf (164.1 KB)
ULN2003 datasheet.pdf (809.7 KB)
Lesson 31 ULN2003A Stepper motor driver.pdf (294.6 KB)

So a summation. 1. Ir breakbeam sensor state is low/ broken I then want it to activate the stepper motor to go clockwise and counter clockwise a 2048 steps then go counter clockwise.
2. Don't allow the ir breakbeam to then activate the motor until 60 seconds has passed or more from the previous sensor state low.
3. Combine both of these sketches into one.
Thanks for the read looking forward to your help.

Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Pleae go back and fix your original post.

Combining Arduino codes.

Another combining sketches.

1 Like

I formatted the codes correctly , as for combining the codes I still need added code to complete and combine them. Appreciate it groundFungus

So have you looked at the two links that @groundFungus posted?

What don't you understand about them?
Especially the first one. :wink:

Well for one Grumpy_Mike I don't know how to add code to the ir breakbeam so it doesn't activate the stepper motor again until 60 seconds passed from initial activation. Second I don't know how to add code to the stepper motor to receive the ir breakbeam code and then activate the code. Third that said code that is activated from the ir breakbeam that gets the stepper motor rotating I don't know how to add code to set a specific amount of steps clockwise then go counterclockwise the same amount. Thanks for the read :slight_smile:

So you have not got to the stage where you want to combine two code, so your question is a bit misleading.

When you see the breakbeam go low, store in a unsigned long variable the value of the current millis() timer.
Then only look at the breakbeam state again when the current value of the millis() timer minus your start time variable is greater than 60000.

You use an if statement where you check the state of the ir breakbeam variable.
This is of course inside the if statement, described above, seeing if it is time to look at the breakbeam state again.

1 Like

I'll be back tommorow after I figure out how to use and incorporate millis in a sensor code. Thanks Grumpy_Mike

Some millis() timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

2 Likes

Hey everyone I read alot about millis functions and watched a few videos about it , first I'll show you my ir breakbeam code that works great here's the website Arduino | IR Breakbeam Sensors | Adafruit Learning System , second I want to explain that I want the millis function to stop the ir breakbeam to not be able to detect sensorstate = low , breakbeam unbroken , everytime time the sensor beam is broken I want to stop it from doing that until 60,000 milliseconds and then it goes back to unbroken. So here's the code.

/* 
  IR Breakbeam sensor demo!
*/

#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); 
  }
  
  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  } 
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;
}

So now I can tell you what I'm having problems with, for one I don't know how to incorporate.

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 60,000;  //the value is a number of milliseconds
const byte  *I DON'T KNOW WHAT TO PUT HERE*

For void setup I don't know how to incorporate
startMillis = millis(); //initial start time** **}**

And for void loop I don't know how to incorporate

currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    digitalWrite(*WHAT TO PUT HERE*, !digitalRead(ALSO WHAT TO PUT HERE));  //if so, change the state of the LED.  Uses a neat trick to change the state
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current.

UKHeliBob , Would appreciate your help , and anyone else

Your topic was moved to its current location as it is more suitable.

Thank you

consider

// using millis as a delay

const byte PinSw  = A1;
const byte PinLed = LED_BUILTIN;

enum { Off = HIGH, On = LOW };

const unsigned long PeriodWait  = 5000;
const unsigned long PeriodLed   =  200;
      unsigned long msecLst;
      unsigned long msec;

enum { Run, Wait };
int mode = Run;

// -----------------------------------------------------------------------------
void
doSomething (void)
{
    if ((msec - msecLst) > PeriodLed)  {
        msecLst = msec;
        digitalWrite (PinLed, ! digitalRead (PinLed));
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    msec = millis ();

    if (Run == mode)  {
        doSomething ();
    }
    else {      // mode == Wait
        if ((msec - msecLst) > PeriodWait)
            mode    = Run;
    }

    if (LOW == digitalRead (PinSw))  {
        mode    = Wait;
        msecLst = msec;
        digitalWrite (PinLed, Off);
    }
}

// -----------------------------------------------------------------------------
void setup (){
    pinMode (PinSw, INPUT_PULLUP);

    pinMode (PinLed,   OUTPUT);
    digitalWrite (PinLed,   Off);

}

@noobhobbyist,

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Thanks gcjr not sure if coding is easy for you , but i'm a total noob , how will I incorporate this to ir breakbeam code? What will that look like?

rename doSomething() to something more descriptive and put the IR detection code inside it. that code should include what's inside the PinSw read if statement

Appreciate the assistance gcjr , I attempted that and got many errors , MOST LIKELY I did It something incorrect due to my experience. Is there a way I can use millis function after the ir breakbeam says broken on the seriel console
image
it turns off the 3.3v/5v pin I'm currently using 3.3v for the powersource, for about 60,000 milliseconds? I'm trying to think of the simplest way to carry this out.

post the code you made changes to

1 Like

Sure gcjr

// using millis as a delay

const byte PinSw  = A1;
const byte PinLed = LED_BUILTIN;

enum { Off = HIGH, On = LOW };

const unsigned long PeriodWait  = 5000;
const unsigned long PeriodLed   =  200;
      unsigned long msecLst;
      unsigned long msec;

enum { Run, Wait };
int mode = Run;

// -----------------------------------------------------------------------------
void
doSomething (void)
{
    if ((msec - msecLst) > PeriodLed)  {
        msecLst = msec;
        digitalWrite (PinLed, ! digitalRead (PinLed));
        /* 
  IR Breakbeam sensor demo!
*/

#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); 
  }
  
  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  } 
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;
}
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    msec = millis ();

    if (Run == mode)  {
        doSomething ();
    }
    else {      // mode == Wait
        if ((msec - msecLst) > PeriodWait)
            mode    = Run;
    }

    if (LOW == digitalRead (PinSw))  {
        mode    = Wait;
        msecLst = msec;
        digitalWrite (PinLed, Off);
    }
}

// -----------------------------------------------------------------------------
void setup (){
    pinMode (PinSw, INPUT_PULLUP);

    pinMode (PinLed,   OUTPUT);
    digitalWrite (PinLed,   Off);

}

You need to merge both setups int one setup and both loops into one loop...
Start with working code.
Copy paste setup of second code into setup of first code.
Try to compile code and see if it still works.
Then (gradually add the loop code of sketch 2 in sketch 1.
Compile after each time.
If you put in a lot of stuff at once you will get a lot of compiler errors at once. It is difficult to find the problem then. Also: only look at the first error. All the others may disappear if you fix the first. And look for errors before (or very near) the point indicated by the compiler.

1 Like

looks like you just copied your code into doSomething().

when i said "put the IR detection code inside it" i meant to put just and only the lines of your code that do the IR detection into doSomething() -- just the code in your loop()

but even that can be simplified

    byte sensor = digitalRead (SENSORPIN);
    if (lastState != sensor)  {
        lastState = sensor;

        digitalWrite (LEDPIN, ! sensor);

        if (LOW == sensor)
            Serial.println ("Broken");
        else
            Serial.println ("Unbroken");
    }

you also need to add the code that changes the mode to set the timer

        mode    = Wait;
        msecLst = msec;

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.