Demo entry 6353174

L298N based control

   

Submitted by anonymous on Mar 31, 2017 at 01:40
Language: C. Code size: 2.5 kB.

#include <Servo.h>
#include <Stepper.h>


// create servo name
Servo myservo;

int pos = 0; // variable to store the servo position 
int button = 15; // The button will be on Pin 2 


// SET 2 BUTTON FOR CW AND CCW FOR STEPPER MOTOR X
int BUX1 = 16;
int BUX2 = 17;

const int stepsPerRevolution = 1;
Stepper myStepperX(stepsPerRevolution, 2, 3, 4, 5); 

// SET 2 BUTTON FOR CW AND CCW FRO STEPPER MOTOR Y
int BUY1 = 18;
int BUY2 = 19;

Stepper myStepperY(stepsPerRevolution, 8, 9, 10, 11); 

void setup() 
{ 
 //set the baud rate
 Serial.begin(9600);

 myservo.attach(6); // attaches the servo on pin 9 to the servo object
 pinMode(pos, OUTPUT);
 pinMode(button, INPUT);
 digitalWrite (button, LOW);

 //INITLALIZE THE PIN STATE OF THE BUTTON
 pinMode(BUX1,INPUT);
 pinMode(BUX2,INPUT);
 pinMode(BUY1,INPUT);
 pinMode(BUY2,INPUT);
 digitalWrite(BUX1,LOW);
 digitalWrite(BUX2,LOW);
 digitalWrite(BUY1,LOW);
 digitalWrite(BUY2,LOW);

 
 //SPEED OF THE STEPPER MOTOR
 myStepperX.setSpeed(60);
 myStepperY.setSpeed(60);
 
} 

void loop() 
{ 
 // goes from 0 degrees to 90 degrees
 if (digitalRead(button) == LOW)
 for(pos = 0; pos < 90; pos += 90) 
 {
 myservo.write(pos);
  delay(50);
  // waits 50ms for the servo to reach the position 
 } 
 
 // goes from 90 degrees to 0 degrees 
 if (digitalRead(button) == HIGH) 
 for(pos = 90; pos>=90; pos-=90)  
 {
 myservo.write(pos);
 delay(50);
 // waits 50ms for the servo to reach the position 
 }


 //Reads pressure from the given pin.
 int pressureValue = analogRead(A0);
 float pressure =((pressureValue/1024.0)-0.04)/0.000901; 
 Serial.println();
 Serial.print("Pressure = ");
 Serial.print(pressure);
 Serial.print(" kPa = ");
 Serial.print(pressure/100);
 Serial.println(" bar ");
 delay(1000);

 //READ THE STATE OF THE BUTTON
 int BX1 = digitalRead(BUX1);
 int BX2 = digitalRead(BUX2);
 int BY1 = digitalRead(BUY1);
 int BY2 = digitalRead(BUY2);

// THE CONTROL OF THE STEPPER MOTOR X
 // CW
 if(BX1 == HIGH && BX2 == LOW)
 {
 myStepperX.step(stepsPerRevolution);
 delay(500);
 }
 //CCW
 if(BX1 == LOW && BX2== HIGH)
 {
 myStepperX.step(-stepsPerRevolution);
 delay(500);
 }

// THE CONTROL OF THE STEPPER MOTOR Y
 // CW
 if(BY1 == HIGH && BY2 == LOW)
 {
 myStepperY.step(stepsPerRevolution);
 delay(500);
 }
 //CCW
 if(BY1 == LOW && BY2== HIGH)
 {
 myStepperY.step(-stepsPerRevolution);
 delay(500);
 }
 
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).