Arduino Sketch MakerPlot Arduino Bi-Directional Control - Part 2
// MakerPlot Arduino Bi-Directional Control - Part 2
// This sketch 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 = 100; // 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);
delay(2000); // allow connection to stabilize
Serial.println(); // send in case garbage in queue
Serial.println("!RSET"); // Reset the plot
Serial.println("!O butMin=1"); // Set butMin object to 1, ON
Serial.println("!O butMin.Run"); // Run event code button to configure minutes, turn other 2 off
Serial.println("!O txtXMax=2"); // Set text box for maximum time to 2 minutes
Serial.println("!O txtXMax.Run"); // Run text box event code to update time
// clear constant drawings and place constant text on plot
Serial.println("!CLRC(CR)@TEXT 30A,105A,1.5A,(Blue),Controlled from Arduino!");
delay(3000); // delay 3 seconds
Serial.println("!O LED0=1"); // turn ON LED0 on the interface
Serial.println("!O SW0=1"); // turn ON SW0 on the interface
Serial.println("!BELL"); // sound PC Bell
delay(1000); // delay 1 second
Serial.println("!O LED1=1"); // turn ON LED1 on the interface
Serial.println("!O SW1=1"); // turn ON SW1 on the interface
Serial.println("!BELL"); // sound PC Bell
delay(1000); // delay 1 second
Serial.println("!O LED2=1"); // turn ON LED2 on the interface
Serial.println("!O SW2=1"); // turn ON SW2 on the interface
Serial.println("!BELL"); // sound PC Bell
delay(1000); // delay 1 second
Serial.println("!O LED3=1"); // turn ON LED3 on the interface
Serial.println("!O SW3=1"); // turn ON SW3 on the interface
Serial.println("!BELL"); // sound PC Bell
delay(1000); // delay 1 second
Serial.println("!O LED*=0"); // turn OFF all LEDs
Serial.println("!O SW*=0"); // turn OFF all Switches
Serial.println("!BELL"); // sound PC Bell
delay(1000); // delay 1 second
}
void loop() {
Serial.println("!READ(Slider)"); // read the slider
setPoint = Serial.parseInt(); // change the setPoint to reflect it
sensorValue = analogRead(analogInPin); // get pot voltage into sensorValue
// Check pot value against setpoint, if above light LED
// And take snapshot is sensorValue ? setPoint
if (sensorValue > setPoint)
{
if (LEDstate == 0) // take snapshot if above setpoint,
// just once each crossing (if LEDstate was off)
{
Serial.println("!O butSnap=1"); // Turn on Snapshot button
Serial.println("!O butSnap.Run"); // Run snapshot event code
Serial.println("!O butSnap=0"); // Turn off snapshot button
}
LEDstate = 1;
}
else
LEDstate = 0;
digitalWrite(LEDpin,LEDstate);
// Check states of pushbuttons, if pressed change setpoint up or down
SW1state = digitalRead(SW1pin);
if (SW1state == 0)
{
setPoint++; // increment setPoint by 1
Serial.print ("!O Slider ="); // send to Makerplot to adjust slider
Serial.println(setPoint); // based on new setPoint
}
SW2state = digitalRead(SW2pin);
if (SW2state == 0)
{
setPoint--; // decrenemt setPoint by 1
Serial.print ("!O Slider ="); // send to Makerplot to adjust slider
Serial.println(setPoint); // based on new setPoint
}
// 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
// wait 100 milliseconds before the next loop
delay(100);
}
// This sketch 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 = 100; // 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);
delay(2000); // allow connection to stabilize
Serial.println(); // send in case garbage in queue
Serial.println("!RSET"); // Reset the plot
Serial.println("!O butMin=1"); // Set butMin object to 1, ON
Serial.println("!O butMin.Run"); // Run event code button to configure minutes, turn other 2 off
Serial.println("!O txtXMax=2"); // Set text box for maximum time to 2 minutes
Serial.println("!O txtXMax.Run"); // Run text box event code to update time
// clear constant drawings and place constant text on plot
Serial.println("!CLRC(CR)@TEXT 30A,105A,1.5A,(Blue),Controlled from Arduino!");
delay(3000); // delay 3 seconds
Serial.println("!O LED0=1"); // turn ON LED0 on the interface
Serial.println("!O SW0=1"); // turn ON SW0 on the interface
Serial.println("!BELL"); // sound PC Bell
delay(1000); // delay 1 second
Serial.println("!O LED1=1"); // turn ON LED1 on the interface
Serial.println("!O SW1=1"); // turn ON SW1 on the interface
Serial.println("!BELL"); // sound PC Bell
delay(1000); // delay 1 second
Serial.println("!O LED2=1"); // turn ON LED2 on the interface
Serial.println("!O SW2=1"); // turn ON SW2 on the interface
Serial.println("!BELL"); // sound PC Bell
delay(1000); // delay 1 second
Serial.println("!O LED3=1"); // turn ON LED3 on the interface
Serial.println("!O SW3=1"); // turn ON SW3 on the interface
Serial.println("!BELL"); // sound PC Bell
delay(1000); // delay 1 second
Serial.println("!O LED*=0"); // turn OFF all LEDs
Serial.println("!O SW*=0"); // turn OFF all Switches
Serial.println("!BELL"); // sound PC Bell
delay(1000); // delay 1 second
}
void loop() {
Serial.println("!READ(Slider)"); // read the slider
setPoint = Serial.parseInt(); // change the setPoint to reflect it
sensorValue = analogRead(analogInPin); // get pot voltage into sensorValue
// Check pot value against setpoint, if above light LED
// And take snapshot is sensorValue ? setPoint
if (sensorValue > setPoint)
{
if (LEDstate == 0) // take snapshot if above setpoint,
// just once each crossing (if LEDstate was off)
{
Serial.println("!O butSnap=1"); // Turn on Snapshot button
Serial.println("!O butSnap.Run"); // Run snapshot event code
Serial.println("!O butSnap=0"); // Turn off snapshot button
}
LEDstate = 1;
}
else
LEDstate = 0;
digitalWrite(LEDpin,LEDstate);
// Check states of pushbuttons, if pressed change setpoint up or down
SW1state = digitalRead(SW1pin);
if (SW1state == 0)
{
setPoint++; // increment setPoint by 1
Serial.print ("!O Slider ="); // send to Makerplot to adjust slider
Serial.println(setPoint); // based on new setPoint
}
SW2state = digitalRead(SW2pin);
if (SW2state == 0)
{
setPoint--; // decrenemt setPoint by 1
Serial.print ("!O Slider ="); // send to Makerplot to adjust slider
Serial.println(setPoint); // based on new setPoint
}
// 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
// wait 100 milliseconds before the next loop
delay(100);
}