1

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);
    }
   }
}

 
7
  • the program does not record the sequence of keypresses ... it only records which keys were pressed ... is that the desired behavior? Commented Mar 16, 2021 at 16:14
  • yea that's correct, its just to tell the master which outputs it would like off or on, the master will reply telling the keypad which outputs are on which will turn on an associated LED on the keypad. any additional keypads on the same serial network will also get the message and know to update their demand for outputs to the current master state. Commented Mar 16, 2021 at 16:47
  • do you have an answer to the original question jsotola? Commented Mar 17, 2021 at 19:39
  • 1
    one of your if statements does not evaluate the comparison of two values, but actually assigns the value of one variable to another variable ... it is what is called a logic error ... syntax is correct, but the if statement does not do what you expected Commented Mar 17, 2021 at 20:42
  • main question is why are you not debugging the code? ... i see no serial.print() statements throughout your code, so that you could see the value of variables as the program execution progresses Commented Mar 17, 2021 at 20:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.