Go Down

### Topic: Blinking led and checking the loop (Read 201 times)previous topic - next topic

#### Thunderer

##### Nov 23, 2016, 01:37 pm
I have made a perfect Tic-Tac-Toe analyser. But previously used rgb lights (With some more chips and connecting them with analog port) but now I planned to do with single led but the move of computer should blink. I completed the code but noticed that most time consumes in user move. The code there out (That part)
Code: [Select]
`while(analogRead(0) != 0 && analogRead(1) != 0 && analogRead(2) != 0 && analogRead(3) != 0 && analogRead(4) != 0 && analogRead(5) != 0){ //The code here out}`
Now there 9 variables - u1, u2 ... u9 and if-
u1 = 1 then blink led 13 (pin)
u2 = 1 then blink led 12
....
u9 = 1 then blink led 5 (pin)
But if =0 then do not blink

But suppose u1 = 1 and u5 = 1 then blink both leds

I want just to blink the leds when user is user's chance is there

I know it may be silly for you but I do not know why my brain is not working today

#### Robin2

#1
##### Nov 23, 2016, 01:51 pm
You need to post the complete program.

The use of WHILE may not be appropriate if it is blocking the Arduino from doing other things.

...R
Several Things at a Time
Two or three hours spent thinking and reading documentation solves most programming problems.

#### J-M-L

#2
##### Nov 23, 2016, 01:52 pmLast Edit: Nov 23, 2016, 03:12 pm by J-M-L
Quote
now I planned to do with single led
so you have just 1 LED for the board? how does this work?

Quote
but the move of computer should blink

Quote
I completed the code but noticed that most time consumes in user move. The code there out (That part)
and? you removed that code so we can't see it? and why is that relevant?

(and while is locking you up is not good to blink something on the side by the way)

Quote
Now there 9 variables - u1, u2 ... u9 and if-
u1 = 1 then blink led 13 (pin)
u2 = 1 then blink led 12
....
u9 = 1 then blink led 5 (pin)
But if =0 then do not blink
so now you have 9 LEDs? thought you wanted to do with only one?
usually it's better to use an array when you have many of the same things. will make your programing life easier

Quote
I want just to blink the leds when user is user's chance is there
how do you know the user has a chance? lucky day?

clear as mud to me... sorry... ( )

Now if you ask is to be able to do different things at the same time, then Robin's pointer is a great one to study and here is an example of what your code structure could look like
Code: [Select]
`#define NUMLEDS 9const byte LedPins[] = {5, 6, 7, 8, 9, 10, 11, 12, 13};bool ledBlinkStatus[NUMLEDS]; // true means led needs to blinkconst unsigned long blinkHalfPeriod = 250; // LEDs change state every 250msbool userTurnToPlay = true;// ----------------------------------------------------void setup() {  for (byte i = 0; i < NUMLEDS; i++) {    pinMode(LedPins[i], OUTPUT);    digitalWrite(LedPins[i], LOW); // Led off    ledBlinkStatus[i] = false; // Led not blinking  }  // =======================================================  // FOR DEMO PURPOSE HERE ONLY  // ask for example Leds #0, #3 and #7 to blink  // =======================================================  ledBlinkStatus[0] = true;  ledBlinkStatus[3] = true;  ledBlinkStatus[7] = true;  // =======================================================  // =======================================================}// ----------------------------------------------------void blinkLeds(){  static unsigned long nextBlinkTime = 0ul;  if (millis() - nextBlinkTime > blinkHalfPeriod) {    nextBlinkTime += blinkHalfPeriod;    for (byte i = 0; i < NUMLEDS; i++) {      if (ledBlinkStatus[i]) digitalWrite(LedPins[i], !digitalRead(LedPins[i]));    }  }}// ----------------------------------------------------bool userAction(){  bool isKeyPressed = false;  // check some stuff and update isKeyPressed  // =======================================================  // SOME CODE GOES HERE  // =======================================================  return isKeyPressed;}// ----------------------------------------------------void handleUserTurn(){  if (userAction()) {    // do something to represent the user action    // =======================================================    // SOME CODE GOES HERE    // =======================================================    // and update status for who needs to play    userTurnToPlay = false;  }}// ----------------------------------------------------void handleComputerTurn(){  byte computerMove = 0; // a value between 0 and 8  // calculate computer move  // =======================================================  // SOME CODE GOES HERE TO COMPUTE computerMove  // =======================================================  // update ledBlinkStatus for the selected LED (will start blinking soon)  ledBlinkStatus[computerMove] = true;    for (byte i = 0; i < NUMLEDS; i++)      if (ledBlinkStatus[i]) digitalWrite(LedPins[i], HIGH); // sync them all  // and update status for who needs to play  userTurnToPlay = true;}// ----------------------------------------------------void loop() {  blinkLeds();  if (userTurnToPlay) handleUserTurn();  else handleComputerTurn();}`
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

#### Thunderer

#3
##### Nov 23, 2016, 04:19 pm
Ho, the code is good but it is something different than I thought. Here it is like this divided into three parts
Display - Here only user input moves are displayed by a still led (Actually I am defining the u here)
Process - Process the moves and return to a array where the leds matrix[the column][the row]
Input - Here only led needs to blink (as per I told) when existing from the loop as follow-
while(if none inputs are pressed)
}
Now it becomes normal
Now input as if input = 1 then row 1 column 1 return to the processing arrays .....

Just I have problem in blinking up the leds
There the user has to wait until the led blinks or have to keep it pressed.
In my engine I used many chip to perform parallely so I do not have problem but now problem is there. I have reduced the trees the centre opening tree to nearly to zero as if return input is not at the corner then defeat is clear and so I have also for other trees I have reduced their work.

You helped full previously, now a bit but may be best. You have always helped me you are the best

#### J-M-L

#4
##### Nov 24, 2016, 12:28 pm
OK without seeing your code I'm not sure we can help. the code structure I proposed above is one that leverages the main loop and not blocking anything to maintain blinking when blinking is due and only handle human actions when the user touches buttons for example.

if you want blinking with only one CPU then you need to take a similar approach in your code architecture
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

#### Thunderer

#5
##### Nov 24, 2016, 01:40 pm
May be mine mind returned. I was silly. I simply changed the while loop to do while loop and put your code after changing a bit and expect in your main loop you implanted if user's turn I implemented my while condition and it worked thx a lot. And sorry for that time I could not post the whole code as I had a virus which disabled my clipboard. I have formatted it and backuped data.

Go Up