Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Dweeno Slave 2017 v1.0
- serial messaging sketch for Uno / Nano / Mega
- */
- int button_pin_2 = 2; // Button #1 is connected to pin 2.
- int button_pin_4 = 4; // Button #2 is connected to pin 4.
- int buttonValue = 0; // used to store the button input in.
- int inputPinValue = 0; // This is a temporary variable to store the input pin values in.
- int LEDpin = 13; // This is the usual #13 LED pin
- // These four variables are used for the debounce timer for the buttons.
- boolean buttonPushed = false;
- unsigned long buttonPress_startTime = 0;
- unsigned long buttonPress_currentTime = 0;
- int buttonPress_delayTime = 1000; // This is the debounce time used for both of the buttons.
- char charRead;
- int mDigits[4]; // This is the character array for the incoming message word.
- int mPosition = -1; // This is a index into the mText[] array above.
- int asciiCharValue;
- int commandWord = 0; // This is a counter for which command word is being received (12 words maximum).
- int commandArray[12]; // This is the array that will hold the 12 command words when done.
- int c_ones; // These are used for converting the serial/char data into a 4-digit number.
- int c_tens;
- int c_hundreds;
- int c_thousands;
- boolean messageIncoming = false;
- boolean messageOutgoing = false;
- // Below is variables used for the arduinoTimeDelay(int, int) function.
- int waitMinutes = 0;
- int waitSeconds = 0;
- boolean timeDelayTimer_Enabled = false;
- unsigned long timeDelay_startTime = 0;
- unsigned long timeDelay_currentTime = 0;
- int timeDelay_intervalTime = 1000;
- int timeDelay_timeCounter = 0;
- // Function declarations.
- void performCommands();
- void button1_pressed();
- void button2_pressed();
- void blink_slow(int);
- void blink_quick(int);
- void digitalRead_pin_D3();
- void analogRead_pin_A3();
- void digitalWrite_pin_D4(int);
- void analogWrite_pin_D5(int);
- void arduinoTimeDelay(int, int);
- void arduinoTimeDelayFinished();
- void resetCommandWords();
- // ##################################################################
- void setup() {
- Serial.begin(115200);
- // Note: the serial speed in the Arduino sketch and the PC program both must match!
- for (int x = 0; x < 8; x++) {
- commandArray[x] = 0;
- }
- mPosition = -1;
- commandWord = 0;
- pinMode(button_pin_2, INPUT_PULLUP);
- pinMode(button_pin_4, INPUT_PULLUP);
- pinMode(3, INPUT_PULLUP);
- //pinMode(A3, INPUT); -- analogRead doesn't require a pinmode declaration, and may not function properly with it done!
- pinMode(4, OUTPUT);
- //pinMode(5, OUTPUT); -- analogWrite doesn't require a pinmode declaration, and may not function properly with it done!
- pinMode(LEDpin, OUTPUT);
- digitalWrite(LEDpin, LOW);
- }
- // ##################################################################
- void loop() { // Beginning of main loop
- if (buttonPushed == false) {
- buttonValue = digitalRead(button_pin_2);
- if (buttonValue == 0) {
- button1_pressed();
- }
- buttonValue = digitalRead(button_pin_4);
- if (buttonValue == 0) {
- button2_pressed();
- }
- }
- if (Serial.available()) {
- while (Serial.available() > 0) {
- charRead = Serial.read();
- delay(1);
- asciiCharValue = (int) charRead;
- if (asciiCharValue == 42) { // 42 = the asterisk ***
- messageIncoming = false;
- messageOutgoing = true;
- }
- else if (asciiCharValue == 58) { // 58 = the colon :::
- convertToCommandWord();
- mPosition = -1;
- commandWord ++;
- }
- else {
- if (asciiCharValue > 47 && asciiCharValue < 58) {
- mPosition ++;
- mDigits[mPosition] = asciiCharValue - 48;
- messageIncoming = true;
- }
- }
- }
- }
- if (messageIncoming == false) {
- if (messageOutgoing == true) {
- performCommands();
- resetCommandWords();
- messageOutgoing = false;
- }
- }
- // Button debounce timing code------
- if (buttonPushed == true) {
- buttonPress_currentTime = millis();
- if (buttonPress_currentTime >= buttonPress_startTime) {
- if (buttonPress_currentTime >= (buttonPress_startTime + buttonPress_delayTime)) {
- buttonPushed = false;
- }
- }
- else {
- buttonPress_startTime = millis();
- }
- }
- // Below is variables used for the arduinoTimeDelay(int, int) function.
- if (timeDelayTimer_Enabled == true) {
- timeDelay_currentTime = millis();
- if (timeDelay_currentTime >= timeDelay_startTime) {
- if (timeDelay_currentTime >= (timeDelay_startTime + timeDelay_intervalTime)) {
- timeDelay_timeCounter++;
- if (timeDelay_timeCounter >= waitSeconds) {
- timeDelayTimer_Enabled = false;
- arduinoTimeDelayFinished();
- }
- else {
- timeDelay_startTime = millis();
- }
- }
- }
- else {
- timeDelay_startTime = millis();
- }
- }
- } // end of main loop
- // ##################################################################
- void convertToCommandWord() {
- c_ones = 0;
- c_tens = 0;
- c_hundreds = 0;
- c_thousands = 0;
- if (mPosition == 0) {
- c_ones = mDigits[0];
- }
- if (mPosition == 1) {
- c_ones = mDigits[1];
- c_tens = mDigits[0] * 10;
- }
- if (mPosition == 2) {
- c_ones = mDigits[2];
- c_tens = mDigits[1] * 10;
- c_hundreds = mDigits[0] * 100;
- }
- if (mPosition == 3) {
- c_ones = mDigits[3];
- c_tens = mDigits[2] * 10;
- c_hundreds = mDigits[1] * 100;
- c_thousands = mDigits[0] * 1000;
- }
- commandArray[commandWord] = c_ones + c_tens + c_hundreds + c_thousands;
- for (int y = 0; y < 4; y++) {
- mDigits[y] = 0;
- }
- }
- // ##############################################################
- void resetCommandWords() {
- for (int x = 0; x < 12; x++) {
- commandArray[x] = 0;
- }
- commandWord = 0;
- }
- // ##############################################################
- void commandNotFound() {
- // If the sent command is not found, then this message gets returned.
- Serial.print("???:*");
- /*
- Below is a set of lines to troubleshoot communications problems.
- If you comment out the single Serial.print() line above,
- and then un-comment-out the set of Serial.print() lines below,
- the error message returns all the numbers currently stored in commandArray[] .
- */
- /*
- Serial.print("err:");
- Serial.print(commandArray[0]);
- Serial.print(":");
- Serial.print(commandArray[1]);
- Serial.print(":");
- Serial.print(commandArray[2]);
- Serial.print(":");
- Serial.print(commandArray[3]);
- Serial.print(":");
- Serial.print(commandArray[4]);
- Serial.print(":");
- Serial.print(commandArray[5]);
- Serial.print(":");
- Serial.print(commandArray[6]);
- Serial.print(":");
- Serial.print(commandArray[7]);
- Serial.print(":");
- Serial.print(commandArray[8]);
- Serial.print(":");
- Serial.print(commandArray[9]);
- Serial.print(":");
- Serial.print(commandArray[10]);
- Serial.print(":");
- Serial.print(commandArray[11]);
- Serial.print(":?");
- */
- }
- // ############################################################
- void performCommands() {
- switch (commandArray[0]) {
- case 100:
- // blink the pin 13 LED slowly, as many times as requested
- blink_slow(commandArray[1]);
- break;
- case 101:
- // blink the pin13 LED quickly, as many times as requested
- blink_quick(commandArray[1]);
- break;
- case 103:
- // perform a digitalRead on pin D3.
- digitalRead_pin_D3();
- break;
- case 104:
- // perform an analogRead on pin A3.
- analogRead_pin_A3();
- break;
- case 105:
- // perform a digitalWrite on pin D4.
- // commandArray[1] = the value to write (zero or 1).
- digitalWrite_pin_D4(commandArray[1]);
- break;
- case 106:
- // perform an analog write on pin D5.
- // commandArray[1] = the value to write (zero to 255).
- analogWrite_pin_D5(commandArray[1]);
- break;
- case 107:
- // Use the Arduino to cause a time delay.
- // commandArray[1] = the time to wait in minutes.
- // commandArray[2] = the time to wait in seconds.
- arduinoTimeDelay(commandArray[1], commandArray[2]);
- break;
- default:
- // if nothing else matches, then signal that the command sent was not found.
- commandNotFound();
- break;
- }
- }
- // ##############################################################
- void button1_pressed() {
- Serial.print("btn:1:*");
- buttonPushed = true;
- buttonPress_startTime = millis();
- }
- void button2_pressed() {
- Serial.print("btn:2:*");
- buttonPushed = true;
- buttonPress_startTime = millis();
- }
- void blink_slow(int blinkTimes) {
- // command# = 100
- for (int x = 0; x < blinkTimes; x++) {
- digitalWrite(LEDpin, HIGH);
- delay(1000);
- digitalWrite(LEDpin, LOW);
- delay(1000);
- }
- Serial.print("100:*");
- }
- void blink_quick(int blinkTimes) {
- // command# = 101
- for (int x = 0; x < blinkTimes; x++) {
- digitalWrite(LEDpin, HIGH);
- delay(125);
- digitalWrite(LEDpin, LOW);
- delay(125);
- }
- Serial.print("101:*");
- }
- void digitalRead_pin_D3() {
- // command# = 103
- inputPinValue = digitalRead(3);
- Serial.print("103:");
- Serial.print(inputPinValue);
- Serial.print(":*");
- }
- void analogRead_pin_A3() {
- // command# = 104
- inputPinValue = analogRead(A2);
- Serial.print("104:");
- Serial.print(inputPinValue);
- Serial.print(":*");
- }
- void digitalWrite_pin_D4(int pinValue) {
- // command# = 105
- digitalWrite(4, pinValue);
- Serial.print("105:*");
- }
- void analogWrite_pin_D5(int pinValue) {
- // command# = 106
- analogWrite(5, pinValue);
- Serial.print("106:*");
- }
- void arduinoTimeDelay(int t_Minutes, int t_Seconds) {
- // command# = 107
- waitMinutes = t_Minutes * 60;
- waitSeconds = (waitMinutes + t_Seconds);
- timeDelay_timeCounter = 0;
- timeDelayTimer_Enabled = true;
- timeDelay_startTime = millis();
- }
- void arduinoTimeDelayFinished() {
- // This gets called when the arduinoTimeDelay() function has completed.
- Serial.print("107:*");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement