I'm new working with arduino, only know the basics
In my project I receive values in Analog inputs that i should convert them in bits
For example if a receive a signal thats higher than 3.3V it should return a bit = 1 and if lower than 1.5V should return a bit = 0.
And after receiving 8bits I want to convert that in one byte
for example I receive in analogRead 8 values that after converting them in 1's and 0's i want to build a byte
let's say that a received 1101100 wich should correspond to an "l"
I already did a lot of research and i jus can't find anything to work with...
Consider using digitalRead() instead. With a 5 volt Arduino, any input of 3 or more volts will return a 1 while anything under 3 volts will give you a zero. Then you can multiply and add the results:
I consider your thoughts and i came up with something near to what I want
For know i'm reading values from Serial monitor because my circuit isn't ready for use but i'm getting my expected values.
Sorry for the comments, they are in portuguese. but i can put them in english if anyone needs it
my code until now is like this:
(the important part is basicly the function rcv_bin(). Function save_by() commented for know. still working on it)
const byte numChars = 32;
byte inf_save[numChars];
char receivedChars[numChars]; // an array to store the received data
int rcv_int;
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false)
{
rc = Serial.read();
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData()
{
if (newData == true)
{
Serial.print("This just in ... ");
Serial.println(receivedChars);
rcv_int = atoi(receivedChars);
Serial.print("int value of the data received: ");
Serial.println(rcv_int);
int b_save=0;
rcv_bin(rcv_int);
newData = false;
}
}
/*
void save_by()
{
Serial.println("funçao save_by começa aqui ");
if(int i=0 < 32)
{
int dt_rcv[numChars];
char ch_rcv;
//char ch_rcv_b;
//ch_rcv_b=char(bi);
//posiçao i do vetor de inteiros /*bytes guarda o valor do byte recebido byt
dt_rcv[i]=byt;
//objetivo é ler valor recebido em forma de caracter
//dt_rcv = inf_save[i];
//int aux = dt_rcv;
ch_rcv = char(dt_rcv[i]);
Serial.print("data(char) received: ");
Serial.println(char(dt_rcv));
//imprimi o valor ascii do caracter recebido. Como passar esse valor para um caracter alfanumerico?
Serial.print("data received(ascii value): ");
Serial.println(dt_rcv[i]);
Serial.print("data received(char): ");
Serial.println(ch_rcv);
i++;
}
else
{
Serial.println("Buffer full, byte cleared. new transmition comming. Please re-enter info");
i=0;
}
}
*/
void rcv_bin(int rcv_num) //recebe 1's e 0's e transforma-os num byte //void rcv_bin(int rcv_num, int byt)
{
//ARRANJAR MANEIRA DE GRAVAR O BYT COMO DEVE SER. NESTE MOMENTO APENAS GRAVA UMA POSIÇAO
Serial.print("ja dentro da funçao rcv_bin. o valor de rcv_int: ");
Serial.println(rcv_num);
/*
* rcv_int é o valor que vai estar na entrada
* que vai variar entre 0 e 1024 onde a partir
* de 500 vai corresponder
* a um bit de valor 1 e 0 caso contrario
*/
static int count;
static int bit_aux[8];
static int byt;
static char byt_c;
// transforma a entrada em 1's ou 0's
if(count < 8) //se count < 8 entao bit ainda nao foi todo recebido entao continua a escrever em byt
{
if(rcv_int>500)
{
Serial.println("Bit recebido = 1");
bit_aux[count] = 1;
byt=bitWrite(byt, count, 1);
}
else
{
Serial.println("Bit recebido = 0");
bit_aux[count] = 0;
byt=bitWrite(byt, count, 0);
}
Serial.print("valor da variavel auxiliar count: ");
Serial.println(count);
count++;
}
else //se for byte ja foi todo recebido e entao count vai a zero para voltar a receber novo bit
{
Serial.println("byte complatamente recebido");
byt=0;
count=0;
}
/*
Serial.println("byte recebido em binario: ");
Serial.print(bit_aux[0]);
Serial.print(bit_aux[1]);
Serial.print(bit_aux[2]);
Serial.print(bit_aux[3]);
Serial.print(bit_aux[4]);
Serial.print(bit_aux[5]);
Serial.print(bit_aux[6]);
Serial.print(bit_aux[7]);
Serial.println();
Serial.println();
Serial.print("byte recebido em binario: ");
Serial.println(byt, BIN);
Serial.print("byte recebido em inteiro: ");
Serial.println(byt);
Serial.print("byte recebido em caracter: ");
Serial.println(char(byt));
Serial.println();
Serial.println();
*/
Serial.print("caracter recebido: ");
Serial.println(char(byt));
byt_c = byt;
Serial.println(byt_c);
}
After this my goal is to print the caracters received all together like it was a sentence. don't know if I'm going to save the chars received in an array of chars or just print them in a line