# analog to digital

I got an analog input that's going to see 0-5v, depending on the input I need to have a digital pin send an output like this

1v = high 12ms low 24ms high 6ms low 65ms and then go back to reading analog input and if it's at 1v do the same string of highs and lows.

Can I send the data through a digital pin or can I possibly send the data over tx pin?

Use a digital pin, the tx pin is otherwise occupied.

Can I use delay to keep the digital pin high and low? Will analog pin be able to read at the same time? I didn't think it could.

No, check out the Blink without Delay example. Use the millis() timer.

Use millis() for timing rather than delay() and you can read the analogue pin during the timing periods.

How would I go about doing that?

You need to look into using millis() for timing as in the BlinkWithoutDelay example and Several things at the same time

Save the time an event happens then each time through loop() check whether the required period has elapsed since the event. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action.

electronicM:
How would I go about doing that?

If I may quote Gandalf: "Google is mighty my friend."

But why the specific pulses? A good example of the XY-problem. So what do you really want to do?

septillion:
If I may quote Gandalf: "Google is mighty my friend."

But why the specific pulses? A good example of the XY-problem. So what do you really want to do?

I already did, time to get to work.

couple of intervals are in microseconds, is there a previousmicros?

septillion:
So what do you really want to do?

septillion:

I need to send different high/low signals depending on the analog input.

electronicM:
couple of intervals are in microseconds, is there a previousmicros?

previousMillis is just a stupid name. Make up any name you like to hold the last time an event of interest occurred.

That's not what you really want to do

XY-problem

previousMillis is just a name. I prefer “startTime”

Now we find out that you need microsecond intervals. It would be nice if you would tell us things like that:

1v = high 12ms low 24ms high 6ms low 65ms

but there is an equivalent micros() command.

sorry, I was just making an example.

Ok I am back, had no luck with delays and couldn’t implement blink without delay.

``````const int analogPin = A0;

void setup() {
pinMode(12, OUTPUT);
Serial.begin(9600);
}

void loop() {

if (analogValue > 900 && analogValue < 1000) {

digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)  case1
delayMicroseconds (20);
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delayMicroseconds (20);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
delayMicroseconds (100);
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay (100);

}
if (analogValue > 500 && analogValue < 900) {

digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)  case2
delayMicroseconds (40);
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delayMicroseconds (40);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
delayMicroseconds (60);
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay (100);
}
else {
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)    case3
delayMicroseconds (60);
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delayMicroseconds (60);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
delayMicroseconds (20);
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay (100);
}

}
``````

It works if I just use case1 and else, but one I add more than one case, it stops working and just outputs what’s in the else brackets.

Wow, how did you manage to mangle the layout/indentation that much! The IDE really tries to help so it really takes effort to do this. Hit Ctrl + t for fun.

And still, where do you need those pulses for!? They are precise it almost sounds like you try to do something weird like generation a protocol or something. Maybe the Arduino has already a hardware solution for it but you don't really tell what you want to do.

And ever heard of variables? 12 is as clear as 42 (Answer to the Ultimate Question of Life, the Universe, and Everything). And now we're there, heard of else if() ?