could anyone verifies this sketch

Hi all,

I've been trying to get a binary representation of an analog signal and then connect it to LED. i am not sure if the sketch is correct but when i connect 3vP.p in pin A1 and oscilloscope in pin 28 , i can see pules. if the steps i am taking are wrong could someone guide me what is the appropriate way to do it.

==============================================================================
const int AnalogInputPin= A1;
const int DigitalOutputPin= 28;

void setup() {

pinMode(AnalogInputPin, INPUT);
pinMode(DigitalOutputPin,OUTPUT);
Serial. begin(9600);

}

void loop() {
byte a;
//analogRead(AnalogInputPin);
a=analogRead(AnalogInputPin);
digitalWrite(DigitalOutputPin,a);
Serial.println(a);

while(0){
}

}

i think it is worth knowing that i am knew for C language and Embedded circuit.

regards,

M,

byte a;
a=analogRead(AnalogInputPin);

The analogRead function returns a value in the range 0 to 1023. A large part of that range will not fit in a byte.

while(0){
}

WTF?

What problem are you having? Where are you expecting a "binary representation of an analog signal"?

[What problem are you having?]

the problem that i am not sure if this is a correct way to write a code to output digital representation of analog signal.

[Where are you expecting a "binary representation of an analog signal"?]

i am using Arduino Due board. i assigned pin A1 as analog input and pin 28 as a digital output.
after i loaded the sketch in the board, i connected the pin 28(DigitalOutput) to a oscilloscope. the output i get is sequence of pulses.

That is pretty much all you get from a digital output, what did you expect?

KeithRB:
That is pretty much all you get from a digital output, what did you expect?

yeah but is my sketch correct for converting input signal to digital then output it?, why im asking this question is because there is precise function which does the converting directly analogReadResolution() but i don’t know how to use it in my project.

M

is my sketch correct for converting input signal to digital then output it?,

I doubt it, but you'll need to be more clear about what that means. Leaving aside the issue with byte that PaulS mentioned, all your code does is set your output pin high if your analogRead returns a non-zero value.

I assume you want to send some kind of representation of the analog value read out on a digital line, but without more detail, it's hard to help.

If you want a "binary representation of the analog signal" you're going to need more than 1 digital output pins; The ADC is 10-bits, so you would need up to 10 pins to represent it.