Notifications
Clear all

Questions about changing to array from buttons  

Page 3 / 7

robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@gam

Perhaps do a flow chart?

The question IS_ANY_TOOL_ON becomes a call to a subroutine checkSensor() that returns a number. If that number is -1 then no tool is on otherwise the number is the number of the tool that is on and the position the disc has to be at. It is the number that is used to answer the next question IS_DISC_AT_TOOL_POSITION coded as does the number of the sensor = the number of the disc position?

and so on ...

The blue boxes are ACTIONS. This is code to move the disc to some position or turn the pump on or off.

Programming is converting steps you could do yourself written in a way the computer can execute.

To enlarge image right mouse click and choose Open link in new window.

flowChart

 

 

This post was modified 1 month ago 3 times by robotBuilder

ReplyQuote
MadMisha
(@madmisha)
Estimable Member
Joined: 1 year ago
Posts: 130
 

@gam I hope this helps

 

int numberTools = 8; //Number of tools connected
int toolSens[] = {A0, A1, A2, A3, A4, A5, A6, A7}; //Pins of tool sensors
int threshold[] = {570, 570, 570, 570, 570, 570, 570, 570}; //Threshold for when to turn on
int ventLoc[] = {1, }; //Location for the stepper motor for that tool in steps or degrees
bool onState = false; //If system is on. Starts in off state aka false, change to true when system is on
int toolOn = 0; //What tool it is currently on
int cleanOut = 0; //Timer to clear the line
int cleaningTime = 120; //How long to keep on before shutting down
int activeSensor;

//Please note that I have never used an int like this before(as a function)
int checkSensor(){
  for (int i = 0; i < numberTools; i++) {
    if (analogRead(toolSens[i]) > threshold[i]) return i; //i represents current index being tested(starting with 0) and = toolSens[i], threshold[i], and ventLoc[i]
  }
  return -1; //If no value was true then return -1
}

//This is the section for the shutdown process. A countdown to clear the line then turns vac off. 
//It also checks for new tool being used
void goOff(){
  cleanOut = cleaningTime; //Set the countdown time to what you entered above
  while (cleanOut != 0) { //While the counter does not = 0
    cleanOut--; //Lower the counter by 1(count down)
    if (activeSensor != -1 ) { //If tool is turned on durring countdown
      toolOn = activeSensor; //Sets the tool that is on and blast gate to move to(I forgot this before)
      changeTool(); //Change to new tool
      }
    }
   //Shutdown vac here
   onState = false; //set the state to off, I forgot this before
  }

void changeTool(){
  if(onState == true) { //If system is on, turn off vac to move(prevents binding)
    //Turn off vac here
    delay(100); //Long enough to loose suction
    }
  //Move stepper here using ventLoc[toolOn], You can rearrange when toolOn is set to enable math for the move if needed
  onState = true;
  //Vac on here
  }

void setup() {

  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A2, INPUT);
  // ..
  pinMode(A7, INPUT);

}

void loop() {
  if (onState == false){ //If it is off
    activeSensor = checkSensor(); //check to see if tools are active
    if (activeSensor != -1) { //If it did not return -1 then it returns the tool that is active
      toolOn = activeSensor; //Sets the tool that is on and blast gate to move to
      Serial.println("Turning on");
      Serial.println(activeSensor);
      changeTool(); //Changes tool selection
      }
    }
   else { // If the system is on
    activeSensor = checkSensor(); //Check sensors
    if (activeSensor == toolOn) { //If the active tool is the same one it is set to
      delay(400); // Do nothing
      }
    else {//If not the tool it is on has changed
      toolOn = activeSensor; //Sets the tool that is on and blast gate to move to
      changeTool(); //change the blast gate position
      }
    }
  delay(200); 
}

 

 

 So, using int as a function is not something I have done before and I borrowed that from @robotbuilder and I hope it works because I like it. I also used his suggestion on how to post code here and it worked better than any other method I have tried here, thanks!

 

 The way I set it up is that the lists for toolSens, threshold and ventLoc will all correlate to each other. As in the first entry of (0) will relate to each other. So then I stepped through the numbers to see what tool is active, it starts at 0 and checks the sensor that is 0 to see if it the threshold of 0 and then in another function it will move it to position 0. So if the statement was true, it will return the number it is currently on, if it is not true then it will increase the number (like 1) and test that one. If none of them returned true then it returns -1 (I also barrowed that from his code). If -1 is returned then the loop(or anywhere else it checks) will know that no tool is on.

 

 If for some reason you need to keep the previous number it was on before the move then change the active tool after you move the stepper motor. You could even pass the i to the function and use math between ventLov[i] and ventLoc[activeSensor] to do the move and then set activeSensor to the passed value(i).

 


ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@madmisha

 So, using int as a function is not something I have done before and I borrowed that from @robotbuilder and I hope it works because I like it.

The int means the function will return an integer.  It is where you declare what type of variable will be returned.

void means the function will not return anything.

I was going to show how make a one to one conversion of pseudo code or a flowchart into actual code but I ran out of time tonight.

The question boxes would return a TRUE or FALSE value (YES or NO) which is a bool type.

 

bool IS_ANY_TOOL_ON(){
  activeSensor = -1   // assume no tool is on
  for (int i = 0; i < numberTools; i++) {
    if (analogRead(toolSens[i]) > threshold[i]){
        activeSensor = i;
    }
  }
  if (activeSensor == -1){
     return FALSE;
  else
     return TRUE;
  }
}

bool IS_DISC_AT_TOOL_POSITION(){
  if (discPosition == activeSensor){
    return TRUE;
  else
    return FALSE;
}

bool IS_VACUUM_PUMP_ON(){
  if (pumpOn == 1){
    return TRUE;
  else
    return FALSE;
}

 

 

More examples:

// Method prototypes
void printSomeMath();
double doSomeMath(float x, float y, double angle);

// The implemented methods
void printSomeMath()
{
  Serial.println(doSomeMath(2.0f, 9.7f, 93.572000));
}

double doSomeMath(float x, float y, double angle)
{
  return (x * y) + (x * angle) + (y * angle) - (x / y);
}


 

This post was modified 1 month ago 3 times by robotBuilder

MadMisha liked
ReplyQuote
YurkshireLad
(@yurkshirelad)
Estimable Member
Joined: 4 months ago
Posts: 240
 

You could do:

 

bool IS_ANY_TOOL_ON(){
  for (int i = 0; i < numberTools; i++) {
    if (analogRead(toolSens[i]) > threshold[i]){
        return TRUE;
    }
  }
  return FALSE;
}
This post was modified 1 month ago by YurkshireLad

ReplyQuote
GAM
 GAM
(@gam)
Eminent Member
Joined: 4 months ago
Posts: 46
Topic starter  

@robotbuilder Like the flow chart I probably should have started that way!

Thanks

 


ReplyQuote
GAM
 GAM
(@gam)
Eminent Member
Joined: 4 months ago
Posts: 46
Topic starter  

@madmisha  Just read through this (for the first time) and need to check it out!  Again looks like you did a lot of work and I hope you weren't up at 3:41 AM like to post said.  I will copy this to Pro and work with it. from what I read I have a better understanding now.

Thanks 

Greg 


ReplyQuote
GAM
 GAM
(@gam)
Eminent Member
Joined: 4 months ago
Posts: 46
Topic starter  

@yurkshirelad  Thanks for the reply I will check it out!

Greg


ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@gam

In practice I never start with a flow chart although I might use one to explain the flow of the program to others (or myself later). It is useful I think to write down the flow of instructions in a human language form to check for logical errors.

Here is an example where the logical flow is easier to follow (I think) based on the flow chart. The function names are self commenting.

To turn it into working code you have to fill in the instructions between the curly brackets for each of the eight functions.

The whole idea is to make code as readable as possible. Personally I can follow a spatial visual representation better than a linear verbal description.

 

 

bool ANY_TOOL_ON(){}               // returns TRUE or FALSE
bool DISC_AT_TOOL_POSITION(){}     // returns TRUE or FALSE
bool VACUUM_ON(){}                // returns TRUE or FALSE

int  DISC_POSITION(){}             // returns disc position
int  TOOL_NUMBER(){}               // returns tool number
void TURN_ON_VACUUM(){}              // turns on VACUUM
void TURN_OFF_VACUUM(){}             // turns off VACUUM
void MOVE_DISC_TO_TOOL_POSITION(){}  // moves disc to tool position

void setup(){}

void loop(){
  if ( ANY_TOOL_ON() == true ){
    if (DISC_AT_TOOL_POSITION()==true){
      if (VACUUM_ON() == false){
        TURN_ON_VACUUM();
      }
      else{
        TURN_OFF_VACUUM();
        MOVE_DISC_TO_TOOL_POSITION();
        TURN_ON_VACUUM();
        }
      }
  }
}

 

flow

 

 

This post was modified 1 month ago by robotBuilder

ReplyQuote
YurkshireLad
(@yurkshirelad)
Estimable Member
Joined: 4 months ago
Posts: 240
 
Posted by: @gam

@yurkshirelad  Thanks for the reply I will check it out!

Greg

I thought I would post an optimisation to that code fragment, nothing more. 🙂


GAM liked
ReplyQuote
GAM
 GAM
(@gam)
Eminent Member
Joined: 4 months ago
Posts: 46
Topic starter  

@robotbuilder  Like this as well.  All of you have given so much help!  I need to sit with all this and work on it.   Hope some day I can show a completed project!

Thanks to all

Greg


ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@gam

I just noticed I didn't turn off the vacuum if there was no tool in operation.

Also I just discovered I can change the  colors in the source code after it has been inserted!

 

bool ANY_TOOL_ON(){}                 // returns TRUE or FALSE
bool DISC_AT_TOOL_POSITION(){}       // returns TRUE or FALSE
bool VACUUM_ON(){}                   // returns TRUE or FALSE

int  DISC_POSITION(){}               // returns disc position
int  TOOL_NUMBER(){}                 // returns tool number
void TURN_ON_VACUUM(){}              // turns on VACUUM
void TURN_OFF_VACUUM(){}             // turns off VACUUM
void MOVE_DISC_TO_TOOL_POSITION(){}  // moves disc to tool position

void setup(){}

void loop(){
  if ( ANY_TOOL_ON() == true ){
    if (DISC_AT_TOOL_POSITION()==true){
      if (VACUUM_ON() == false){
        TURN_ON_VACUUM();
      }
      else{
        TURN_OFF_VACUUM();
        MOVE_DISC_TO_TOOL_POSITION();
        TURN_ON_VACUUM();
        }
      }
  }
  else {
    TURN_OFF_VACUUM();   //  **** TURN OFF VACUUM IF NO TOOL ON  ****
  }
}

 

 

This post was modified 1 month ago 2 times by robotBuilder

ReplyQuote
MadMisha
(@madmisha)
Estimable Member
Joined: 1 year ago
Posts: 130
 

I love the form and function that @robotbuilder posted but I would be against the use of the bool function for determining if a tool is on. I don't think if a tool is on is as useful as what tool is on. But you can easily take this and merge it with what I posted. Also, naming is great, although I would stick to the recommended formatting. I really need to be better at sticking to naming conventions at all times. It's a must when I use Kotlin so I don't know why I drop it when I do simple things like this.

 

@gam Check everything I posted. I think I wrote that in about 20-30 minutes and most of that time I was complaining about the IDE faults. I often make mistakes and omit even the most obvious of things on quick projects.


ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@gam

Reading all your posts again I see you have a relay for each vac motor. For some reason I thought there was only be one vac motor and the rotating disc was used to attach each tool dust outlet to some common pipe to the dust collector? If there is a vac motor for each tool can't they all feed into the same dust bin? Each outlet would only need a one way flap.

To enlarge click right mouse button and select open link in new window.

dustCollection

 

This post was modified 1 month ago 2 times by robotBuilder

ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@madmisha

I don't think if a tool is on is as useful as what tool is on.

The code has both.
ANY_TOOL_ON()
TOOL_NUMBER()
It has to do with readability.
Also there was the issue of more than one tool being used at the same time.

Anyway I have completely misunderstood the physical layout and am confused as to the use of the DISC. Probably because the original code only referenced one vac motor.
const int vacPin = 4; //to led to simulate Vacuum relay

2666 image

 

This post was modified 1 month ago 2 times by robotBuilder

ReplyQuote
GAM
 GAM
(@gam)
Eminent Member
Joined: 4 months ago
Posts: 46
Topic starter  

@robotbuilder, @madmisha,

I do have 3 vacuum motors at this time just to increase the volume of vacuum. They are running on 1 relay at this point and that is fine. When working on the design (and had 3 motors) it crossed my mind that there may be times on smaller applications with more restrictive hose nozzles that less vacuum is needed and could in fact put some strain on the motors as it would be like a particle blockage.  That's the only reason I had the extra relays in place, if I decided to include (Code) number of motors for specific tools.   Currently the motors are plugged into a strip with individual on/off switches that I can manage it with.

vac assy2
vac assy1

You might notice the ultrasonic sensor in the same picture.  That to is for the future so I can detect how full the sawdust container is with out taking it apart to check.  That might be easier to just do separate project project at that time. 

Recently read an article about Hilego HC-05  BT module that as I understand it will show information to phones/tablets, which I have a number of outdated ones lying around. Might make a good monitor display for the system.

First thing first,  need to work on what you given me.  


ReplyQuote
Page 3 / 7