Punching above my weight.

Can someone give me some advice on this code.

basically this code is a controller for a saw, this was not written by myself, ive had a look threw the source and there are bits i understand and then bits i dont.

basically the saw has the following
Motor with 2 limits (MAX & MIN)
some kind of encoder, which works out the positioning via pulses
few buttons, Go, Home, Stop, + increase + decrease measurment.

currently the saw the min limit dose nothing, The max limit works and displays fine but the program tells us the min limit has not been set.

could someone possible take a look at this code and tell me how simple/hard its going to be to get this working ?

ive not got a problem attempting it my self but id like someones professional opition as to weather its going to work or not.

Regards.

testsaw.ino (4.68 KB)

Please just post the code so that everyone can see it.

//Date : 2015-05-05
//Electric saw control system
//Programmer : Hareen Madhusha Bannaheka 
//

#include <LiquidCrystal.h>
#define minMeasure 10 //minimum distance thar saw moves in cm
#define revDistance 500 // dictance of the saw for on revelution in cm
//Pin naming 
short monoflopTime =5;
int go_btn = 22;
int stop_btn=23;
int home_btn=24;
int inc_btn=25;
int dec_btn=26;
int pos_limit=20;
int neg_limit=21;
int moter1=52;
int moter2=53;
int encoderClock=51;
int encoderData=50;
LiquidCrystal lcd(12, 11, 10, 7, 6, 5, 4);//LiquidCrystal(rs, rw, enable, d4, d5, d6, d7) 
long currentPosition=0;
int requiredPosition=0;
boolean incPress=false;
boolean decPress=false;
byte encoderRead[25];
byte revs[12];
byte divs[13];
int revNumber;
int divNumber;
int homelimitled=49;
int stopled=48;
int homerev;
int homediv;

/*Usage: Stop the moter*/
void moterStop(){
  digitalWrite(moter1,HIGH);
  digitalWrite(moter2,HIGH);
}
/*motor stop by stop button*/
void moterStopbtn(){
  digitalWrite(stopled,HIGH);
  digitalWrite(moter1,HIGH);
  digitalWrite(moter2,HIGH);
}
/*Usage: Set the direction of the moter*/
void moterdir1(){
  digitalWrite(moter1,LOW);
  digitalWrite(moter2,HIGH);
}
/*Usage: Set the direction of the moter*/
void moterdir2(){
  digitalWrite(moter1,HIGH);
  digitalWrite(moter2,LOW);
}
/*Usage: max limit occur*/
void maxlimit(){
  moterStop();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Max Limit ");
  while(digitalRead(home_btn)){
    lcdPrint(0,1,"Press Home to reset");
  }
  lcd.clear();
  onStart();
  
  //lcdPrint(0,0,"Max size achieved");
}
/*Usage: set the starting sequence */
void onStart(){  
  while(digitalRead(pos_limit)){
    moterdir1();
  }
  //moterStop();
  readEncoder();
  homerev=revNumber;
  homediv=divNumber;
  digitalWrite(homelimitled,HIGH);
  lcd.clear();
  lcdPrint(0,0,"Current:");
  lcd.print(minMeasure);
  lcdPrint(0,1,"Required:");
  //lcd.print(minMeasure);
}
// to print on LCD
void lcdPrint(int col,int row, String data ){
lcd.setCursor(col,row);
lcd.print(data);
}
//  encoder read
void readEncoder(){
  int output[2];
  for (int i=0;i<25;i++){
    digitalWrite(encoderClock, LOW);
    delayMicroseconds(monoflopTime);
    digitalWrite(encoderClock, HIGH);
    delayMicroseconds(monoflopTime);
    encoderRead[i]=digitalRead(encoderData);
  }
  for(int i=0; i<12;i++){
    revs[i]=encoderRead[i];
  } 
  for (int i=0; i<13;i++){
    divs[i]=encoderRead[i+12];
  }
  divNumber = 0;
  for (int i = 0; i <13; i ++){
    divNumber = (divNumber<<1) | (divs[i]);            
  }
  revNumber = 0;
  for (int i = 0; i <12; i ++){
    revNumber = (revNumber<<1) | (revs[i]);             
  
  }

}

// function for GO button
void go(){
  long rdivNumber=(requiredPosition%revDistance);
  rdivNumber=rdivNumber*4096/revDistance;
  rdivNumber = rdivNumber+divNumber;
  int rrevNumber= int(requiredPosition/revDistance)+revNumber;
  do{
    moterdir2(); 
    readEncoder();
    int incresediv=divNumber-homediv;
    currentPosition=incresediv*revDistance/4096;
    lcd.setCursor(8,0);
    lcd.print(currentPosition);
    if(!digitalRead(pos_limit)){
     maxlimit();
    }
  }
  while(rdivNumber>divNumber && rrevNumber>revNumber);
  
}
//**********************************************
void setup(){
  lcd.begin(16,2);// lcd initilize
  //pin mode assigning
  for (int i=22;i<=28;i++){
    pinMode(i,INPUT);
  }
  pinMode(moter1,OUTPUT);
  pinMode(moter2,OUTPUT);
  pinMode(encoderClock,OUTPUT);
  pinMode(encoderData,INPUT);
  pinMode(homelimitled,OUTPUT);
  pinMode(stopled,OUTPUT);  
  pinMode(neg_limit, INPUT);   
  pinMode(pos_limit, INPUT);
  lcd.print("Electric saw ");
  lcd.setCursor(0, 2);
  lcd.print("control system");
  attachInterrupt(0, moterStopbtn, LOW);
  //attachInterrupt(2, moterStop, LOW);
  attachInterrupt(3, maxlimit, LOW);
  //digitalWrite(moter1,HIGH);
  //digitalWrite(moter2,HIGH);
  digitalWrite(encoderClock, HIGH);
  digitalWrite(homelimitled, LOW);
  digitalWrite(stopled, LOW);
  digitalWrite(moter1, HIGH);
  digitalWrite(moter2, HIGH);
   moterdir1(); // set moter rotate
   onStart(); // run strat sequence 

}
void loop(){
  /* readEncoder();
   lcd.clear();
   lcd.print(divNumber);*/
  // read up button 
  if (!digitalRead(inc_btn)){
  requiredPosition+=minMeasure;
  while(!digitalRead(inc_btn)){};
   
  }
  
  // read down button
  if (!digitalRead(dec_btn)){
  requiredPosition-=minMeasure;
  while(!digitalRead(dec_btn)){};
   
  }
  
  if(!digitalRead(go_btn)){
    digitalWrite(homelimitled,LOW);
    go();
  }
  
  if(!digitalRead(home_btn)){
    digitalWrite(stopled,LOW);
    onStart();
  }
  //Display the required position
  lcdPrint(0,0,"Current:");
  lcd.print(currentPosition);
  lcdPrint(0,1,"Required:");
  lcd.setCursor(9,1);
  lcd.print(requiredPosition);
  lcd.print("  ");
  
}

sorry i thought it was going to be to large to upload.

mathewbuer:
basically this code is a controller for a saw, this was not written by myself, ive had a look threw the source and there are bits i understand and then bits i dont.

Doesn't that mean there is a major risk of you changing something and causing someone a VERY serious injury?

Have you consulted your liability insurers to see if they are happy for you to mess with the code (or me, either) ?

...R

The saw blades are currenlty off.

all that currently works in the motors which drive the device backwards and forwards.

we have a sparky who has disabled the saw blade motors from spinning until everythingi s working,

the saw's control unit has died and we can not get a replacement since the saw is so old.

the saw is used by people who are trained in our workshop to use it.

all code supplied by anyone would be for educational purposes only. we accept we use the code at our own risk/

Regards

Might was well add so safety features while you're at it :slight_smile:
Like a infrared sensor so any with body heat that comes close to the blade, it'll shut off. That might not work though because sawing will generate heat.

basically the user will put the material on the saw, set the size to cut on control panel, press go and the saw will work like it originally did.

if we could get a replacement controller we would but this is our last option.

mathewbuer:
The saw blades are currenlty off.

Strangely, I had assumed that.

I am worried about what will happen when they are turned on again under the control of software that you don't understand.

This is NOT a DIY job. Buy a new saw with a professionally built and safety certified controller.

...R

who wrote the original code that you posted? As a industrial ele I wouldn't consider a arduino for controls but then again I have access to more robust controller. If your sparky is worth his salt then all off the safety's for the saw will be hardwired like e-stop and safety guard interlocks.

"currently the saw the min limit dose nothing, The max limit works and displays fine but the program tells us the min limit has not been set."........................... how does it tell you?

almost looks like someone was coding a custom script for the saw then quit or changed there build as theres references to things like neg_limit and a input pin assigned but no code that I can see.

Even if this was done with a proper controller and ladder logic a programmer would normally be there during start-up to test and ensure all I/O was installed and working correctly.

The arduino code is basically to control the saw head movement, the blades are controlled by another controller which work fine.