I want to convert sensor analog output voltage in 8 bit digital format and display the value and transmit the value serially.what should be actual code for this as i am new in arduino. kindly help me.
Really basic code:
byte analogValue;
void setup{
Serial.begin(9600); // start serial port
}
void loop(){
//read an analog pin
analogValue = analogRead(A0)>>2; // 10 bit value converted to 8 bit
Serial.println (analogValue); // send 8 bit value in decimal format (0 to 255) to serial monitor
delay (500); // crudely delay 1/2 second so don't flood PC serial port
}
I want the DIGITAL OUTPUT in zero(0) or one(1) format for equivalent analog vaule. if sensor analog value is 5v then 8 bit digital output will be 11111111 and sensor analog value is 0v then 8 bit digital output will be 00000000. in this way i have to transmit these digital output of equivalent analog signal should be sent through serially.What should be the code? kindly help me.
Ok, so change this line
Serial.println (analogValue); // send 8 bit value in decimal format (0 to 255) to serial monitor
to
Serial.println (analogValue, BIN); // send 8 bit value in binary format (00000000 to 11111111) to serial monitor
Thank you.
Now i want to encode the each serial digital value. my encoded program i have written i.e.
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(13, OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// send an intro:
Serial.println("ENCODED VALUE");
Serial.println();
}
void loop() {
// get any incoming bytes:
if (Serial.available() > 0) {
int thisChar = Serial.read();
if (thisChar == '0')
{
digitalWrite(13,HIGH);
Serial.print("1");
delay(500);
digitalWrite(13,LOW);
Serial.print("0");
delay(500);
}
else {
digitalWrite(13,HIGH);
Serial.print("1");
delay(1000);
}
}
MY PROBLEM IS THAT WHAT COMMAND SHOULD I USE TO ENCODE EACH BINARY VALUE?