Arduino Sketch MP_BasicInteractiveControl
/*
MP_BasicInteractiveControl
Performs basic interaction with MakerPlot Interactive Interface to
read the slider for the setPoint and control virual LED0 if alarming.
Plots analog values of input and setpoint and
digital values of switches and LED state.
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.
created July 2012
by Martin Hebel, SelmaWare Solutions
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 LEDstate = 0; // Store state of LED
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);
config_MP(); // function to configure MakerPlot
}
void loop() {
measureData(); // measure values
read_MPSlider(); // Read slider value from MakerPlot
buttonControl(); // Use buttons to also adjust setpoint
control(); // control based on settings
plotData(); // diplay/plot values
// wait 100 milliseconds before the next loop
delay(100);
}
void measureData()
{
// read the analog in value
sensorValue = analogRead(analogInPin);
}
void read_MPSlider()
{
flushBuffer(); // Ensure buffer empty
Serial.println("!READ (Slider)"); // Request the slider's value
setPoint = Serial.parseInt(); // Accept returned data
}
void buttonControl()
{ // Check states of pushbuttons, it pressed change setpoint up or down
SW1state = digitalRead(SW1pin);
if (SW1state == 0)
{
setPoint+=10; // add 10 to setPoint
Serial.print("!O Slider= "); // Update slider with new setpoint
Serial.println(setPoint);
}
SW2state = digitalRead(SW2pin);
if (SW2state == 0)
{
setPoint-=10; // subtract 10 from setPoint
Serial.print("!O Slider= "); // Update slider with new setpoint
Serial.println(setPoint);
}
}
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.println(LEDstate); // send 1/0 for LED with carriage return
}
void control()
{
// Check pot value against setpoint, if above light LED, take snapshot
if (sensorValue > setPoint)
LEDstate = 1;
else
LEDstate = 0;
digitalWrite(LEDpin,LEDstate);
Serial.print("!O LED0="); // set up to control virtual LED0
Serial.println(LEDstate); // send value with CR
}
void config_MP()
{
delay(2000); // allow connection to stabilize
Serial.println(); // send in case garbage in queue
Serial.println("!RSET"); // Reset the plot
Serial.println("!O txtSW*= "); // Clear all switch text boxes
Serial.println("!O txtLED*= "); // Clear all LED text boxes
Serial.println("!O txtSlider=SetPnt"); // Label slider
Serial.println("!O Slider.Max=1000"); // Set slider max to 1000
Serial.println("!O txtLED0=Alarm"); // Label LED0 for alarm
Serial.println("!O LED*=0"); // Turn all LEDs off
// clear constant drawings and place constant text on plot
Serial.println("!CLRC(CR)@TEXT 30A,105A,1.5A,(Blue),Basic Interaction with Arduino!");
}
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();
}
MP_BasicInteractiveControl
Performs basic interaction with MakerPlot Interactive Interface to
read the slider for the setPoint and control virual LED0 if alarming.
Plots analog values of input and setpoint and
digital values of switches and LED state.
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.
created July 2012
by Martin Hebel, SelmaWare Solutions
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 LEDstate = 0; // Store state of LED
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);
config_MP(); // function to configure MakerPlot
}
void loop() {
measureData(); // measure values
read_MPSlider(); // Read slider value from MakerPlot
buttonControl(); // Use buttons to also adjust setpoint
control(); // control based on settings
plotData(); // diplay/plot values
// wait 100 milliseconds before the next loop
delay(100);
}
void measureData()
{
// read the analog in value
sensorValue = analogRead(analogInPin);
}
void read_MPSlider()
{
flushBuffer(); // Ensure buffer empty
Serial.println("!READ (Slider)"); // Request the slider's value
setPoint = Serial.parseInt(); // Accept returned data
}
void buttonControl()
{ // Check states of pushbuttons, it pressed change setpoint up or down
SW1state = digitalRead(SW1pin);
if (SW1state == 0)
{
setPoint+=10; // add 10 to setPoint
Serial.print("!O Slider= "); // Update slider with new setpoint
Serial.println(setPoint);
}
SW2state = digitalRead(SW2pin);
if (SW2state == 0)
{
setPoint-=10; // subtract 10 from setPoint
Serial.print("!O Slider= "); // Update slider with new setpoint
Serial.println(setPoint);
}
}
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.println(LEDstate); // send 1/0 for LED with carriage return
}
void control()
{
// Check pot value against setpoint, if above light LED, take snapshot
if (sensorValue > setPoint)
LEDstate = 1;
else
LEDstate = 0;
digitalWrite(LEDpin,LEDstate);
Serial.print("!O LED0="); // set up to control virtual LED0
Serial.println(LEDstate); // send value with CR
}
void config_MP()
{
delay(2000); // allow connection to stabilize
Serial.println(); // send in case garbage in queue
Serial.println("!RSET"); // Reset the plot
Serial.println("!O txtSW*= "); // Clear all switch text boxes
Serial.println("!O txtLED*= "); // Clear all LED text boxes
Serial.println("!O txtSlider=SetPnt"); // Label slider
Serial.println("!O Slider.Max=1000"); // Set slider max to 1000
Serial.println("!O txtLED0=Alarm"); // Label LED0 for alarm
Serial.println("!O LED*=0"); // Turn all LEDs off
// clear constant drawings and place constant text on plot
Serial.println("!CLRC(CR)@TEXT 30A,105A,1.5A,(Blue),Basic Interaction with Arduino!");
}
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();
}