Stepmotor and 2 Sensors :((((

Hi guys,

So we are a group of studends working on a project with arduino for the first time.

in our project we have a step motor, a strain gauge sensor and an infrared sensor and they should all work together and give us the results of the sensors continuously. we have the programs but we dont know how to write all these 3 prgrams in one sketch. can some one pls help us?! :slight_smile:

here are the prgrams:

  1. Step motor (one revolution)

#include <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

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

void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}

void loop() {
// step one revolution in one direction:
Serial.println(“clockwise”);
myStepper.step(stepsPerRevolution);
delay(500);

// step one revolution in the other direction:
Serial.println(“counterclockwise”);
myStepper.step(-stepsPerRevolution);
delay(500);
}

  1. strain gauge program:

int sensorPin = 0; // Connect sensor to a0
int sensorReading;

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

void loop(void) {
sensorReading = analogRead(sensorPin);

Serial.print("Sensor value = ");
Serial.println(sensorReading); // the raw analog value
delay(1000);
}

  1. Infrared program:

#define sensor A1
double distanceSmooth = 0;
void setup() {
Serial.begin(9600); // start the serial port
}

void loop() {

// 5v
double volts = analogRead(sensor)0.0048828125; // value from sensor * (5/1024) //iht → double
double distance = 13
pow(volts, -1); // worked out from datasheet graph
distanceSmooth = 0.8 * distanceSmooth + 0.2 * distance;
delay(500); // slow down serial port

if (distance <= 30){
Serial.println(distanceSmooth); // print the distance
}
}

thanks <3

It is not likely that someone will do it for you. Here are a couple of tutorials to help you. Give it a try and if you have trouble, post the code that you attempted, describe what the code actually does and describe how that differs from what you want. Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

How to merge codes.
Another merge Arduino codes.

Try not to use delay() for timing. Delay() blocks execution and can make a program very unresponsive.
Non-blocking timing tutorials:
Several things at a time.
Beginner’s guide to millis().
Blink without delay().

Weell you have this

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

and

int sensorPin = 0;     // Connect sensor to a0
int sensorReading;

and this

int sensorPin = 0;     // Connect sensor to a0
int sensorReading;

that all needs to be in one sketch like so

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor
nt sensorPin = 0;     // Connect sensor to a0
int sensorReading;
int sensorPin = 0;     // Connect sensor to a0
int sensorReading;

Notice you may need to use A1 as A0 is already taken. Also naming the same veriable multiple times might generate an error and other issues.

And you’ll need to combine setup()
kinda like so

 // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
Serial.begin(9600);   
Serial.begin(9600); // start the serial port

It should now be a simple matter of combining all the loop() codes into one loop(). Then going through and getting rid of the redundant thingies, easy-peasy.

Now post the code of your best code attempt at combining the loop() thingies into one loop(), in code tags and properly formatted.

And don’t forget to give those who helped you credit for doing your homework.

@groundFungus thank u for your reply and your help. we actually wrote a code but i was kinda afraid to post it becasue i think its more than wrong ;D

here is our code

#include <Stepper.h>
#define sensor A1
int sensorPin = 0; // Connect sensor to a0
int sensorReading;
double distanceSmooth = 0;
const int stepsPerRevolution = 600; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 3, 5, 4, 6);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println(“clockwise”);
myStepper.step(20 * stepsPerRevolution);
delay(1000);
// step one revolution in the other direction:
Serial.println(“counterclockwise”);
myStepper.step(-20 * stepsPerRevolution);
delay(1000);
sensorReading = analogRead(sensorPin);
Serial.print("Sensor value = ");
Serial.println(sensorReading); // the raw analog value
delay(1000);
double volts = analogRead(sensor) * 0.0048828125; // value from sensor * (5/1024) //iht → double
double distance = 13 * pow(volts, -1); // worked out from datasheet graph
distanceSmooth = 0.8 * distanceSmooth + 0.2 * distance;
delay(500); // slow down serial port
if (distance <= 30) {
Serial.println(distanceSmooth); // print the distance
}
}

becasue i think its more than wrong

That statement conveys no information that we can use to help you.

Describe what the code actually does and describe how that differs from what you want.

Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

You do not need any of the delay()s. the stepper.step() functions block.