Hello,
I'm terrible at this but learning by solving one problem at a time. What I am doing is sending voltage changes from an analog potentiometer on Uno #1 via Xbee to a receiving Uno #2 and lighting an LED via pin 3 using PWM. So far, I'm good.
I have now added a DAC (Sparkfun MCP4725) to send smooth DC voltages to a receiving unit for measurement. I can not understand how to translate the pot values from the sending Uno (mapped to 0-255 for PWM) that show up on the serial bus in Uno#2.
I have the DAC connected and tested with the example sine wave sketch and the LED on the DAC out responds correctly. I am using the Adafruit library for the DAC but ironically it works better with the Sparkfun DAC I am now using.
[The overall idea is to create a wireless expression pedal for a guitar processor. I want to use DC instead of changing to MIDI CC messages and sending those to the processor.]
Any guidance is appreciated to the fullest.
Chad
/* ~ Simple Arduino - xBee Receiver sketch ~
Read an PWM value from Arduino Transmitter to fade an LED
The receiving message starts with '<' and closes with '>' symbol.
Dev: Michalis Vasilakis // Date:2/3/2016 // Info: www.ardumotive.com // Licence: CC BY-NC-SA */
#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
//Constants
const int ledPin = 3; //Led to Arduino pin 3 (PWM)
//Variables
bool started= false;//True: Message is started
bool ended = false;//True: Message is finished
char incomingByte ; //Variable to store the incoming byte
char msg[3]; //Message - array from 0 to 2 (3 values - PWM - e.g. 240)
byte index; //Index of array
#define MCP4725_ADDR 0x60
#define DAC_RESOLUTION (12)
void setup() {
Wire.begin();
//Start the serial communication
Serial.begin(9600); //Baud rate must be the same as is on xBee module
pinMode(ledPin, OUTPUT);
dac.begin(0x60);
}
void loop() {
while (Serial.available()>0){
//Read the incoming byte
incomingByte = Serial.read();
//Start the message when the '<' symbol is received
if(incomingByte == '<')
{
started = true;
index = 0;
msg[index] = '\0'; // Throw away any incomplete packet
}
//End the message when the '>' symbol is received
else if(incomingByte == '>')
{
ended = true;
break; // Done reading - exit from while loop!
}
//Read the message!
else
{
if(index < 4) // Make sure there is room
{
msg[index] = incomingByte; // Add char to array
index++;
msg[index] = '\0'; // Add NULL to end
}
}
}
if(started && ended)
{
int value = atoi(msg);
analogWrite(ledPin, value);
Serial.write(value); //Only for debugging
index = 0;
msg[index] = '\0';
started = false;
ended = false;
dac.setVoltage(0, false);
}
}