I am not multiplexing a serial signal.
I used serial only to see the output of the multiplexer (to see if it was working) but i will analyse the signal with arduino analogRead.
it is this my proget.
I have three analog signal 2.7V offset of 1.3 so it is positive and smaller then 5. this signal can have a variable frequency from 0 to 50Hz.
What I am doing is I put this three signal in the mux,
I send them in arduino only if the push button is HIGH (I put the push button in analog input and I used them as digital because using the mux require a lot of digital pin).
Then from arduino I send this signal in a PWM form out from digital pin 3 and I let them pass in a optic fiber, after I received them in another arduino where after going to the DAC will be separete by a DEMUX. this is my idea of my project.
I didnt use serial because I don't know the time and I didn't really understand how I can decode them. I don't know for example if I use Manchester to send by serial then I don't know how I can get the original signal.
my code for now is this I am still working in the receiver part. and both arduino have to be transmitter and receiver.
They need to be receiver if the button is high and receiver if the button is down. but I hqve three receiver and three transmitter so they hqve to work independently.
what do I mean is if only button a is high it is arduino 1 have one transmitter and two receiver and arduino 2 have two transmitter and one receiver.
#include <SPI.h> //
#include <DAC_MCP49x1.h>
#define SS 10
// Set up the DAC.
// First argument: model (MCP4901, MCP4911, MCP4921)
// Second argument: SS photodiodo (10 is preferred)
// (The third argument, the LDAC photodiodo, can be left out if not used)
DAC_MCP49x1 dac(DAC_MCP49x1::MCP4901, SS);
int incomingSignal;//storage for A0 data
const int T = 128; //period in microsecond
int voltage;
long lasttime;;
long debounceDelay = 0.0005;
byte buttonPin; // the number of the pushbutton pin
byte buttonPin1;
int r0 = 0;
int r1 = 0; // the three digital pins for the bits
int r2 = 0;
void transmittedSignal()
{
digitalWrite(4, LOW); // I am using device CD4051B as mux
digitalWrite(5, r0); // send the bits to the digital pins
digitalWrite(6, r1);
digitalWrite(7, r2);
incomingSignal = analogRead(A0) >> 2;//get new value from A0
analogWrite(3, incomingSignal); //assigned value to the LEDs
digitalWrite(4, HIGH); //I disable the device CD405 //I am not sure if it is correct or it is necessary pag 3 data sheet CD4051B
Serial.println(incomingSignal);
}
int receivedSignal()
{
digitalWrite(8,LOW); // I am using device CD4051B as demux
voltage = digitalRead(9); //for now I am sending from the other arduino only a digital signal 1 0 to see if I am receivng something, after it will be a PWM
//voltage = ((pulseIn(9, LOW, 1280) * 100) / T) >> 1; //here is how i can get after the dac the original signal
//voltage = map(constrain(voltage, 0, 100), 0, 100, 0, 255);
//dac.output(voltage);
digitalWrite(8, HIGH); //I disable the device CD405 //I am not sure if it is correct or it is necessary pag 3 data sheet CD4051B
}
void setup() {
ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); //prescaler 128 STO FACENDO UNA CONVERSIONE OGNI 0.104MS
TCCR2A |= (1 << WGM20);
bitClear(TCCR2B, CS20); //set prescaler to 8
bitSet(TCCR2B, CS21);
bitClear(TCCR2B, CS22);
dac.setSPIDivider(SPI_CLOCK_DIV128);
dac.setPortWrite(false);
pinMode(3, OUTPUT); // tx
pinMode(4, OUTPUT); // mux enable
pinMode(5, OUTPUT); // r0
pinMode(6, OUTPUT); // r1
pinMode(7, OUTPUT); // r2
pinMode(8, OUTPUT); // demux enable
pinMode(9, INPUT); //rx
pinMode(A1, INPUT); // button
pinMode(A2, INPUT); // button
pinMode(A3, INPUT); // button
pinMode(A0, INPUT); // input mult
digitalWrite(4,HIGH); //inizialization for the mux
digitalWrite(8,HIGH);//inizialization for the demux
Serial.begin(9600); // fire up the serial
while (!Serial);
}
void loop () {
buttonPin = digitalRead(A1);
if ( buttonPin == HIGH )
{
r0 = 0; //bitRead() -> parameter 1 = binary sequence, parameter 2 = which bit to read, starting from the right most bit
r1 = 0; //channel 7 = 111, 1 = 2nd bit
r2 = 0; // third bit
transmittedSignal();
delay (1000); // time to read the values
}
else
{
receivedSignal();
Serial.println(voltage);
}
buttonPin1 = digitalRead(A2);
if (buttonPin1 == HIGH)
{
r0 = 0; //bitRead() -> parameter 1 = binary sequence, parameter 2 = which bit to read, starting from the right most bit
r1 = 1; //channel 7 = 111, 1 = 2nd bit
r2 = 0; // third bit
transmittedSignal();
delay (1000); // time to read the values
}
else
{
receivedSignal();
Serial.println(voltage);
}
byte buttonPin2 = digitalRead(A3);
if ( buttonPin2 == HIGH )
{
r0 = 1; //bitRead() -> parameter 1 = binary sequence, parameter 2 = which bit to read, starting from the right most bit
r1 = 0; //channel 7 = 111, 1 = 2nd bit
r2 = 0; // third bit
transmittedSignal();
delay (1000); // time to read the values
}
else
{
receivedSignal();
Serial.println(voltage);
}
}