Right now im working on a project to create a BUS system.
I have two wires which work as both, energy and data transmission (the power/data line switches from +9V to -9V, where +9V is high and -9V is low).
I then have a circuit to power the arduino board and another to get the voltage to +5,2V (from +9V) or +0,2V (from -9V).
Now what i want to do is sending a bitcode to the arduino, lets say 8 bits: 10101100
This will represent in +9V -9V +9V -9V +9V +9V -9V -9V and then the line goes back to +9V constant.
The voltage on the line without a message send is constant +9V, so when i send something i want to "activate" the arduino to listen, like i send 4 bits (0000) and then want to monitor the incoming message to process it later.
I know i can read a analog pin with digitalRead() to get the result (high or low) and i know the timing how the pulses will come ( lets say 1Hz, but later i want to go up to 50kHz).
This means i should be able to use digitalRead(PIN), wait the frequence time, digitalRead(PIN) again, wait again and so on.
I have something in mind like:
read Pin;
write binary to variable X;
(variable X shall have 4 "digits", so when a new read comes the value before should go to the next place
X at start: 1111, now the first low comes in, so X should be 1110, then 1100, 1000, 0000)
wait frequence time;
if X == 0000;
loop until counter reaches 8;
read Pin;
counter +1;
write binary to variable Y;
wait X time;
Now the variable Y shall be monitored the same, except i know its 8 bits long, so i can use a counter. After that the arduino goes back to its waiting mode, +9V on the line, until the next 0000 is monitored.
My most urgent problem atm: how can i write those digitalReads to a variable in bit format?
Im new with the arduino so im asking for the syntax i could use to achieve this. Can i use bitWrite() for this? For the second part with the 8 bits i assume i can, but i know the message lengh. How can i monitor the line and watch if four times 0 is send?
I dont need or want a full code or something like that, i just want a tap in the right direction.
EDIT: Corrected some errors
EDIT2: The "code" i gave as example is only to show how it should work, i know that its completly missing any functionable syntax. Its only as example.