Go Down

Topic: simultaneous analog input read and led flash (Read 423 times) previous topic - next topic

khoadley85

Howdy,
   
         I need to be able to read input data from an analog pin while simultaneously pulsing an led using an output pin. Below is the code i have so far. My goal is to have the arduino start recording (printing to serial) and initiate an LED flash sequence when i hit a button (in this case when i press the 's' key on my computer).  The problem is that when i start the sequence by hitting 's' it will print the analog input for the allotted time that i request and THEN trigger the LED flash. How do i make both things occur at the same time? Thanks in advance!

Code: [Select]

int LDR = 0;                               
int LDRValue = 0;

void setup()
  {
    Serial.begin(9600);            //start the serial monitor with 9600 buad
    pinMode(13, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(9, OUTPUT);   
  }

void loop()
  {

    if (Serial.available())
      {
       char inputValue = Serial.read();
       if(inputValue =='s')
               {  while (millis() < 5000)
                   { LDRValue = analogRead(LDR);         
                     Serial.println(LDRValue);
                   }
                 
                  delay (500);
                  digitalWrite(13, HIGH);
                  delay (1);
                  digitalWrite(13, LOW);
                  delay (200);
                  digitalWrite(13, HIGH);
                  delay (20);
                  digitalWrite(13, LOW);
               }
               else
               {
               }
      }
  }

Grumpy_Mike

Quote
I need to be able to read input data from an analog pin while simultaneously pulsing an led using an output pin.

No you don't you want to interleaved them.

Never use delay in this sort of code.
Do not compair the millis timer to an absoloute value always use the current time minus the start time.
Look at the blink without delay example.

khoadley85

Hello Gumpy_Mike,

       Thanks for the quick reply. Im not sure i understand you correctly. Why do i not want to interleave them? Its important for the analog read (data from a light sensor) to occur simultaneously with the LED pulse as this light pulse initiates the response i am looking for in the analog read data.

      Also, what is the blink without delay example and where can this be found? Thanks again for your help.

Grumpy_Mike

Missing punctuation read that as
No you don't,    you want to interleaved them.
You can do nothing similtainousyl  on a single core processor like all the arduinos.

Blink without delay is in the examples menu of the IDE.

khoadley85

Hello Grumpy_Mike,

I followed your advice and i incorporated the "blink without delay" code into my sketch and it seems to better approach what i am looking for. However, i still seem to get rather large jumps in the serial output associated with the led turning on or off. This occurs only for the few ms surrounding the led turning on or off. Unfortunately this is the most crucial moment for what i want to do so this is a large problem. I did not realize there was no way to run things simultaneously (new to arduino). Would it then make more sense to utilize two separate arduous (one for data acquisition and another for LED control)? And if so, what would be the best way to control two microcontrollers at once? Thanks again for your help. Learning new things every day!


Grumpy_Mike

Quote
Would it then make more sense to utilize two separate arduous

No.

Quote
i still seem to get rather large jumps in the serial output associated with the led turning on or off. This occurs only for the few ms surrounding the led turning on or off.

That is probably a hardware problem, most likely caused by bad layout. The current path for the LED, that is pin -> LED -> resistor -> ground looks like it is causing some sort of ground bounce. ( you have got a resistor in line haven't you if not that is what is causing the problem ) This ground bounce is when the current path for LED and the voltage you are trying to measure is the same piece of wire. The current through the wire causes a temporary voltage drop to occur and gets added to the voltage you are trying to measure.
What is the nature of this analogue input? Is it high impedance? Is it changing rapidly?

Quote
Learning new things every day!

Me too, and I have been at it over 40 years.  :)

khoadley85

Howdy,

      Thanks for all the help Grumpy_Mike. I was not aware of the ground bounce problem. However, i have now placed resistors on the led/driver setup but still no luck. The led is powered by a Buckpuck driver so the led is actually attached to the driver and then i have a transistor hooked up to the driver. The arduino pinOUT (#11) is connected to the transistor and this is how it controls the LED flash (the driver requires a 0-5v signal for ON/OFF). Even without the LED attached/ON, i still see the jump in serial reading corresponding to when the code would turn the LED on or off. The data i am collecting is light intensity and i am doing this currently with a simple CdS photoresistor (i plan on utilizing a far more sophisticated photodiode once i figure out what i am doing). What am i doing wrong??? Thanks again for the help.

Regards,

       Kenneth D. Hoadley

Grumpy_Mike

What pull up resistor are you using with the photo resistor, a schematic of your setup would help.

Go Up