int dataWord;
//DataBus Pin Declaration---I would like to get rid of this and only use PORTD/PORTB manipulation.
int DB0 = 2; //Digital Pin 2 on Sparkfun
int DB1 = 3;
int DB2 = 4;
int DB3 = 5;
int DB4 = 6;
int DB5 = 7;
int DB6 = 8;
int DB7 = 9; //Digital Pin 9 on Redboard
//Voltage Declaration
float voltage1;
float voltage2;
float voltage3;
//Power Declaration
float power1;
//RelayPin Digital Pin Declaration
int relayPin = 11;
const int timeDelay = 500;
//Write/Pulse Declaration
int WR = 13;
String state = "";
void setup()
{
Serial.begin(9600);
while(!Serial);
pinMode(DB0,OUTPUT); //set Pin 0 as an output
pinMode(DB1,OUTPUT); //set Pin 1 as an output
pinMode(DB2,OUTPUT); //set Pin 2 as an output
pinMode(DB3,OUTPUT); //set Pin 3 as an output
pinMode(DB4,OUTPUT); //set Pin 4 as an output
pinMode(DB5,OUTPUT); //set Pin 5 as an output
pinMode(DB6,OUTPUT); //set Pin 6 as an output
pinMode(DB7,OUTPUT); //set Pin 7 as an output
//Select Line A2 set to Ground due to lack of Pins.
pinMode(A0, INPUT); //Analog In Voltage
pinMode(A2, INPUT); //Analog In Voltage
pinMode(A4, INPUT); //Analog In Voltage
//Bits 0-7---
digitalWrite(DB0,HIGH); //These are the values I would like to change from the Serial Monitor
digitalWrite(DB1,LOW); //Right now I manually change these from Low to High
digitalWrite(DB2,HIGH);
digitalWrite(DB3,LOW);
digitalWrite(DB4,HIGH);
digitalWrite(DB5,HIGH);
digitalWrite(DB6,HIGH);
digitalWrite(DB7,HIGH);
//Write line setup
pinMode(WR, OUTPUT);
pinMode(WR, OUTPUT);
pinMode(WR, OUTPUT);
//Write line pulse
digitalWrite(WR, HIGH);
digitalWrite(WR, LOW);
digitalWrite(WR, HIGH);
//Setup for relay(relayPin) and the analog output(A5) of relay circuit
pinMode(relayPin, OUTPUT);
pinMode(A5, INPUT);
//Turns the system on to begin testing
digitalWrite(relayPin, HIGH); // turn the relay on
delay(timeDelay); // wait for one second
}
void portSet(byte x){
byte a = x;
byte y;
byte z;
// SET PORTD 2-7 AS OUTPUT
DDRD = DDRD | B11111100;
// SET PORTB 8-9 AS OUTPUT
DDRB = DDRB | B00000011;
x = ~x;
y = x & B11000000;
y = y >> 6;
PORTB = PORTB | y;
z = x << 2;
z = z & B11111100;
PORTD = PORTD | z;
}
void loop(){
//Begin serial comms
if(Serial.available() == 0){
state = Serial.readString();
//Loop to manually turn the circuit off
if (state == "p off") {
Serial.print("");
digitalWrite(relayPin, LOW);
Serial.println("SYSTEM IS OFF!");
}
//Loop to manually turn the circuit on
if (state == "p on") {
Serial.print("");
digitalWrite(relayPin, HIGH);
Serial.println("SYSTEM IS ON!");
}
//Loop to manually print voltage1
if (state == "voltage1") {
Serial.print("");
Serial.print("Voltage1 is: ");
Serial.print(voltage1,4);
Serial.println(" V");
}
//Loop to manually print power1
if (state == "power1"){
Serial.print("Power1 is: ");
Serial.print(power1,5);
Serial.println(" W");
delay(2000);
}
//Loop to manually print voltage2
if (state == "voltage2") {
Serial.print("");
Serial.print("Voltage2 is: ");
Serial.print(voltage2,4);
Serial.println(" V");
}
//Loop to manually print voltage3
if (state == "voltage3") {
Serial.print("");
Serial.print("Voltage3 is: ");
Serial.print(voltage3,4);
Serial.println(" V");
}
}
if((dataWord >= 0) && (dataWord <= 255)){
dataWord = Serial.parseInt();
Serial.println(dataWord, BIN);
}
//If/Print statement if voltage is out of lower bound
if(voltage1 < voltage2){
digitalWrite(relayPin, LOW);
Serial.println("Voltage is too low!");
}
//If/Print statement if voltage is out of higher bound
if(voltage1 > voltage3){
digitalWrite(relayPin, LOW);
Serial.println("Voltage is too high!");
}
//If/Print statement if voltage is in correct range
if ((voltage1 >= voltage2) && (voltage1 <= voltage3)){
digitalWrite(relayPin, HIGH);
}
}
This may be long but I wanted to be thorough but the overall idea of this project is that I have a Sparkfun Redboard v3, hooked up to an AD7228 DAC chip(I'll link the datasheet) and a transistor/relay circuit(I am attaching a schematic). Essentially, I am able to edit the 8 data bits from the DAC by setting them high or low to change the output voltage of the DAC. That output voltage is then read in by my Redboard through an Analog PIN, and if that voltage is within my voltage range that I set, my circuit will turn on and run continuously unless I set a voltage that is too high or too low. In that case, the circuit will print a warning and turn off. Currently, the circuit works in a manual fashion, I can go into my code in the Arduino IDE and manually change bits 0-7 from the DAC(2-9 on the Arduino as I saw that using PINS 0 & 1 is not advisable) to HIGH or LOW to get different voltage outputs. I would like to be able to set these values from the Serial Monitor so that I can set them all at the same time but I have not had much luck as I'm using both PORTD & PORTB.
In summary, I would like to be at the Serial Monitor, enter in a 8 bit word and it will set the values for my 8 PINS and adjust my output voltage on the DAC. In the screenshot of my schematic, please ignore the model # as Fritzing did not have the part I needed but the functionality is the same. I apologize if I didn't explain something well, I tried to stick close to the posting guidelines but thank you in advance for your help.