Hello all, new to this forum and new to programming with the arduino. My problem is this:
I am trying to control a 5v stepper motor as seen below
https://www.google.com/search?q=5v+stepper+motor&espv=2&source=lnms&tbm=isch&sa=X&ved=0ahUKEwihuOyBwfXSAhXCNxQKHfDIAgUQ_AUICSgC#imgrc=zyWw6CoVh0fTnM:
Using a ultrasonic sensor:
I need the sensor to be able to detect when something is a certain distance away which then triggers the stepper motor to turn an x amount of steps then be able to hold that position until the ultrasonic sensor no longer detects something in its path, then the stepper motor should go back to its origin point. so to recap.
Ultrasonic detects an item > Stepper motor moves to x from origin > Stepper motor holds x position until Ultrasonic sensor no longer detects an item > Ultrasonic sensor no longer detects item > Stepper motor returns back to origin point.
The code that I have now is
///*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/
#define trigPin 4
#define echoPin 3
#define led 2
#define led2 1
#define STEPS_PER_MOTOR_REVOLUTION 32 // Number of steps per revolution of INTERNAL motor in 4-step mode
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64 // Steps per OUTPUT SHAFT of gear reduction
#include <Stepper.h>
int bluePin=8;
int pinkPin=9;
int yellowPin=10;
int orangePin=11;
int currentStep = 0;
bool clockwise = true;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
Serial.begin(9600);
pinMode(bluePin,OUTPUT);
pinMode(pinkPin,OUTPUT);
pinMode(yellowPin,OUTPUT);
pinMode(orangePin,OUTPUT);
digitalWrite(bluePin,LOW);
digitalWrite(pinkPin,LOW);
digitalWrite(yellowPin,LOW);
digitalWrite(orangePin,LOW);
}
void loop() {
int directionStep = clockwise ? currentStep : (4-1) - currentStep;
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 10) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(2);
{
if (distance < 10){
switch(directionStep){
case 0:
digitalWrite(bluePin,HIGH);
digitalWrite(pinkPin,LOW);
digitalWrite(yellowPin,LOW);
digitalWrite(orangePin,LOW);
break;
case 1:
digitalWrite(bluePin,LOW);
digitalWrite(pinkPin,HIGH);
digitalWrite(yellowPin,LOW);
digitalWrite(orangePin,LOW);
break;
case 2:
digitalWrite(bluePin,LOW);
digitalWrite(pinkPin,LOW);
digitalWrite(yellowPin,HIGH);
digitalWrite(orangePin,LOW);
break;
case 3:
digitalWrite(bluePin,LOW);
digitalWrite(pinkPin,LOW);
digitalWrite(yellowPin,LOW);
digitalWrite(orangePin,HIGH);
break;
}
}
if (distance > 10) {
switch(directionStep){
case 0:
digitalWrite(bluePin,LOW);
digitalWrite(pinkPin,LOW);
digitalWrite(yellowPin,LOW);
digitalWrite(orangePin,HIGH);
break;
case 1:
digitalWrite(bluePin,LOW);
digitalWrite(pinkPin,LOW);
digitalWrite(yellowPin,HIGH);
digitalWrite(orangePin,LOW);
break;
case 2:
digitalWrite(bluePin,LOW);
digitalWrite(pinkPin,HIGH);
digitalWrite(yellowPin,LOW);
digitalWrite(orangePin,LOW);
break;
case 3:
digitalWrite(bluePin,HIGH);
digitalWrite(pinkPin,LOW);
digitalWrite(yellowPin,LOW);
digitalWrite(orangePin,LOW);
break;
}
}
currentStep = (++currentStep < 4) ? currentStep: 0;
delay(2);
}
}
///
[b][code]
[/b]
[/code]