Go Down

Topic: 30 Second Timer + LED Display + Pager Motor HELP!! (Read 3 times) previous topic - next topic

BigMike

Quote
Thanks for the replies BigMike & mem.

Mike, your code seems very easy and effective in terms of what I need to do. It was clear for the most part, although I had a couple of questions

a) what is the crtMillis value going to be when it loops through the first time? cause it seems like as soon as the value of crtMillis changes,  you set it back to 0.


Lack of comments and poorly named variables makes it harder to understand :)

However, I can't see where I'm setting crtMilli back to zero.  Each time through the loop I grab the current value of the millisecond counter using the line crtMilli = millis(); If it differs from the previous value (prvMilli) then at least one milli second has passed.

Doing a test for different (!=) rather then just greater than means that you don't need to fix the millis() function timer rollover bug mem mentions.

Quote

b) I didn't quite understand the TimerVal --; thing. Is it an int, I presume so. How does it know how much time has passed? Do I set it equal to another millis function?


TimerVal--; is a decrement by one instruction. Equivalent to TimerVal = TimerVal - 1;.  There are pre- and post- decrement and increment instructions: i++; ++i; i-- and --i;

So the timer works by counting down to zero in milliseconds.

Regards,

Mike




Shubs

Big Mike, I managed to figure out what everything stands for and actually have MOSTLY everything running. Thanks for the help again (mem & yourself) Here's my code:

Quote
int rxPin = 0;
int txPin = 1;
int motPin = 2;
int ledPin = 13;

unsigned long PreviousTime = 0;
unsigned long CurrentTime = 0;
int TimerValue = 0;
int Counter = 30;

void setup()  {
 // define pin modes for tx, rx, led pins:
 pinMode(rxPin, INPUT);
 pinMode(txPin, OUTPUT);
 pinMode(motPin, OUTPUT);
 pinMode(ledPin, OUTPUT);
 for (int i = 3; i< 12; i++){
   pinMode(i, OUTPUT);
 }
 // set the data rate for the SoftwareSerial port
 Serial.begin(9600);
}

void loop() {
 CurrentTime = millis();
 if(CurrentTime != PreviousTime){
   PreviousTime = CurrentTime;
   if (Serial.available() > 0){  
     char someChar = Serial.read();
     Serial.print(someChar);
     if (someChar == 'a'){
       motor(ledPin);
       motor(motPin);
     }
     if (someChar == 'b'){
       TimerValue = 1000;
       Counter = 30;
     }
   }
   TimerValue --;
   if(TimerValue <= 0){
     TimerValue = 1000;
     Serial.println(Counter);
     Display(Counter);
     if(Counter <= 0){
       Counter = 31;
     }
     Counter --;
   }
 }
}

void motor(int pinNum)
{
 digitalWrite(pinNum, HIGH);
 delayMicroseconds(5000);
 digitalWrite(pinNum, LOW);
}

void Display(int number){
 TurnOffDisp();
 int ones;
 int tens;
 ones = number%10;
 number = number/10;
 tens = number%10;

 if(ones==0) {
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
   digitalWrite(10, LOW);
   digitalWrite(3, LOW);
   digitalWrite(4, LOW);
   digitalWrite(5, LOW);
 } .....
}


Final question, I need to drive 2 LED Displays (7-segment, common anode) with the countdown running on it. I managed to get one of the digits running (ones) but realized that I don't have enough pins to run the second digit (tens).

I googled a couple of things and read about multilexing/charlieplexing. This requires me to use 2 transistors (1 for the CA pin of each display) which require 2 pins and 7 other pins that run in parallel to the 2 LED displays. Thus, requiring me to use only 9 pins. I also read that it quickly keeps scanning and refreshing the 2 digits fast enough so that the naked eye cannot see if one of them is off.

I just wanted a guideline or a brief idea of what my code should look like. Currently, I am asking my Arduino to change the Pin state based on the number it reads.

Thanks in advance,
Shubs
 

mem

If you need 7 outputs for each of the two lEDs and one for the motor you may have enough pins without multiplexing. You can use the six analog pins as digital outputs 14-19.

Shubs

While I was reading over my code, I read that I have used delayMicroseconds() in the motor function.
Is there any way that I can use the millis() to prevent it from delaying the code?

Quote

void motor(int pinNum)
{
 digitalWrite(pinNum, HIGH);
 delayMicroseconds(5000);
 digitalWrite(pinNum, LOW);
}


I was hoping that I could shove in another millis() and modify it as follows
Code: [Select]
void motor(int pinNum){
unsigned long time = 0;
 time = millis();
 if (time == 0){
   digitalWrite(pinNum, HIGH);
 }
 if (time >= 5000){
 digialWrite(pinNum, LOW);
 time = 0;
 }
}


Although, I have a feeling that 2 millis functions might cause a slight problem. Please shed some light.

Regards,
Shubs


mem

You can simply change  
  delayMicroseconds(5000); // delay 5000 microseconds = 5milliseconds
to
 delay(5); // delay 5 milliseconds


Go Up