Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Countdown stop watch on: December 08, 2012, 03:12:25 am
Thanks for your input Nick,

As I said, I found that program online. I did not write it, I have trouble writing my name.

I have no idea about programming and was hoping to get some useful input.

Your help would be most appreciated.

Best wishes.
2  Using Arduino / Programming Questions / Countdown stop watch on: December 08, 2012, 12:19:33 am
Hi everyone,

I need a start/stop countdown timer as part of a game I am developing. Basically the countdown timer is measuring how quickly a person can do something and then displaying a result between 1 and 99(or 00) if not completed within the alotted time.
Generally the alotted time would be maximum 20 seconds but this figure may vary. Start and finish of the alotted time would be determined by two seperate INPUTs (external switches). The first would go HIGH to initiate the countdown sequence and the second would go HIGH to stop the countdown sequence.


I have found this program and I think it will suit my purposes quite well.

Code:
/*
 59~0 second counter used with Freetronics DMD
 See http://www.freetronics.com/dmd for resources and a getting started guide.
 Note that the DMD library uses the SPI port for the fastest, low overhead writing to the
 display. Keep an eye on conflicts if there are any other devices running from the same
 SPI port, and that the chip select on those devices is correctly set to be inactive
 when the DMD is being written to. 
 */

// you always need the code from here...........
#include <DMD.h> // for DMD
#include <SPI.h> // SPI.h must be included as DMD is written by SPI (the IDE complains otherwise)
#include <TimerOne.h>
#include "SystemFont5x7.h"
#include "Arial_black_16.h"
#define DISPLAYS_ACROSS 1 // change to 2 for two screens, etc.
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN); // creates instance of DMD to refer to in sketch
int a=60;
unsigned long b,c;
boolean n;

void ScanDMD() // necessary interrupt handler for refresh scanning of DMD
{
  dmd.scanDisplayBySPI();
}

void setup()
{
  //initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
  Timer1.initialize( 5000 );           //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
  Timer1.attachInterrupt( ScanDMD );   //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI() 
  dmd.clearScreen( true );   //true is normal (all pixels off), false is negative (all pixels on)
  dmd.selectFont(Arial_Black_16);   
  Serial.begin(9600);
  b=millis(); 
}
// ........... to here!

void drawSecond(int second)
{
  switch(second)
  {
  case 0:
    dmd.drawString( 7,0, "00", 2, GRAPHICS_NORMAL );
    break;
  case 1:
    dmd.drawString( 8,0, "01", 2, GRAPHICS_NORMAL );
    break;
  case 2:
    dmd.drawString( 7,0, "02", 2, GRAPHICS_NORMAL );
    break;
  case 3:
    dmd.drawString( 7,0, "03", 2, GRAPHICS_NORMAL );
    break;
  case 4:
    dmd.drawString( 7,0, "04", 2, GRAPHICS_NORMAL );
    break;
  case 5:
    dmd.drawString( 7,0, "05", 2, GRAPHICS_NORMAL );
    break;
  case 6:
    dmd.drawString( 7,0, "06", 2, GRAPHICS_NORMAL );
    break;
  case 7:
    dmd.drawString( 7,0, "07", 2, GRAPHICS_NORMAL );
    break;
  case 8:
    dmd.drawString( 7,0, "08", 2, GRAPHICS_NORMAL );
    break;
  case 9:
    dmd.drawString( 7,0, "09", 2, GRAPHICS_NORMAL );
    break;
  case 10:
    dmd.drawString( 8,0, "10", 2, GRAPHICS_NORMAL );
    break;
  case 11:
    dmd.drawString( 9,0, "11", 2, GRAPHICS_NORMAL );
    break;
  case 12:
    dmd.drawString( 8,0, "12", 2, GRAPHICS_NORMAL );
    break;
  case 13:
    dmd.drawString( 8,0, "13", 2, GRAPHICS_NORMAL );
    break;
  case 14:
    dmd.drawString( 8,0, "14", 2, GRAPHICS_NORMAL );
    break;
  case 15:
    dmd.drawString( 8,0, "15", 2, GRAPHICS_NORMAL );
    break;
  case 16:
    dmd.drawString( 8,0, "16", 2, GRAPHICS_NORMAL );
    break;
  case 17:
    dmd.drawString( 8,0, "17", 2, GRAPHICS_NORMAL );
    break;
  case 18:
    dmd.drawString( 8,0, "18", 2, GRAPHICS_NORMAL );
    break;
  case 19:
    dmd.drawString( 8,0, "19", 2, GRAPHICS_NORMAL );
    break;
  case 20:
    dmd.drawString( 7,0, "20", 2, GRAPHICS_NORMAL );
    break;
  case 21:
    dmd.drawString( 8,0, "21", 2, GRAPHICS_NORMAL );
    break;
  case 22:
    dmd.drawString( 7,0, "22", 2, GRAPHICS_NORMAL );
    break;
  case 23:
    dmd.drawString( 7,0, "23", 2, GRAPHICS_NORMAL );
    break;
  case 24:
    dmd.drawString( 7,0, "24", 2, GRAPHICS_NORMAL );
    break;
  case 25:
    dmd.drawString( 7,0, "25", 2, GRAPHICS_NORMAL );
    break;
  case 26:
    dmd.drawString( 7,0, "26", 2, GRAPHICS_NORMAL );
    break;
  case 27:
    dmd.drawString( 7,0, "27", 2, GRAPHICS_NORMAL );
    break;
  case 28:
    dmd.drawString( 7,0, "28", 2, GRAPHICS_NORMAL );
    break;
  case 29:
    dmd.drawString( 7,0, "29", 2, GRAPHICS_NORMAL );
    break;
  case 30:
    dmd.drawString( 7,0, "30", 2, GRAPHICS_NORMAL );
    break;
  case 31:
    dmd.drawString( 8,0, "31", 2, GRAPHICS_NORMAL );
    break;
  case 32:
    dmd.drawString( 7,0, "32", 2, GRAPHICS_NORMAL );
    break;
  case 33:
    dmd.drawString( 7,0, "33", 2, GRAPHICS_NORMAL );
    break;
  case 34:
    dmd.drawString( 7,0, "34", 2, GRAPHICS_NORMAL );
    break;
  case 35:
    dmd.drawString( 7,0, "35", 2, GRAPHICS_NORMAL );
    break;
  case 36:
    dmd.drawString( 7,0, "36", 2, GRAPHICS_NORMAL );
    break;
  case 37:
    dmd.drawString( 7,0, "37", 2, GRAPHICS_NORMAL );
    break;
  case 38:
    dmd.drawString( 7,0, "38", 2, GRAPHICS_NORMAL );
    break;
  case 39:
    dmd.drawString( 7,0, "39", 2, GRAPHICS_NORMAL );
    break;
  case 40:
    dmd.drawString( 7,0, "40", 2, GRAPHICS_NORMAL );
    break;
  case 41:
    dmd.drawString( 8,0, "41", 2, GRAPHICS_NORMAL );
    break;
  case 42:
    dmd.drawString( 7,0, "42", 2, GRAPHICS_NORMAL );
    break;
  case 43:
    dmd.drawString( 7,0, "43", 2, GRAPHICS_NORMAL );
    break;
  case 44:
    dmd.drawString( 7,0, "44", 2, GRAPHICS_NORMAL );
    break;
  case 45:
    dmd.drawString( 7,0, "45", 2, GRAPHICS_NORMAL );
    break;
  case 46:
    dmd.drawString( 7,0, "46", 2, GRAPHICS_NORMAL );
    break;
  case 47:
    dmd.drawString( 7,0, "47", 2, GRAPHICS_NORMAL );
    break;
  case 48:
    dmd.drawString( 7,0, "48", 2, GRAPHICS_NORMAL );
    break;
  case 49:
    dmd.drawString( 7,0, "49", 2, GRAPHICS_NORMAL );
    break;
  case 50:
    dmd.drawString( 7,0, "50", 2, GRAPHICS_NORMAL );
    break;
  case 51:
    dmd.drawString( 8,0, "51", 2, GRAPHICS_NORMAL );
    break;
  case 52:
    dmd.drawString( 7,0, "52", 2, GRAPHICS_NORMAL );
    break;
  case 53:
    dmd.drawString( 7,0, "53", 2, GRAPHICS_NORMAL );
    break;
  case 54:
    dmd.drawString( 7,0, "54", 2, GRAPHICS_NORMAL );
    break;
  case 55:
    dmd.drawString( 7,0, "55", 2, GRAPHICS_NORMAL );
    break;
  case 56:
    dmd.drawString( 7,0, "56", 2, GRAPHICS_NORMAL );
    break;
  case 57:
    dmd.drawString( 7,0, "57", 2, GRAPHICS_NORMAL );
    break;
  case 58:
    dmd.drawString( 7,0, "58", 2, GRAPHICS_NORMAL );
    break;
  case 59:
    dmd.drawString( 7,0, "59", 2, GRAPHICS_NORMAL );
    break;
    case 60:
    dmd.drawString( 7,0, "60", 2, GRAPHICS_NORMAL );
    break;
   
  }
}

/*

void loop()
{
  for (int a=60; a>=0; --a)
  {
    drawSecond(a);
    delay(997);
    dmd.clearScreen( true );
  } 
}
*/

void loop()
{
  if (a==19 && n==false)
  {
    dmd.clearScreen( true );
    n=true;
  }
  drawSecond(a);
  c=millis()-b;
  if (c>=1000)
  {
    --a;   
    b=millis();
    if (a<0)
    {
      a=60;
    }
  }
}

However I need it to do two things slightly differently. (I am aware it currently only counts down from 60, but thats easy for me to change so it starts at 99 so I am not worried about that)

1. It needs to be able to start counting down when an input (digital 2) goes HIGH and then stop the countdown when an input (digital 3) goes HIGH and then leave the remaining result showing on the DMD.

2. As it stands the current countdown is one real time second for one displayed second. ie every second the display goes down by one increment. However I want to set it up that for every quarter of a second in real time, it counts down one increment. (naturally this means the countdown timer display will be going 4 times faster than it currently does.)

Now I have tried various ideas, particularly with problem 2 as listed above but just cant seem to get it going and as for problem one..........I really have no idea other than I know it needs to read the input as High or Low etc.

3  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 04, 2012, 05:26:28 pm
Mike, you have hit the nail on the head mate.

Missing ground and as such circuit incomplete.

Excellent example of mind fixation. (this is a major problem for pilots and has been the cause of numerous crashes over the years) In my case, because I was feeling uncertain about the software I was totally fixated on that and not noticing that there was a fundamental error elsewhere..............a missing ground.

As is the case in airline crashes there was also more than one mistake............when ever I checked the signal I used an easy to get to 0V and of course that was not on the Arduino.

My sincere thanks to all involved but especially to Mike for your patience and thought processes.
4  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 04, 2012, 09:03:08 am
Yes it does mate. Been happily doing it now for about 20 mins or so


So still no change unfortunately.
5  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 04, 2012, 08:49:51 am
Mike I just noticed an interesting thing, if I remove the input pin that is plugged into digital 2 and move it to say dig 1 or 0 or analog 1-5 then the same led continues to flash.
While the pin is unplugged nothing happens at all which is what you would expect but we have not set any of the other pins as anything?

By the way the signal is a regulated 5.oV DC input which I understand is well within specs.
6  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 04, 2012, 08:34:04 am
Yes I definitely want a random number every time and yes that could mean the same one quite a number of times in a row.....Some call it luck, I call it variance.

However I left the program running for a good half hour and during that time it would have had the input switch turn on and off many hundreds of times and the chances of the same number coming up that many times are astronomical.

I have added the code and it has not changed anything except that now its selected number 7 as its lock in number so that removes the clue of the first number which I never really believed anyways. (see last post by me)

Looking at the way its coded though isnt the program selecting that random number and calling it inputState and then in the very next line changing it to equal digitalRead(2)?

Just to be 100% certain I have everything the way you intended...........


Code:
byte whichpin = 3; // set to 3 so turning it off is harmless
int inputState=HIGH, oldState=LOW;
void setup() {
    Serial.begin(9600);         
    randomSeed(analogRead(0));
    for(int i=3;i<11;i++) {
     pinMode(i, OUTPUT);
    }
    pinMode(2,INPUT);
}

void loop() {
  inputState = random(0,10);
 inputState = digitalRead(2);
if (inputState != oldState) {  // a change has occoured
            if(inputState == HIGH) { // input high so turn on a random LED
whichpin = random(3,10); // calculate what LED is to be on
digitalWrite(whichpin, HIGH);  // set the LED on
            } // end of input high
            else { // here if input is low
                digitalWrite(whichpin, LOW); // turn of the LED that was on
            } // end of input is low
} // end of a change has occoured
oldState = inputState;
}
7  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 04, 2012, 07:00:21 am
Thanks Mike,

What is happenning now is that everything works perfectly except that the same LED comes on every single time.

I have also reached this stage on numerous occasions and each time I have come up with the following options generally going with option 2 and offering poor syntax or coding and hence the reason I am here.

Either it is not random selecting

Stuck permanently in a loop

the first number selected in a random function is always the same.

Or is it possible that the analogread (0) which is supposed to be noise affected is not noise affected for the first run through?

Is it possible that the "noise" part of analogread has to be turned on? I have extreme doubts about this but hey just throwing it out there.




I just had an idea, is there anyway to run the random selector once doing nothing so that the second pass would end up selecting the useable random number? At least that would prove or disprove theories 3 and 4.

I have spent hours going through other peoples builds trying to find someone with a similar setup but no luck as yet..........well no luck from the point of view that I could fully understand what was being done and therefore to pick out the relevant bits.


PS if it means anything, turning everything off for a few minutes and then turning it all back on again does not change anything at all...........even the same LED blinks which is by the way output 3.

hmmmm..........is that a clue? 3 is the first of the series?  I know it doesnt make sense and yes I am clutching at straws.



By the way, just to confirm for myself that everything is working fine at hardware level, I have tested the multiple blink program which has almost identical OUTLET setup and it works fine.
8  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 03, 2012, 10:50:22 pm
Thanks Bill and Mike.

Unfortunately that code does exactly what I was already able to do.

The problem is that I only want it to generate one number for each pass of HIGH from input switch 2. That input switch will be staying HIGH for a variable ammount of time.
Once it goes to LOW then that should turn whatever output was on HIGH from the first pass through to LOW which it currently does.
Then when the input goes HIGH again, I want one more number generated
And so on.


I have had it at the stage where I can get just one to stay on by entering a WHILE loop but I dont seem to able to exit that loop when the input goes LOW.

That is where I am certain I had it all wrong but because I am struggling to understand the ; and the { } and how they affect things and when they can and cannot be used.

Everytime I had what I considered logical the verifier would force me to delete or add one or the other or tell me my BREAK was outside the loop etc. Then when I got things happy for the verifier the program would get stuck in the loop...........well either that or the random number generator always started at exactly the same number because the same number LED would be lighting every time.
9  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 03, 2012, 08:37:14 am
Cyber, yeah cheers, stupid mistake partly because of exhaustion and partly because I am struggling with the syntax.


Billroy, cheers for that mate and also a perfect example of why I am having so many problems and frustrations...........there simply is no such thing as a whichpin in the code section.

I am used to having a book full of code, there uses, syntax and options sitting next to me so I can compare them all and then pick and choose what I want.Well thats how it was in Basic days........lol. I still have all those old books.

I am totalled for tonight so gunna hit the sack.............Im in Australia and its quite late/early morning actually. However tomorrow I will give that a run but more importantly try to understand what its doing and why.........I consider that as important as having it work.

Im a sparky by trade so the wiring and hardware is pretty much straight forward for me with just the odd calculation that I have to look up here and there that is no longer in my head. But this programming thing.............lol, well.

By any chance is there a decent book or website with the code that you would suggest?

Anyways off to bed and thanks again mate. Will post back, probably with a heap of questions tomorrow.

Best wishes.
10  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 03, 2012, 07:36:35 am
Sorry Mike, I have had enough. I cant work it out and I dont understand what your clues are.

I am wasting my time and I think its better if I just use the picaxe as I am confident I can do that and wont be wasting anyone elses time or my own.

To be perfectly honest I am not impressed with the way all the program codes etc are set out. It makes no sense to me that only some of the stuff is shown under reference and other stuff is not and you have to try and hunt high and low to find it.

I spent a crap load on a book from Arduino that I was told would contain all the code necessary and when I got home I find it just has a bunch of projects and nothing else to help me.

Thanks for everything.

11  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 03, 2012, 06:27:49 am
Cheers Mike, once again, I appreciate your input and its all taken on board. I have not done any programming for about 30 years or whenever the first PCs became available and i had to learn Basic. Naturally over time much of that is now fuzzy and while for this project I could have used Basic and worked with Picaxe controllers, I decided that it might be smarter to learn a new more up to date skill than rehashing an old one. Sooo here I am.

I will start reading up on your new clues and see what can be achieved. lol Once again I learned a crap load today and once again, my sincere thanks for bearing with me.


PAUL.S............Im guessing you are referring to my opening post and your thoughts are appreciated, I think Mike helped me with that part already.  However if you mean the one a couple up then Im not quite sure what you mean but happy to be enlightened.
12  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 03, 2012, 03:30:54 am
OK so I have now spent another 12 hours on this and I am out of ideas.
This is the code and I have made notes saying what I think it is going to do.
I suspect my problem is in the way things are bracketed but despite hours or reading I cant figure it out.

This is how it currently stands..........
Code:
int ranNum;
int signal, oldsignal;
void setup()
{Serial.begin(9600);         
randomSeed(analogRead(0));
    delay(100);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(2,INPUT);
    }

void loop()
{
ranNum=random(3,10);                                      // selects random number between 3 and 9

delay(1000);
signal = digitalRead(2);                                      //signal is high if input is on or low if off

if((signal == HIGH)&&(oldsignal==HIGH))               //if signal is high and oldsignal is low then program goes to 9999 if signal is low and oldsignal is high or low then        program  goes to 3333 if signal is high and oldsignal is high then
{
digitalWrite(ranNum,HIGH);                                //turns on digital output that equals the random number
}
{
while(signal=HIGH)                                             //allows the program to stay here unless
{
if(signal=LOW)                                                   //the input turns off
break;                                                        //at which point the while loop stops
}
{
if(signal=LOW);                                               //3333

return;
}
if(signal=HIGH);                                                //9999

delay(50);
digitalWrite(ranNum,LOW);                              //causing the led to be turnmed off
delay(1000);                                                 
oldsignal=signal;
}}
13  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 02, 2012, 07:53:01 am
Thanks heaps Mike and I totally agree with the concept of writing it myself in order to learn.

Gunna hit the sack and then when fresh, hopefully can think straight and get my head arround it.

Will return tomorrow, hopefully lavishing crap loads of praise on you for your assistance.

In the meantime, my sincere appreciation for your patience and assistance to date.

Your a legend.
14  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 02, 2012, 07:40:34 am
I need to somehow stop the loop but have it reactivated when the variable timer completes its cycle and sends a new input.
15  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 02, 2012, 07:36:08 am
I bet they don't turn on all at once, but rather one at a time, very quickly, since there is no code to turn them off.


lol, you win that bet bill, spot on correct and my bad for not reading Mikes post properly. Who is the idiot that invented tiredness............ smiley-mad smiley-wink
Pages: [1] 2