This is just something that I'm wondering about. I was doing a project very recently for a robotics team where I had this CNC shield kit. I also had this QR/Barcode scanner that would read barcodes on packages, a I2C 16 by 2 lcd screen with three buttons to select a number on the lcd screen, a servo motor, and an electromagnet which served as a gripper. How it would work is you scan a package via barcode and barcode scanner. The scanner then got the number that the barcode represented and it would insert it into the cell that went along with that barcode. Then you go to the LCD screen and select which number you want. The machine would then go and retrieve the package that was in that numbered cell and barcode value. Afterwards it would drop it off representing the package being delivered. Here is a video where it scans a package with the barcode value of one and then retrieves 1 with a value of 7. (the video was too big so I broke it into 2 halves)
IMG_0873_Trim (2).zip (7.6 MB)
IMG_0873_Trim(3).zip (7.2 MB)
I controlled the electromagnet with a relay and 9 volt battery.
Here is the code(I made it so it just demonstrated specifically storing package 1 and retrieving package 7 so that's why the step values are fixed)
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
//#include<Servo.h>
SoftwareSerial mySerial(13,12);
// A1 is up button, A2 Select, and A3 Down
LiquidCrystal_I2C lcd(0x27,16,2); // run ic2_scanner sketch and get the IC2 address, which is 0x27 in my case
int count = 1;
int pre = count;
int stepX = 2;
int stepY = 3;
int stepZ = 4;
int DirX = 5;
int DirY = 6;
int DirZ = 7;
const int t = 1000;
const int Y = 2000;
const int Z = 2000;
Servo A;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(11,OUTPUT);//pin 11 controlled the relay and turned the Electromagnet on and off
pinMode(stepX,OUTPUT);
pinMode(stepY,OUTPUT);
pinMode(stepZ,OUTPUT);
pinMode(DirX,OUTPUT);
pinMode(DirY,OUTPUT);
pinMode(DirZ,OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
A.attach(10);
A.write(0);
}
void loop() {
if(mySerial.available()){
char C = mySerial.read();
Serial.print(C);
delay(5);
digitalWrite(DirY,LOW);
for(int x =0; x<1200; x++){
digitalWrite(stepY,HIGH);
delayMicroseconds(Y);
digitalWrite(stepY,LOW);
delayMicroseconds(Y);
}
digitalWrite(DirX,HIGH);
for(int x = 0; x<1150; x++){
digitalWrite(stepX,HIGH);
delayMicroseconds(t);
digitalWrite(stepX,LOW);
delayMicroseconds(t);
}
A.write(90);
digitalWrite(DirY,HIGH);
for(int x =0; x<400; x++){
digitalWrite(stepY,HIGH);
delayMicroseconds(Y);
digitalWrite(stepY,LOW);
delayMicroseconds(Y);
}
digitalWrite(11,HIGH);
delay(3000);
digitalWrite(DirY,LOW);
for(int x = 0; x<1200; x++){
digitalWrite(stepY,HIGH);
delayMicroseconds(Y);
digitalWrite(stepY,LOW);
delayMicroseconds(Y);
}
A.write(0);
digitalWrite(DirX,LOW);
for(int x = 0; x<1150; x++){
digitalWrite(stepX,HIGH);
delayMicroseconds(t);
digitalWrite(stepX,LOW);
delayMicroseconds(t);
}
digitalWrite(DirZ,LOW);
for(int x = 0; x <4000; x++){
digitalWrite(stepZ,HIGH);
delayMicroseconds(Z);
digitalWrite(stepZ,LOW);
delayMicroseconds(Z);
}
digitalWrite(11,LOW);
delay(2000);
digitalWrite(DirX,HIGH);
for(int x = 0; x<250; x++){
digitalWrite(stepX,HIGH);
delayMicroseconds(t);
digitalWrite(stepX,LOW);
delayMicroseconds(t);
}
digitalWrite(DirX,LOW);
for(int x = 0; x<250; x++){
digitalWrite(stepX,HIGH);
delayMicroseconds(t);
digitalWrite(stepX,LOW);
delayMicroseconds(t);
}
digitalWrite(DirZ,HIGH);
for(int x = 0; x <4000; x++){
digitalWrite(stepZ,HIGH);
delayMicroseconds(Z);
digitalWrite(stepZ,LOW);
delayMicroseconds(Z);
}
}
if(digitalRead(A2) == LOW){
if(count == 12){
;
}
else{
pre = count;
count++;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(pre);
lcd.setCursor(0,1);
lcd.print("> ");
lcd.print(count);
delay(500);
}
}
if(digitalRead(A3) == LOW){
if(count == 1){
;
}
else{
pre = count;
count--;
lcd.clear();
lcd.setCursor(0,1);
lcd.print(pre);
lcd.setCursor(0,0);
lcd.print("> ");
lcd.print(count);
delay(500);
}
}
if(digitalRead(A1) == LOW){
digitalWrite(DirY,LOW);
for(int x =0; x<4300; x++){
digitalWrite(stepY,HIGH);
delayMicroseconds(Y);
digitalWrite(stepY,LOW);
delayMicroseconds(Y);
}
digitalWrite(DirZ,LOW);
for(int x = 0; x <4000; x++){
digitalWrite(stepZ,HIGH);
delayMicroseconds(Z);
digitalWrite(stepZ,LOW);
delayMicroseconds(Z);
}
digitalWrite(11,HIGH);
digitalWrite(DirZ,HIGH);
for(int x = 0; x <4000; x++){
digitalWrite(stepZ,HIGH);
delayMicroseconds(Z);
digitalWrite(stepZ,LOW);
delayMicroseconds(Z);
}
digitalWrite(DirX,HIGH);
for(int x = 0; x<1150; x++){
digitalWrite(stepX,HIGH);
delayMicroseconds(t);
digitalWrite(stepX,LOW);
delayMicroseconds(t);
}
digitalWrite(DirY,LOW);
for(int x =0; x<1500; x++){
digitalWrite(stepY,HIGH);
delayMicroseconds(Y);
digitalWrite(stepY,LOW);
delayMicroseconds(Y);
}
digitalWrite(11,LOW);
delay(500);
while(1);
}
}
So in the video it worked fine. However about a couple of days after that when I powered it up sometimes the arduino would skip some step. Like it wouldn't go right sometimes on the X-axis and then when it went left I didn't have any limit switches and it would ram itself against the end. Or it would stop short during the mySerial.available() if statement and wouldn't do anything when I made A1==LOW after. If it's truly because the arduino is imperfect then would a Raspberry Pi be better or some other microcontroller? Sorry for the long post