Go Down

Topic: A rotating array... thing (Read 548 times) previous topic - next topic

metalmm

Hey folks, my project has been moving along slowly but surely, but I've run into a little bit of a "best programming practices" snag

The project: I have a turntable with 4 stations, parts are loaded at station 1, checked at 2 and 3, then put down a chute at 4 depending if they are good or not, right now I'm just doing one part at a time, tracking each station variable as it passes through, then resetting them to a resting state once it finishes

This all works fine, with one at a time, here's what I want to do.


I want each turntable location to be tracked so I can do ~3 parts at once, what station it is at, and what status that station is in, I can reset each turntable location to zero upon completion.  So I started mapping this out in my mind and it turned into a huge goliath of dozens of variables, and then placeholder variables to hold onto them while they all copy over oneanother as the stations rotate, it was a total mess.

So I thought there's got to be a better way of keeping track of something like that beyond my caveman usage of ints!  Maybe a string? I have no idea!

jremington

#1
Sep 27, 2017, 07:21 pm Last Edit: Sep 27, 2017, 07:26 pm by jremington
Use an array of structures, which can hold any sort of relevant information in one place, identifiable by a single index (e.g. 0 to 3).

See this tutorial, and this one for an array of structures.


DVDdoug

#2
Sep 27, 2017, 07:33 pm Last Edit: Sep 27, 2017, 07:43 pm by DVDdoug
Quote
tracking each station variable as it passes through
I have no idea what that means...   What's a "station variable"?  How many of these varibles are there, and what do you mean by "tracking"?   Is this information displayed somewhere, or stored somewhere?

I'm guessing you need one or more arrays.  You can think of an array as a chart or a table, or just a series of data.     i.e. You could store the high-temperature over the last 100 days in a 100-element (1 dimensional) array.   A (uncompressed) digital audio file is essentially a big array (CD audio requires 44,100 16-bit values for each second of audio.   ...Times two, since it's 2-channel stereo.)

A structure is like a database with different types of variables.   For example, you can have a database-type structure to keep track of all the information related to each employee such as name address, age, gender, salary, etc.     

metalmm

Use an array of structures, which can hold any sort of relevant information in one place, identifiable by a single index (e.g. 0 to 3).

See this tutorial, and this one for an array of structures.
This sounds exactly like what I was trying to do, I'll give it a shot, thanks

metalmm

Use an array of structures, which can hold any sort of relevant information in one place, identifiable by a single index (e.g. 0 to 3).

See this tutorial, and this one for an array of structures.


Could I have an assist here, Ive been poking around various tutorials and making a test sketch, it looks like I can define a structure, but can't seem to call on it afterwards  void loop() "Bay1 not defined in this scope".

do I need to define a variable and then pull the information out of the structure to read it or did I declare it wrong?

const int led1 = 1;
const int led2 = 2;
const int led3 = 3;
const int led4 = 4;
const int led5 = 5;


void setup()
{
  pinMode(13, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);

  //BEGIN STRUCTURE
  struct Baytest {
   int baynumber;
    byte passfail;
    int location;
  };
  struct Baytest Bay1 = {1, 0, 1};
  struct Baytest Bay2 = {2, 0, 2};
  struct Baytest Bay3 = {3, 0, 3};
  struct Baytest Bay4 = {4, 0, 4};
}


void loop()
{
  if (Bay1.baynumber = 1){
  digitalWrite(led1, HIGH);
    delay(1000);} // Wait for 1000 millisecond(s)
  digitalWrite(led1, LOW);
  delay(1000); // Wait for 1000 millisecond(s)


jremington

#5
Sep 28, 2017, 05:33 pm Last Edit: Sep 28, 2017, 05:35 pm by jremington
You are defining the structs as local variables and so they are valid only in setup().

Define them as globals, outside of setup() and loop(), so that they are accessible in both. Study these examples carefully.

metalmm

Oh derp, I completely forgot to close setup, its working now

jremington

#7
Sep 28, 2017, 05:44 pm Last Edit: Sep 28, 2017, 07:46 pm by jremington
Code: [Select]
if (Bay1.baynumber = 1){

Oops!

aarg

If you're after best practices, you should continue the task of factoring code. You can specify the LED pin in the struct, and you can collapse the long list of structs to an array of structs.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

lastchancename

Next step, make your code more readable...
Code: [Select]

switch (Bay1.baynumber) {
  case 1:
    digitalWrite(led1, HIGH);
    delay(1000);} // Wait for 1000 millisecond(s)
    digitalWrite(led1, LOW);
    delay(1000); // Wait for 1000 millisecond(s)
    break:
  case 2:
    // other stuff
}

Then look into eliminating the delay() calls.

In reality, i expect all those cases could be rewritten to remove duplication of code, and using more arrays.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

Go Up