Arduino Sketch MP_InteractiveControl
/*
MP_InteractiveControl
created 20 July 2012
by Martin Hebel, SelmaWare Solutions
The circuit:
* potentiometer connected to A0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital D9 to ground
* Pushbutton swithes on D2 and D3 going to ground.
Operates with MakerPlot Macro "MP_Interfacing.spm"
Physical Board:
* MakerPlot interface is configured.
* Potentiometer value is read, plotted and used to update MP LED bar and progress bar
* SW1 causes MakerPlot to take a snapshot via interface button
* Sw2 causes MakerPlot to autoscale to data
* Setpoint values read from MakerPlot slider
* Alarm via MakerPlot WAV file and LED lighting
* LED1 controlled via interface SW0
* Actual Value, setpoint, switch states, alarm state and LED state are plotted
MakerPlot Interface:
* Virtual SW0 on interface is read to control LED on board.
* VIrtual SW3 on interface is read to control whether alarm WAV is played
* Virtual LEDs light to show physical LED state, physical button/swith values, Alarm state
This example code is in the public domain.
*/
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int LEDpin = 9; // Analog output pin that the LED is attached to
const int SW1pin = 2; // Pushbutton switch, raise setpoint
const int SW2pin = 3; // Pushbutton switch, lower setpoint
int sensorValue = 0; // value read from the pot
int setPoint = 500; // Initial value of setPoint
int SW1state = 0; // Store state of SW1
int SW2state = 0; // Store state of SW2
int SW1LastState = 0; // Hold last state of switch
int SW2LastState = 0; // Hold last state of switch
int LEDstate = 0; // Store state of LED
int alarmSwState = 0; // Holds state of alarm
int alarmState = 0; // Holds current status if LED
int i = 0; // General storage for read
void setup() {
// configure hardware
pinMode(SW1pin, INPUT_PULLUP); // Enable pull-ups on switches
pinMode(SW2pin, INPUT_PULLUP);
pinMode(LEDpin, OUTPUT); // set LED to be an output pin
// initialize serial communications at 9600 bps:
Serial.begin(9600);
MP_config(); // Configure MakerPlot Interactive interface
}
void loop() {
measureData(); // measure values
MP_control_analog(); // Updates interface with analog values
MP_control_digital(); // Read physical switches and control MakerPlot interface
MP_read_sw0_control_LED(); // Read virtual sw0 and control LED
MP_read_sld_control_setpoint(); // Read virtual slider and control setpoint
MP_read_alarm(); // Determin alarm state, read virtual SW3 for alarm sound
plotData(); // plot values
// wait 100 milliseconds before the next loop
delay(100);
}
void measureData()
{
// read the analog in value
sensorValue = analogRead(analogInPin);
// read physical switch values - NOT values so 1 = pressed
SW1state = !digitalRead(SW1pin);
SW2state = !digitalRead(SW2pin);
}
void MP_control_analog()
{
Serial.print("!O ValBarLED ="); // update LED bar text box with value
Serial.println(sensorValue);
Serial.println("!O valBarLED.Run"); // Run code to the control to update LED bar based on max
}
void MP_control_digital()
{
Serial.print("!O LED0="); // Update virual LEDs with states
Serial.println(LEDstate);
Serial.print("!O LED1=");
Serial.println(SW1state);
Serial.print("!O LED2=");
Serial.println(SW2state);
if (SW1state == 1) // if SW1 pressed, operate the Snapshot button om interface
{ Serial.println("!O butSnap=1");
Serial.println("~PWAV stapler");
Serial.println("!O butSnap.Run");
Serial.println("!O butSnap=0");
}
if (SW2state == 1) // if SW2 pressed, autoscale plot and update button on interface
{ Serial.println("!O butAutoScale=1");
Serial.println("!ASCL View");
Serial.println("!O butAutoScale=0");
}
}
void MP_read_sw0_control_LED()
{ // Use interface virutal switch to control LED
flushBuffer();
Serial.println("!READ (sw0)"); // request sw0 control value from MakerPlot
LEDstate = Serial.parseInt(); // Accept values as interger (has about 1 second timeout if data not recieved)
digitalWrite(LEDpin,LEDstate); // control LED based on value read
}
void MP_read_sld_control_setpoint()
{ // Read interface slider for setpoint
flushBuffer();
Serial.println("!READ (slider)"); // request slider control values from MakerPlot
setPoint = Serial.parseInt(); // Accept values as interger (has about 1 second timeout)
Serial.print("!O Bar="); // update bar by slider with actual value
Serial.println(sensorValue);
}
void MP_read_alarm()
{ // sound alarm if setpoint exceeded and virtual switch on, and light LED
flushBuffer();
Serial.println("!READ (SW3)"); // request SW3 state for alarm sound decision
alarmSwState = Serial.parseInt(); // Accept values as interger (has about 1 second timeout)
if (sensorValue > setPoint) // Determine if above setpoint
{
if (alarmSwState == 0)
Serial.println("~PWAV dive"); // sound alarm noise if SW3 (silence) on interface was off
Serial.println("!O led3=1"); // turn on led0
alarmState = 1; // Store state - aalrming
}
else // if no alarm
{
Serial.println("!O led3=0"); // turn off led0
alarmState = 0; // Store state - not alarming
}
}
void plotData()
{
// print the analog values formatted for MakerPlot
Serial.print(sensorValue); // send 1st value
Serial.print(","); // send comma delimiter
Serial.println(setPoint); // send 2nd value with carriage return
// print the digital values formatted for MakerPlot
Serial.print("%"); // send binary indicator
Serial.print(SW1state); // send 1/0 for SW1
Serial.print(SW2state); // send 1/0 for SW2
Serial.print(LEDstate); // send 1/0 for LED
Serial.println(alarmState); // send 1/0 for alarming state with CR
}
void flushBuffer()
{
Serial.flush(); // flush serial buffer (may not always flush?)
while (Serial.available()) // manually empty as well, read as long as bytes in there
Serial.read();
}
void MP_config()
{ // conifgure MakerPlot
delay(2000); // Allow comms to stabilize
Serial.println("(CR)!SPAN 0,1000"); // Y-Axis scale
Serial.println("!YLBL Pot ADC"); // Y-Axis label
// clear constant drawings and place constant text on plot
Serial.println("!CLRC(CR)@TEXT 30A,105A,1.5A,(Blue),Interactive Control with Arduino!");
Serial.println("@TEXT 1A,D0,0.7A,(BLUE),Switch 1"); // Label digital traces
Serial.println("@TEXT 1A,D1,0.7A,(BLUE),Switch 2");
Serial.println("@TEXT 1A,D2,0.7A,(BLUE),LED State");
Serial.println("@TEXT 1A,D3,0.7A,(BLUE),Alarm State");
Serial.println("!O txtBar=Actual"); // Configure progress bar name
Serial.println("!O bar.max=1000"); // Set maximum of progress bar
Serial.println("!O txtSlider=SetPnt"); // conifgure slider name
Serial.println("!O Slider.max=1000"); // maximimum value of slider
Serial.print("!O Slider="); // Set slider to setpoint value
Serial.println(setPoint);
Serial.println("!O txtBarLED=Pot ADC"); // Name of LED bar value
Serial.println("!O txtBarMax=Pot MAX"); // Name of LED bar maximum
Serial.println("!O valBarMax=1023"); // Maximum value of LED bar
Serial.println("!O led*=0"); // set all controls starting with led to 0
Serial.println("!O sw*=0"); // set all controls starting with sw to 0
Serial.println("!O txtLED*= "); // Blank out LED names
Serial.println("!O txtSW*= "); // Blank out switch names
Serial.println("!O txtSW0=LED"); // Label SW0 for LED control
Serial.println("!O txtSW3=Silence"); // Label SW1 for alarm silence
Serial.println("!O txtLED0=LED"); // Label LED0 for LED state on board
Serial.println("!O txtLED1=Btn 1"); // Label LED 1 for button 1 on board
Serial.println("!O txtLED2=Btn 1"); // Label LED 2 for button 2 on board
Serial.println("!O txtLED3=Alarm"); // Label LED0 for Alarm
Serial.println("!RSET"); // reset plot
}
MP_InteractiveControl
created 20 July 2012
by Martin Hebel, SelmaWare Solutions
The circuit:
* potentiometer connected to A0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital D9 to ground
* Pushbutton swithes on D2 and D3 going to ground.
Operates with MakerPlot Macro "MP_Interfacing.spm"
Physical Board:
* MakerPlot interface is configured.
* Potentiometer value is read, plotted and used to update MP LED bar and progress bar
* SW1 causes MakerPlot to take a snapshot via interface button
* Sw2 causes MakerPlot to autoscale to data
* Setpoint values read from MakerPlot slider
* Alarm via MakerPlot WAV file and LED lighting
* LED1 controlled via interface SW0
* Actual Value, setpoint, switch states, alarm state and LED state are plotted
MakerPlot Interface:
* Virtual SW0 on interface is read to control LED on board.
* VIrtual SW3 on interface is read to control whether alarm WAV is played
* Virtual LEDs light to show physical LED state, physical button/swith values, Alarm state
This example code is in the public domain.
*/
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int LEDpin = 9; // Analog output pin that the LED is attached to
const int SW1pin = 2; // Pushbutton switch, raise setpoint
const int SW2pin = 3; // Pushbutton switch, lower setpoint
int sensorValue = 0; // value read from the pot
int setPoint = 500; // Initial value of setPoint
int SW1state = 0; // Store state of SW1
int SW2state = 0; // Store state of SW2
int SW1LastState = 0; // Hold last state of switch
int SW2LastState = 0; // Hold last state of switch
int LEDstate = 0; // Store state of LED
int alarmSwState = 0; // Holds state of alarm
int alarmState = 0; // Holds current status if LED
int i = 0; // General storage for read
void setup() {
// configure hardware
pinMode(SW1pin, INPUT_PULLUP); // Enable pull-ups on switches
pinMode(SW2pin, INPUT_PULLUP);
pinMode(LEDpin, OUTPUT); // set LED to be an output pin
// initialize serial communications at 9600 bps:
Serial.begin(9600);
MP_config(); // Configure MakerPlot Interactive interface
}
void loop() {
measureData(); // measure values
MP_control_analog(); // Updates interface with analog values
MP_control_digital(); // Read physical switches and control MakerPlot interface
MP_read_sw0_control_LED(); // Read virtual sw0 and control LED
MP_read_sld_control_setpoint(); // Read virtual slider and control setpoint
MP_read_alarm(); // Determin alarm state, read virtual SW3 for alarm sound
plotData(); // plot values
// wait 100 milliseconds before the next loop
delay(100);
}
void measureData()
{
// read the analog in value
sensorValue = analogRead(analogInPin);
// read physical switch values - NOT values so 1 = pressed
SW1state = !digitalRead(SW1pin);
SW2state = !digitalRead(SW2pin);
}
void MP_control_analog()
{
Serial.print("!O ValBarLED ="); // update LED bar text box with value
Serial.println(sensorValue);
Serial.println("!O valBarLED.Run"); // Run code to the control to update LED bar based on max
}
void MP_control_digital()
{
Serial.print("!O LED0="); // Update virual LEDs with states
Serial.println(LEDstate);
Serial.print("!O LED1=");
Serial.println(SW1state);
Serial.print("!O LED2=");
Serial.println(SW2state);
if (SW1state == 1) // if SW1 pressed, operate the Snapshot button om interface
{ Serial.println("!O butSnap=1");
Serial.println("~PWAV stapler");
Serial.println("!O butSnap.Run");
Serial.println("!O butSnap=0");
}
if (SW2state == 1) // if SW2 pressed, autoscale plot and update button on interface
{ Serial.println("!O butAutoScale=1");
Serial.println("!ASCL View");
Serial.println("!O butAutoScale=0");
}
}
void MP_read_sw0_control_LED()
{ // Use interface virutal switch to control LED
flushBuffer();
Serial.println("!READ (sw0)"); // request sw0 control value from MakerPlot
LEDstate = Serial.parseInt(); // Accept values as interger (has about 1 second timeout if data not recieved)
digitalWrite(LEDpin,LEDstate); // control LED based on value read
}
void MP_read_sld_control_setpoint()
{ // Read interface slider for setpoint
flushBuffer();
Serial.println("!READ (slider)"); // request slider control values from MakerPlot
setPoint = Serial.parseInt(); // Accept values as interger (has about 1 second timeout)
Serial.print("!O Bar="); // update bar by slider with actual value
Serial.println(sensorValue);
}
void MP_read_alarm()
{ // sound alarm if setpoint exceeded and virtual switch on, and light LED
flushBuffer();
Serial.println("!READ (SW3)"); // request SW3 state for alarm sound decision
alarmSwState = Serial.parseInt(); // Accept values as interger (has about 1 second timeout)
if (sensorValue > setPoint) // Determine if above setpoint
{
if (alarmSwState == 0)
Serial.println("~PWAV dive"); // sound alarm noise if SW3 (silence) on interface was off
Serial.println("!O led3=1"); // turn on led0
alarmState = 1; // Store state - aalrming
}
else // if no alarm
{
Serial.println("!O led3=0"); // turn off led0
alarmState = 0; // Store state - not alarming
}
}
void plotData()
{
// print the analog values formatted for MakerPlot
Serial.print(sensorValue); // send 1st value
Serial.print(","); // send comma delimiter
Serial.println(setPoint); // send 2nd value with carriage return
// print the digital values formatted for MakerPlot
Serial.print("%"); // send binary indicator
Serial.print(SW1state); // send 1/0 for SW1
Serial.print(SW2state); // send 1/0 for SW2
Serial.print(LEDstate); // send 1/0 for LED
Serial.println(alarmState); // send 1/0 for alarming state with CR
}
void flushBuffer()
{
Serial.flush(); // flush serial buffer (may not always flush?)
while (Serial.available()) // manually empty as well, read as long as bytes in there
Serial.read();
}
void MP_config()
{ // conifgure MakerPlot
delay(2000); // Allow comms to stabilize
Serial.println("(CR)!SPAN 0,1000"); // Y-Axis scale
Serial.println("!YLBL Pot ADC"); // Y-Axis label
// clear constant drawings and place constant text on plot
Serial.println("!CLRC(CR)@TEXT 30A,105A,1.5A,(Blue),Interactive Control with Arduino!");
Serial.println("@TEXT 1A,D0,0.7A,(BLUE),Switch 1"); // Label digital traces
Serial.println("@TEXT 1A,D1,0.7A,(BLUE),Switch 2");
Serial.println("@TEXT 1A,D2,0.7A,(BLUE),LED State");
Serial.println("@TEXT 1A,D3,0.7A,(BLUE),Alarm State");
Serial.println("!O txtBar=Actual"); // Configure progress bar name
Serial.println("!O bar.max=1000"); // Set maximum of progress bar
Serial.println("!O txtSlider=SetPnt"); // conifgure slider name
Serial.println("!O Slider.max=1000"); // maximimum value of slider
Serial.print("!O Slider="); // Set slider to setpoint value
Serial.println(setPoint);
Serial.println("!O txtBarLED=Pot ADC"); // Name of LED bar value
Serial.println("!O txtBarMax=Pot MAX"); // Name of LED bar maximum
Serial.println("!O valBarMax=1023"); // Maximum value of LED bar
Serial.println("!O led*=0"); // set all controls starting with led to 0
Serial.println("!O sw*=0"); // set all controls starting with sw to 0
Serial.println("!O txtLED*= "); // Blank out LED names
Serial.println("!O txtSW*= "); // Blank out switch names
Serial.println("!O txtSW0=LED"); // Label SW0 for LED control
Serial.println("!O txtSW3=Silence"); // Label SW1 for alarm silence
Serial.println("!O txtLED0=LED"); // Label LED0 for LED state on board
Serial.println("!O txtLED1=Btn 1"); // Label LED 1 for button 1 on board
Serial.println("!O txtLED2=Btn 1"); // Label LED 2 for button 2 on board
Serial.println("!O txtLED3=Alarm"); // Label LED0 for Alarm
Serial.println("!RSET"); // reset plot
}