0

Hi we were asked to program something which when i enter something in the serial monitor it prints Hello world but the catch is it only prints hello world under the conditions that your input in the serial monitor is a 3 bit input and after that i need to have a code which then stops it from printing over and over again. my only problem is that i don't really know how to count the number of bits of an input and also to stop it from printing continuously.

void setup() {
  Serial.begin(9600);

}

void loop() {
  String readSerial;
    String input;
    while(Serial.available() > 0){// put your main code here, to run repeatedly:
    delay(2);
  input.concat(String((char)Serial.read()));
  return input;
  }
  Serial.println("HELLO WORLD");
}
  • Is this a school class assignment? – Mikael Patel Oct 3 '18 at 10:35
  • Please add your code. – Mikael Patel Oct 3 '18 at 10:36
  • Yes it is. And its also an assignment before the actual lesson – Underhanded CODE Oct 3 '18 at 10:44
  • What exactly do you mean by 3-bit input? The serial interface only sends whole bytes. Is this about specific values in 3 of the 8 bits of a single byte? – chrisl Oct 3 '18 at 11:10
  • it says here that 3 ready to be read character(bytes) is that 3 bits? or am is my assumption wrong? – Underhanded CODE Oct 3 '18 at 11:24
3

Judging from the comments, you lag some basic knowledge. To really learn, what you are supposed to learn in this assignment, you should review your learning material or C/C++ and Arduino Tutorials on the web. Here I will only give you some hints with reasoning, that may help you in solving the assignments task. (Unfortunately it is not really clear, what exactly the task is here)

  • The Serial interface of the Arduino (an UART interface) sends the data in bytes, one at a time and one after another (hence the name serial). 1 byte is a series of 8 bits, each of them with a value of 1 or 0. Together the 8 bits form a value, that can also be expressed as a decimal value from 0 to 255. For example 01001011 is the binary equivalent of the decimal number 75. (For further explanations google the binary number system) The byte data can also be expressed as characters, as they are defined in the ASCII standard. Every binary value refers to a specific character. This is what happens in the serial monitor of the Arduino IDE. (For more explanations google ASCII)
  • Since the data is send serially, there may appear some delays in the transmission, which are unnoticeable by humans, but the Arduino is fast enough, so that the Serial.available() command may return 0, because at that moment a little delay in the transmission occurred. Hence it is important to catch the whole message, before trying to work with it. You can do this by either counting bytes (waiting until at least 3 bytes are available in the buffer) or by marking the end of the message with a special character (for example the newline character '\n'). There are a lot of tutorials on the web how to do both. You can start with Majenko's blog.
  • You can get the number of bytes, that were received, with the return value of Serial.available(). It will return 3, when there are 3 ready to be read bytes in the buffer.
  • If you define a variable inside the void loop(), it will be thrown away when the function ends and redefined, when the function starts again (In the Arduino world the void loop() function is called again and again forever). You will loose all content, that was written to this variable. If you want to persist data over the executions of the void loop() function, you can define the variable outside of any function at the top of your program. This variable is then global and will be defined only once at the start of the program and hold for the whole lifetime of the program.
  • As wrote in the comment you should not return a value from a void function, since void mostly means, that there is no return value. And returning from the void loop() function will only restart this function (since we are in the Arduino world).
  • You can ditch the delay(2). There is no need for it. And if it only works with the delay, then something else is not correct and you are just working with the symptoms instead with the real issue.

Also you should not try to build everything on your own and reinvent the wheel. Instead try to look at the examples, that come with the Arduino IDE. For example the SerialEvent example, which shows how to receive a serial message (which is ended by a newline character '\n'). Also have a look at the other related examples. If you understand, how they work, you will be able to do the assignment without any problem.

  • Hi again , i think I'm close to getting it but one quick question is there anyway of resetting the serial.available(); value and reverting it back to 0 ? – Underhanded CODE Oct 3 '18 at 15:07
  • You can do Serial.read() without using the returned value, until Serial.available() returns 0. – chrisl Oct 3 '18 at 16:08
1

Assuming that the sketch should print "Hello World" when the input character has 3 bits high. Here are a few different variants of counting the number of bits set in a byte.

int bits1(int c) {
  int res = 0;
  for (int i = 0; i < 8; i++) {
    if ((1 << i) & c) res = res + 1;
  }
  return (res);
}

int bits2(int c) {
  int m = 0x80;
  int res = 0;
  do {
    if (m & c) res++; 
  } while (m >>= 1);
  return (res);
}

int bits3(int c) {
  int m = 0x80;
  int res = 0;
  do {
    res += (m & c) != 0; 
  } while (m >>= 1);
  return (res);
}

#define bits4(c) __builtin_popcount(c) 

#define bits bits4

void setup() {
 Serial.begin(9600);
 while (!Serial);
}

void loop() {
  if (Serial.available() > 0) {
    int c = Serial.read();
    if (bits(c) == 3) {
      Serial.print((char) c);
      Serial.print(':');
      Serial.print(c, BIN);
      Serial.println(": Hello World");
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.