Instructables

Step 4: Arduino Code

Picture of Arduino Code
"Arduino Code:"

This project requires to peices of code. 'Firmware' that is loaded onto the Arduino microcontroller. And 'Software' that is run by the Processing program on the PC.

The chain of command goes like this - Processing -> Ardcuino Board -> Steppr drivers -> stepper motors -> X-Y stages.

Here is a little more detail of the communication that will be happening, based on my understanding of the way things work....

The Processing software will send commands to the Arduino board over the USB cable. T The Arduino board will take the command and set the specifed output pins to high or low logic states (5V or 0V). The stepper drivers are wired to the Arduino board outputs. So when the drivers see the high and low signals, they send power to the approriate coils in the stepper motors, making them go.

The code below sets up the Arduino board to listen for the commands sent from Processing and then take actions.

You can cut and paste the code in the Arduino IDE. Then verify the code by clicking the 'Play" button. If there are no errors you can then upload it to the board by pressing the upload button - which looks like a right arrow.



// Arduino code:

// Read data from the serial and turn ON or OFF a light depending on the value
//and control stepper motor on RepStrap
char val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 13

#define XstepPin 10
#define XdirPin 7

#define YstepPin 2
#define YdirPin 3

void setup() {

pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
pinMode(XstepPin, OUTPUT);
pinMode(XdirPin, OUTPUT);
pinMode(YstepPin, OUTPUT);
pinMode(YdirPin, OUTPUT);
Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
if (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == 'H') { // If H was received
digitalWrite(ledPin, HIGH); // turn the LED on
digitalWrite(XdirPin,HIGH);
digitalWrite(XstepPin,HIGH);

delayMicroseconds(2);
digitalWrite(XstepPin,LOW);
delayMicroseconds(2);
}

if(val == 'h') {
digitalWrite(ledPin, HIGH); // turn the LED on
digitalWrite(YdirPin,HIGH);
digitalWrite(YstepPin,HIGH);

delayMicroseconds(2);
digitalWrite(YstepPin,LOW);
delayMicroseconds(2);

}
if (val == 'L'){
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
digitalWrite(XdirPin,LOW);
digitalWrite(XstepPin,HIGH);

delayMicroseconds(2);
digitalWrite(XstepPin,LOW);
delayMicroseconds(2);
}

if (val == 'l'){
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
digitalWrite(YdirPin,LOW);
digitalWrite(YstepPin,HIGH);

delayMicroseconds(2);
digitalWrite(YstepPin,LOW);
delayMicroseconds(2);
}

delayMicroseconds(1000); // <<<<<< USE TO CHANGE SPEED <<<<<<<<
}

 
Remove these adsRemove these ads by Signing Up