Skip to main content
added 540 characters in body
Source Link
chrisl
  • 16.6k
  • 2
  • 18
  • 27

#include <Keypad.h>

String inputString = ""; // a String to hold incoming data bool stringComplete = false; // whether the string is complete

const byte ROWS = 4; //four rows const byte COLS = 4; //three columns char keys[ROWS][COLS] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'},

}; byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int iAm = 1; //this is device number 1 float checkSum = 0.00; //check sum to send as serial float voltage = 5.26; //system voltage int data[20] {0, 0, 0, 0, 0, 0, 0, 0, 0}; //keypad output call char rsvp = 0; int rsvp1 = 0; void setup() { Serial.begin(9600);

inputString.reserve(200); // reserve 200 bytes for the inputString: }

void loop() {

int key = keypad.getKey();

if (key) { int key1 = key -48;

#include <Keypad.h>

String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},

};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


int iAm = 1;                                    //this is device number 1
float checkSum = 0.00;                          //check sum to send as serial
float voltage = 5.26;                           //system voltage
int data[20] {0, 0, 0, 0, 0, 0, 0, 0, 0};       //keypad output call
char rsvp = 0;
int rsvp1 = 0;
void setup() {
  Serial.begin(9600);
  
  inputString.reserve(200);    // reserve 200 bytes for the inputString:
}


void loop() {

  int key = keypad.getKey();
  
  
  if (key) {
      int key1 = key -48;
      
      if (data[key1] == 1){
       data[key1] = 0;
        
      }
      else {
        data[key1] = 1;
      }
      

} // print the string when a newline arrives: if (stringComplete) { rsvp = inputString.charAt(0); rsvp1 = int(rsvp);

  }
    // print the string when a newline arrives:
  if (stringComplete)  {
    rsvp = inputString.charAt(0);
    rsvp1 = int(rsvp);
    
    if (rsvp1 = iAm) {
      checkSum = voltage * (iAm + data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9]); 
      Serial.print(inputString); 
      Serial.println(rsvp1);
      Serial.print(iAm);
      Serial.print("/");
      Serial.print(data[1]);
      Serial.print("/");
      Serial.print(data[2]);
      Serial.print("/");
      Serial.print(data[3]);
      Serial.print("/");
      Serial.print(data[4]);
      Serial.print("/");
      Serial.print(data[5]);
      Serial.print("/");
      Serial.print(data[6]);
      Serial.print("/");
      Serial.print(data[7]);
      Serial.print("/");
      Serial.print(data[8]);
      Serial.print("/");
      Serial.print(data[9]);
      Serial.print("/");
      Serial.print(voltage);
      Serial.println("/");
      Serial.println(checkSum);
    // clear the string:
    inputString = "";
    stringComplete = false;
    }
  }
}
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

/*
  SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  routine is run between each time loop() runs, so using delay inside loop can
  de
   if (Serial.available() > 0) {
    int recieved = Serial.read();
    if (recieved == iAm){
      checkSum = voltage * (iAm + data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9]); 
      Serial.print(iAm);
      Serial.print("/");
      Serial.print(data[1]);
      Serial.print("/");
      Serial.print(data[2]);
      Serial.print("/");
      Serial.print(data[3]);
      Serial.print("/");
      Serial.print(data[4]);
      Serial.print("/");
      Serial.print(data[5]);
      Serial.print("/");
      Serial.print(data[6]);
      Serial.print("/");
      Serial.print(data[7]);
      Serial.print("/");
      Serial.print(data[8]);
      Serial.print("/");
      Serial.print(data[9]);
      Serial.print("/");
      Serial.print(voltage);
      Serial.println("/");
      Serial.println(checkSum);
    }
   }
}

 

} } void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); // add it to the inputString: inputString += inChar; // if the incoming character is a newline, set a flag so the main loop can // do something about it: if (inChar == '\n') { stringComplete = true; } } }

/* SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can de if (Serial.available() > 0) { int recieved = Serial.read(); if (recieved == iAm){ checkSum = voltage * (iAm + data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9]); Serial.print(iAm); Serial.print("/"); Serial.print(data[1]); Serial.print("/"); Serial.print(data[2]); Serial.print("/"); Serial.print(data[3]); Serial.print("/"); Serial.print(data[4]); Serial.print("/"); Serial.print(data[5]); Serial.print("/"); Serial.print(data[6]); Serial.print("/"); Serial.print(data[7]); Serial.print("/"); Serial.print(data[8]); Serial.print("/"); Serial.print(data[9]); Serial.print("/"); Serial.print(voltage); Serial.println("/"); Serial.println(checkSum); } } }

#include <Keypad.h>

String inputString = ""; // a String to hold incoming data bool stringComplete = false; // whether the string is complete

const byte ROWS = 4; //four rows const byte COLS = 4; //three columns char keys[ROWS][COLS] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'},

}; byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int iAm = 1; //this is device number 1 float checkSum = 0.00; //check sum to send as serial float voltage = 5.26; //system voltage int data[20] {0, 0, 0, 0, 0, 0, 0, 0, 0}; //keypad output call char rsvp = 0; int rsvp1 = 0; void setup() { Serial.begin(9600);

inputString.reserve(200); // reserve 200 bytes for the inputString: }

void loop() {

int key = keypad.getKey();

if (key) { int key1 = key -48;

  if (data[key1] == 1){
   data[key1] = 0;
    
  }
  else {
    data[key1] = 1;
  }
  

} // print the string when a newline arrives: if (stringComplete) { rsvp = inputString.charAt(0); rsvp1 = int(rsvp);

if (rsvp1 = iAm) {
  checkSum = voltage * (iAm + data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9]); 
  Serial.print(inputString); 
  Serial.println(rsvp1);
  Serial.print(iAm);
  Serial.print("/");
  Serial.print(data[1]);
  Serial.print("/");
  Serial.print(data[2]);
  Serial.print("/");
  Serial.print(data[3]);
  Serial.print("/");
  Serial.print(data[4]);
  Serial.print("/");
  Serial.print(data[5]);
  Serial.print("/");
  Serial.print(data[6]);
  Serial.print("/");
  Serial.print(data[7]);
  Serial.print("/");
  Serial.print(data[8]);
  Serial.print("/");
  Serial.print(data[9]);
  Serial.print("/");
  Serial.print(voltage);
  Serial.println("/");
  Serial.println(checkSum);
// clear the string:
inputString = "";
stringComplete = false;
}

} } void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); // add it to the inputString: inputString += inChar; // if the incoming character is a newline, set a flag so the main loop can // do something about it: if (inChar == '\n') { stringComplete = true; } } }

/* SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can de if (Serial.available() > 0) { int recieved = Serial.read(); if (recieved == iAm){ checkSum = voltage * (iAm + data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9]); Serial.print(iAm); Serial.print("/"); Serial.print(data[1]); Serial.print("/"); Serial.print(data[2]); Serial.print("/"); Serial.print(data[3]); Serial.print("/"); Serial.print(data[4]); Serial.print("/"); Serial.print(data[5]); Serial.print("/"); Serial.print(data[6]); Serial.print("/"); Serial.print(data[7]); Serial.print("/"); Serial.print(data[8]); Serial.print("/"); Serial.print(data[9]); Serial.print("/"); Serial.print(voltage); Serial.println("/"); Serial.println(checkSum); } } }

#include <Keypad.h>

String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},

};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


int iAm = 1;                                    //this is device number 1
float checkSum = 0.00;                          //check sum to send as serial
float voltage = 5.26;                           //system voltage
int data[20] {0, 0, 0, 0, 0, 0, 0, 0, 0};       //keypad output call
char rsvp = 0;
int rsvp1 = 0;
void setup() {
  Serial.begin(9600);
  
  inputString.reserve(200);    // reserve 200 bytes for the inputString:
}


void loop() {

  int key = keypad.getKey();
  
  
  if (key) {
      int key1 = key -48;
      
      if (data[key1] == 1){
       data[key1] = 0;
        
      }
      else {
        data[key1] = 1;
      }
      
  }
    // print the string when a newline arrives:
  if (stringComplete)  {
    rsvp = inputString.charAt(0);
    rsvp1 = int(rsvp);
    
    if (rsvp1 = iAm) {
      checkSum = voltage * (iAm + data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9]); 
      Serial.print(inputString); 
      Serial.println(rsvp1);
      Serial.print(iAm);
      Serial.print("/");
      Serial.print(data[1]);
      Serial.print("/");
      Serial.print(data[2]);
      Serial.print("/");
      Serial.print(data[3]);
      Serial.print("/");
      Serial.print(data[4]);
      Serial.print("/");
      Serial.print(data[5]);
      Serial.print("/");
      Serial.print(data[6]);
      Serial.print("/");
      Serial.print(data[7]);
      Serial.print("/");
      Serial.print(data[8]);
      Serial.print("/");
      Serial.print(data[9]);
      Serial.print("/");
      Serial.print(voltage);
      Serial.println("/");
      Serial.println(checkSum);
    // clear the string:
    inputString = "";
    stringComplete = false;
    }
  }
}
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

/*
  SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  routine is run between each time loop() runs, so using delay inside loop can
  de
   if (Serial.available() > 0) {
    int recieved = Serial.read();
    if (recieved == iAm){
      checkSum = voltage * (iAm + data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9]); 
      Serial.print(iAm);
      Serial.print("/");
      Serial.print(data[1]);
      Serial.print("/");
      Serial.print(data[2]);
      Serial.print("/");
      Serial.print(data[3]);
      Serial.print("/");
      Serial.print(data[4]);
      Serial.print("/");
      Serial.print(data[5]);
      Serial.print("/");
      Serial.print(data[6]);
      Serial.print("/");
      Serial.print(data[7]);
      Serial.print("/");
      Serial.print(data[8]);
      Serial.print("/");
      Serial.print(data[9]);
      Serial.print("/");
      Serial.print(voltage);
      Serial.println("/");
      Serial.println(checkSum);
    }
   }
}

 
Source Link

processing data received via serial

Hey i'm trying to make an Arduino that can log key presses from a keypad and save that as a variable. When it receives information from another arduino via serial I want it to create a variable from each position (like comma separated values "/" in this case).Then if its being asked for its data by the master to transmit its string of data.

The code I have below is recording key presses, when it receives serial it puts it in to a string then looks at the first position in that string this becomes the variable RSVP, if RSVP is equal to the Arduinos address (iAm) and later if the checksum adds up, I would like it to respond with its serial string. for some reason every serial command i send to it, it mirrors back the command i sent to it correctly but when i tell it to separate the first position in to the RSVP variable it always returns 1. Not sure if this is to do with converting characters to integers or something if that's all then hopefully an easy fix once you know how.

I have seen a few posts saying its best to use an array rather than a string (but this is based on the IDE serial example so not sure if that's correct)and presumable would need fully re writing which doesn't sound like an easy fix. Also I don't know how I would go about making an array from serial, and I might not to know how to separate CSVs I have seen a few examples but unfortunately i'm a dimwit. Been on this one problem for 3 days now and i'm still in exactly the same place, but with a slightly fried brain.

#include <Keypad.h>

String inputString = ""; // a String to hold incoming data bool stringComplete = false; // whether the string is complete

const byte ROWS = 4; //four rows const byte COLS = 4; //three columns char keys[ROWS][COLS] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'},

}; byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int iAm = 1; //this is device number 1 float checkSum = 0.00; //check sum to send as serial float voltage = 5.26; //system voltage int data[20] {0, 0, 0, 0, 0, 0, 0, 0, 0}; //keypad output call char rsvp = 0; int rsvp1 = 0; void setup() { Serial.begin(9600);

inputString.reserve(200); // reserve 200 bytes for the inputString: }

void loop() {

int key = keypad.getKey();

if (key) { int key1 = key -48;

  if (data[key1] == 1){
   data[key1] = 0;
    
  }
  else {
    data[key1] = 1;
  }
  

} // print the string when a newline arrives: if (stringComplete) { rsvp = inputString.charAt(0); rsvp1 = int(rsvp);

if (rsvp1 = iAm) {
  checkSum = voltage * (iAm + data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9]); 
  Serial.print(inputString); 
  Serial.println(rsvp1);
  Serial.print(iAm);
  Serial.print("/");
  Serial.print(data[1]);
  Serial.print("/");
  Serial.print(data[2]);
  Serial.print("/");
  Serial.print(data[3]);
  Serial.print("/");
  Serial.print(data[4]);
  Serial.print("/");
  Serial.print(data[5]);
  Serial.print("/");
  Serial.print(data[6]);
  Serial.print("/");
  Serial.print(data[7]);
  Serial.print("/");
  Serial.print(data[8]);
  Serial.print("/");
  Serial.print(data[9]);
  Serial.print("/");
  Serial.print(voltage);
  Serial.println("/");
  Serial.println(checkSum);
// clear the string:
inputString = "";
stringComplete = false;
}

} } void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); // add it to the inputString: inputString += inChar; // if the incoming character is a newline, set a flag so the main loop can // do something about it: if (inChar == '\n') { stringComplete = true; } } }

/* SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can de if (Serial.available() > 0) { int recieved = Serial.read(); if (recieved == iAm){ checkSum = voltage * (iAm + data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9]); Serial.print(iAm); Serial.print("/"); Serial.print(data[1]); Serial.print("/"); Serial.print(data[2]); Serial.print("/"); Serial.print(data[3]); Serial.print("/"); Serial.print(data[4]); Serial.print("/"); Serial.print(data[5]); Serial.print("/"); Serial.print(data[6]); Serial.print("/"); Serial.print(data[7]); Serial.print("/"); Serial.print(data[8]); Serial.print("/"); Serial.print(data[9]); Serial.print("/"); Serial.print(voltage); Serial.println("/"); Serial.println(checkSum); } } }