Go Down

Topic: An Amusement Machine using LEDs (Read 3917 times) previous topic - next topic

TommyKat

Thanks a million Mike, I hope to repay your patience by doing my best to learn rather than sponge. I shall begin coding... But first I thought I better try to explain precisely what I hope to achieve. One set of symbols, but three sets of sequences corresponding to three reels and an ability to display the results of a set of three spins. Something like this:

Grumpy_Mike

Thanks Tommy, sorry so long in replying but I have a Maker Fair in two weeks and their is a lot to do.

The only thing that is a bit vague is how a player is going to keep track about what spin he is on. Also the pattern will start moving as soon as the timer is moved off zero not when released which would look / feel a bit better.

With any project you always implement it a small part at a time, so I think I we are on track to get there. I see no need for using a state machine for this one, so go ahead on the first stage.

TommyKat

#17
Apr 15, 2018, 01:44 pm Last Edit: Apr 15, 2018, 01:48 pm by TommyKat
Hey Mike, I'm just back from fighting bush fires this weekend, (I'm in Sydney Australia) so I've dropped the ball at my end...
Will have a crack at the code tomorrow, but please take your time with any help offered.

So it is not possible to know which spin is in play while the display sequence is in spin mode, but when the reel spin stops we enter a four second looping sequence which could compare to a piece of music of four 'bars'. After the first spin there is a tone in the first bar only, after the second spin there is a beat in the first and second bar, and after the third spin there is a beat in the first, second and  third bars. The fourth bar remains empty to signal the end of the sequence. There is also a small pause at the beginning of each bar to separate the tones, in this way a pair or three of a kind will give a set of identical tones rather than one long tone. Hope that makes sense?

Agreed... releasing the timer would feel like a better place for the sequence to start... Hmm tricky.

TommyKat

#18
Apr 17, 2018, 02:54 am Last Edit: Apr 17, 2018, 03:13 am by TommyKat
OK so work begins on building the array. As a starting point I used a set of old reel strips and transcribed the symbols into three strings, one string for each reel (in this case the 2nd and third reels are identical). Each symbol now has a unique alphanumeric identifier.

I have drawn up a diagram as attached

You will notice that I have veered slightly off course, the reel set has 10 different symbols rather than 8 as previously promised. Am I trying to set up a byte array with a limit of 8 bits per byte and am I now exceeding the binary option and making the pattern masking option too complicated?  Should I just use an easier reel set?




Grumpy_Mike

Quote
Am I trying to set up a byte array with a limit of 8 bits per byte and am I now exceeding the binary option and making the pattern masking option too complicated?
Not sure I follow this, why is it complex. A byte array, the smallest you can have, can store up to 256 symbols per array entry, you only need 10.

TommyKat

That bit of wondering out loud was possibly a step too far... I have been reading and rereading your page on arrays and wondering where I am heading before getting there. I have been writing code in the IDE and slowly learning some of the language, but so far I have nothing that clears verification. I post it here just to show that I am "trying"  :)

Code: [Select]
// Fruitmachine 2

int timer = 250;        // set timing between symbol changes in milliseconds

void setup() {
 
for(int i = 2; i<9; i++) { // declare pin numbers for 10 symbols
   pinMode(i, OUTPUT); //declare pins as outputs
   
 }
}
void loop() {
  // loop along 25 smbol array
  for (int thisPin = 2; 3; 2; 4; 5; 6; 3; 2; 7; 2; 8; 9; 3; 4; 5; 2; 3; 2; 10; 2; 11; 2; 3; 9; 5;)  {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);   
    delay(timer);                 
    // turn the pin off:
    digitalWrite(thisPin, LOW);   
  }
}

dougp

The for() loop in setup is constructed correctly.  The one which loops through 25 symbols is not and won't compile.

Describe in detail what you believe is happening in the first loop.
So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

Grumpy_Mike

#22
Apr 17, 2018, 06:41 pm Last Edit: Apr 17, 2018, 06:48 pm by Grumpy_Mike
To set up a byte array with all the symbols in it you do this at the begging of your program, outside any function.
Code: [Select]

byte symbols[] = {2, 3, 2, 4, 5, 6, 3, 2, 7, 2, 8, 9, 3, 4, 5, 2, 3, 2, 10, 2, 11, 2, 3, 9, 5 }


This has 25 entries, we will get clever later but for the moment just use the number 25.
For the moment assume that this value in the symbols array is the same as the pin number.

To light up each LED according to the sequence of the symbols:-
Code: [Select]

for(int i=0; i<25; i++){
   digitalWrite(symbols[i],HIGH); // will turn on the LED
   delay(600); // so you can see it
   digitalWrite(symbols[i],LOW); // will turn off the LED
  delay(200); // so you can see it off
}


the bit
Code: [Select]
symbols[i] returns a number whos value is in the ith position of the array, so when i is equal to 0 it returns the zeroth element of the array which is a 2, when i is equal to 3 it returns 4, because we start counting array elements from zero.

In your code:-
Quote
for(int i = 2; i<9; i++) { // declare pin numbers for 10 symbols
will only set pins 2 to 8 to be outputs, that is 6 not 10.

As your symbols, and hence the LED numbers run up to 11 the loop must run from 2 to <12

TommyKat

Thanks Mike. To test my understanding (and answer Doug's question) I will set out where think we are at.
 
The code now declares an array of 25 symbols (I have reset the values of these to run from 1 to 10, rather than 2 to 11 which was a misunderstanding confusing pin numbers with values)

The setup assigns an output for pin numbers 2 to 12.

The void loop statement now contains a for loop calling up the declared array and counting upwards with the ++ operator (x=x+1) which rolls through the array sequentially turning  LEDs on for 600 ms and off for 200ms.

The for loop statement
Code: [Select]
for(int i=0; i<25; i++) requires that the 1st integer in the array be set at zero, I think I would like to know why that is - it seems important


The code so far:
Code: [Select]
byte symbols[] = {1, 2, 1, 3, 4, 5, 2, 1, 6, 1, 7, 8, 2, 3, 4, 1, 2, 1, 9, 1, 10, 1, 2, 8, 4 };

void setup() {
 
for(int i = 2; i<12; i++) { // declare pin numbers for 10 symbols
   pinMode(i, OUTPUT); //declare pins as outputs
   
 }
}
void loop() {
  // loop along 25 smbol array
 for(int i=0; i<25; i++){
   digitalWrite(symbols[i],HIGH); // will turn on the LED
   delay(600); // so you can see it
   digitalWrite(symbols[i],LOW); // will turn off the LED
  delay(200); // so you can see it off
   
  }
}
 

dougp

The for loop statement
Code: [Select]
for(int i=0; i<25; i++)
 requires that the 1st integer in the array be set at zero, I think I would like to know why that is - it seems important

I believe you're fundamentally misunderstanding the for() loop. for(int = 0... initializes the loop variable .  Here's an explanation of the for() loop.

Hint - I have the Arduino Reference page bookmarked on my PC.
So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

TommyKat

Thanks for that Doug, as a newby I have been attempting to use Google as my main resource for finding answers to questions, which does indeed throw up a lot of answers which I then wade through trying to sort some logic. There are courses, and ebooks and a zillion forum questions, with dozens of angles which evolve over time. I am starting to realise the best answers are inside the Arduino Resources drop down menu and the File Examples in the Arduino IDE. So now I am looking in these places first...

So the syntax for()loop has three parts: for (initialization; condition; increment) {
Where we use the loop to set the pin outputs the first initialisation value is 2 setting pin 2 as the first value and we count up from there. Simple.
The for() loop that calls up the array is initialised to zero because? Is this a programming need to index the loop to the array, or is it a fail-safe arithmetical device to return a non-negative result when the condition of the i value is tested?

dougp

The for() loop that calls up the array is initialised to zero because? Is this a programming need to index the loop to the array, or is it a fail-safe arithmetical device to return a non-negative result when the condition of the i value is tested?
The former.  The idea is to access the array ( which index begins at zero ) in order so that's where you start.  

The second option you mention does still come into play though.  You NEVER want the array index - the number in the square brackets - to go negative.  You also do not want to allow it to exceed the size of the array.  If you would set up a loop like  for(i = 1; i = 25; i++) two undesirable things ensue.  First, you're not accessing element zero.  Second, there is no element 25, the last legal element is 24.  The memory space you (mistakenly) think is your element 25 actually belongs to some other unknown variable.  Somewhere down the line stomping on this value, or accessing it, will cause the program to malfunction.  Be aware the compiler will not flag this.

If there's any possibility of either of the two bad things happening (like after some calculation) you should add some validation code which tests that the index value is within limits before using it.
So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

Grumpy_Mike

#27
Apr 18, 2018, 06:49 am Last Edit: Apr 18, 2018, 06:59 am by Grumpy_Mike
Quote
(I have reset the values of these to run from 1 to 10, rather than 2 to 11 which was a misunderstanding confusing pin numbers with values)
NO!

This means you will be using pin 1 as one of your LED outputs. Pins 0 & 1 are for serial communication and uploading code, unless you know what you are doing ( and you don't at the moment ) do not use them. So put the array back like it was.

You are using pin numbers AS values.

You can separate them but that will be more confusing to you at the moment because it involves using another array and you are struggling with the concept of an array at the moment without having an array being indexed by another array.

Quote
for(int i=0; i<25; i++)

requires that the 1st integer in the array be set at zero, I think I would like to know why that is - it seems important
Think of it like this:- you want the loop to run 25 times, that <25 means the loop will keep on going until the loop index reaches 25. So the last loop is done with the loop index at 24, that means the loop index is NEVER 25, always less than it. If you started the loop index at 1 then you would only do one less than the number in the middle, so by starting at zero you get to do the number of loops you see after the < in the middle. It also matches up the loop index withe the array index when you are using an array but you will note that even when you are not using an array you still structure it like this.

TommyKat

#28
Apr 18, 2018, 07:52 am Last Edit: Apr 18, 2018, 08:07 am by TommyKat
OK Mike, back on track and starting to see the light - the array is a set of pin outputs ordered to replicate my reel symbol values and could be transposed to any suitable sequence of pins on the board - but should then remain constant for subsequent reels.

So the declared array is transposed up to avoid pin 0 and pin 1 effectively notching it up one place, and the setup for()loop reflects this... While the void for()loop that calls up the declared for() loop is notched back a place because it needs to start at zero.

Phew (I think)

the more often I go over this the more likely it will become 2nd nature.

So I think I now need to work in the timer to initiate the void for()loop and hopefully turn it off mid flow.
So I need an input function that will kick off the program and then interrupt the program.
I am looking through the resources...

Grumpy_Mike

Quote
While the void for()loop
No it is not called  void for loop.
It is called a loop function. That is it is a function called loop. The type of function it is, is a void function. That means it returns no variable when it exists.

The for statement makes a section of code repeat, specifically the one instruction following the statement. A number of statements can be made to repeat if they are contained in braces {} .

The two things while they use similar words are very different things.

Quote
and hopefully turn it off mid flow.
We are only using a for statement in the code for a test to get it working and for you to see the results. In the next stage we will remove this statement.
You see I have in mind a plan to teach you coding based on what you want to end up with, so just follow what I say when I say it please.

Go Up