Skip to content

Commit

Permalink
code cleanups, switch LEDs to black when no serial connection
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoostenveld committed Jan 19, 2020
1 parent 9a0d956 commit 6d62607
Showing 1 changed file with 54 additions and 32 deletions.
86 changes: 54 additions & 32 deletions teensy_cvgate_mcp4725_neopixel/teensy_cvgate_mcp4725_neopixel.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,39 @@

#include "colormap.h"

#define enable1 true
#define enable2 true
#define NEOPIXEL_PIN 14
#define GATE1_PIN 15
#define GATE2_PIN 16
#define WIRE_SDAPIN 18
#define WIRE_SCLPIN 19

// 0x60 is the I2C Address of the MCP4725, by default (A0 pulled to GND).
// Please note that this breakout is for the MCP4725A0.
// For devices with A0 pulled HIGH, use 0x61
#define address1 0x60
#define address2 0x61

#define gate1pin A0 // the pin controlling the digital gate
#define gate2pin A1 // the pin controlling the digital gate

#define MCP4726_CMD_WRITEDAC (0x40) // Writes data to the DAC
#define MCP4726_CMD_WRITEDACEEPROM (0x60) // Writes data to the DAC and the EEPROM (persisting the assigned value after reset)

// the values of the DAC range from 0 to 4095 (12 bits)
#define MAXVALUE 4095.

// these are the commands over the serial interface
// these are used to interpret the commands over the serial interface
#define NONE 0
#define VOLTAGE 1
#define GATE 2

// status after parsing the commands
#define OK 0
#define ERROR -1
// this is the status after parsing the commands over the serial interface
#define OK 0
#define ERROR -1

#define PIXELPIN 20
#define NUMPIXELS 4
#define BRIGHTNESS 0.3

Adafruit_NeoPixel pixels(NUMPIXELS, PIXELPIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels(NUMPIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);

// the initial values of the output control voltages and gates
// tghese are the initial values of the output control voltages and gates
int voltage1 = 0, voltage2 = 0;
int gate1 = 0, gate2 = 0;

Expand Down Expand Up @@ -92,17 +91,35 @@ void setup() {
Serial.setTimeout(1000);

Wire.begin();
Wire.setSDA(WIRE_SDAPIN);
Wire.setSCL(WIRE_SCLPIN);

// initialize the gate pins as output:
pinMode(gate1pin, OUTPUT);
pinMode(gate2pin, OUTPUT);
pinMode(GATE1_PIN, OUTPUT);
pinMode(GATE2_PIN, OUTPUT);

// INITIALIZE NeoPixel strip object
pixels.begin();
delay(1000);

// Set all pixel colors to 'off'
pixels.clear();


// Set the pixels to RGBW to indicate that setup is done
pixels.setPixelColor(0, pixels.Color(128, 0, 0));
pixels.show();
delay(250);
pixels.setPixelColor(1, pixels.Color(0, 128, 0));
pixels.show();
delay(250);
pixels.setPixelColor(2, pixels.Color(0, 0, 128));
pixels.show();
delay(250);
pixels.setPixelColor(3, pixels.Color(128, 128, 128));
pixels.show();
delay(250);

Serial.println("Setup done.");
return;
}

Expand Down Expand Up @@ -147,11 +164,11 @@ void loop() {
switch (channel) {
case 1:
voltage1 = (value > MAXVALUE ? MAXVALUE : value);
status = (enable1 ? OK : ERROR);
status = OK;
break;
case 2:
voltage2 = (value > MAXVALUE ? MAXVALUE : value);
status = (enable2 ? OK : ERROR);
status = OK;
break;
default:
status = ERROR;
Expand All @@ -176,27 +193,32 @@ void loop() {
status = ERROR;
}
if (status == OK)
Serial.println("ok");
Serial.println("OK");
else if (status == ERROR)
Serial.println("error");
}
else {
// refresh the output voltages and gates
if (enable1) {
// update the output cointrol voltages and gates
setValue(address1, voltage1);
digitalWrite(gate1pin, gate1);
}
if (enable2) {
setValue(address2, voltage2);
digitalWrite(gate2pin, gate2);
}
digitalWrite(GATE1_PIN, gate1);
digitalWrite(GATE2_PIN, gate2);

// update the Neopixels by mapping a value between 0 and 1 onto the colormap
// note that they are mounted in the opposite order than the 3.5 mm jacks

// update the color of the Neopixels
// the integer representation of the control voltage is represented between 0 and 4095
setColor(0, voltage1/MAXVALUE);
setColor(1, voltage2/MAXVALUE);
// the integer representation of the gate voltage is either 0 or 1
setColor(2, gate1);
setColor(3, gate2);
if (!Serial) {
// switch all pixels off when there is no serial connection
pixels.clear();
pixels.show();
}
else {
// the integer representation of the control voltage is represented between 0 and 4095
setColor(3, voltage1/MAXVALUE);
setColor(2, voltage2/MAXVALUE);
// the integer representation of the gate voltage is either 0 or 1
setColor(1, gate1);
setColor(0, gate2);
}
}
} //main

0 comments on commit 6d62607

Please sign in to comment.