Go Down

Topic: moving stepper motor 28byj48 (Read 28 times) previous topic - next topic

XenofonTza

Hello

i create an application which connects with a server and finally with Arduino and stepper motor. everythinf is fine Arduino receives 1 for moving stepper clockwise and 0 counterclockwise.

if found that code and work fine


//declare variables for the motor pins
int motorPin1 = 8; // Blue   - 28BYJ48 pin 1
int motorPin2 = 9; // Pink   - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
                     // Red    - 28BYJ48 pin 5 (VCC)

int motorSpeed ;     //variable to set stepper speed



//////////////////////////////////////////////////////////////////////////////
void setup() {
//declare the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////
void loop(){


//  if (2 < 3){               // if potentiometer reads 0 to 535 do this
 motorSpeed = (33);  //scale potValue to be useful for motor
 clockwise();                     //go to the ccw rotation function
//  }
//  else {                             //value of the potentiometer is 512 - 1024
//    motorSpeed = (33); //scale potValue for motor speed
//    counterclockwise(); //go the the cw rotation function
//  }
}

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)

void counterclockwise (){
// 1
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(motorSpeed);
// 2
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay (motorSpeed);
// 3
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(motorSpeed);
// 4
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(motorSpeed);
// 5
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(motorSpeed);
// 6
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
delay (motorSpeed);
// 7
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(motorSpeed);
// 8
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(motorSpeed);
}

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 4 to 1
//delay "motorSpeed" between each pin setting (to determine speed)

void clockwise(){
// 1
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delay(motorSpeed);
// 2
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delay (motorSpeed);
// 3
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delay(motorSpeed);
// 4
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
delay(motorSpeed);
// 5
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
delay(motorSpeed);
// 6
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, HIGH);
delay (motorSpeed);
// 7
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
delay(motorSpeed);
// 8
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
delay(motorSpeed);
}


however

i place that code in my project and stepper motor didnt rotate. the "1" and "0" transmited fine to Arduino.

--------------------------


could you please help me.

thank you in advance

XenofonTza


#include <WiFi.h>
#include <SoftwareSerial.h>
////////////////////////////////////////////////

//declare variables for the motor pins
int motorPin1 = 8; // Blue   - 28BYJ48 pin 1
int motorPin2 = 9; // Pink   - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
                     // Red    - 28BYJ48 pin 5 (VCC)

int motorSpeed =5;     //variable to set stepper speed


// Your network SSID (network name).
// TODO: Enter the name of your wifi network here.
char ssid[] = "Tzavelas";

// Your network password.
// TODO: Enter the password of your wifi network here.
char pass[] = "mypass";

// Your network key Index number (needed only for WEP).
int keyIndex = 0;

// Server status flag.
int status = WL_IDLE_STATUS;

// Create WiFi server listening on the given port.
WiFiServer server(4001);

void setup()
{
// Start serial communication with the given baud rate.
// NOTE: Remember to set the baud rate in the Serial
// monitor to the same value.



//declare the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);


// Wait for serial port to connect. Needed for Leonardo only
while (!Serial) { ; }

// Check for the presence of the WiFi shield.
if (WiFi.status() == WL_NO_SHIELD)
{
// If no shield, print message and exit setup.
Serial.println("WiFi shield not present");
status = WL_NO_SHIELD;
return;
}

String version = WiFi.firmwareVersion();
if (version != "1.1.0")
{
Serial.println("Please upgrade the firmware");
}

// Connect to Wifi network.
while (status != WL_CONNECTED)
{
Serial.print("Connecting to Network named: ");
Serial.println(ssid);

// Connect to WPA/WPA2 network. Update this line if
// using open or WEP network.
status = WiFi.begin(ssid, pass);

// Wait for connection.
delay(1000);
}

// Start the server.
server.begin();

// Print WiFi status.
printWifiStatus();
}

void loop()
{
// Check that we are connected.
if (status != WL_CONNECTED)
{
return;
}

// Listen for incoming client requests.
WiFiClient client = server.available();
if (!client)
{
return;
}

Serial.println("Client connected");

String request = readRequest(&client);
//executeRequest(&client, &request);

// Close the connection.
//client.stop();

Serial.println("Client disonnected");
}

// Read the request line. The string from the JavaScript client ends with a newline.
String readRequest(WiFiClient* client)
{
String request = "";

// Loop while the client is connected.
while (client->connected())
{
// Read available bytes.
while (client->available())
{
// Read a byte.
char c = client->read();

// Print the value (for debugging).
Serial.write(c);

if (c == '1'){     

   int microsteps=100;
   for (int i = 0; i<microsteps; i++)       // Iterate for 4000 microsteps.
//      unsigned long start = millis();
//      while (millis() - start <= 5000)
{
   //motorSpeed = (5);  //scale potValue to be useful for motor
   clockwise();      //go to the ccw rotation function
 Serial.write("move up");
}

}
else {     
 
       int microsteps=100;
       for (int i = 0; i<microsteps; i++)       // Iterate for 4000 microsteps.
{
    //motorSpeed = (5);  //scale potValue to be useful for motor
    counterclockwise(); //go the the cw rotation function
  Serial.write("move down");
}

   }

// Exit loop if end of line.
if ('\n' == c)
{

return request;
// Add byte to request line.
request += c;

}

}
}

}


void printWifiStatus()
{
Serial.println("WiFi status");

// Print network name.
Serial.print("  SSID: ");
Serial.println(WiFi.SSID());

// Print WiFi shield IP address.
IPAddress ip = WiFi.localIP();
Serial.print("  IP Address: ");
Serial.println(ip);

// Print the signal strength.
long rssi = WiFi.RSSI();
Serial.print("  Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)

void counterclockwise ()...............

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 4 to 1
//delay "motorSpeed" between each pin setting (to determine speed)

void clockwise()....................

==============================================================================
this is my code

XenofonTza

my code is in the second post..

Go Up