Go Down

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

Thunderer

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

Thx in advanced

Robin2

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 pm Last 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
how can a move 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 9
const byte LedPins[] = {5, 6, 7, 8, 9, 10, 11, 12, 13};
bool ledBlinkStatus[NUMLEDS]; // true means led needs to blink
const unsigned long blinkHalfPeriod = 250; // LEDs change state every 250ms
bool 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

Thunderer

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)
blink the leds up
}
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

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

Thunderer

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
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy