Saturday, March 2, 2024

arduino uno – Cannot get serial port’s consideration as soon as sketch is operating


This sketch is meant to pay attention for a single character despatched to the serial port, as a way of beginning and stopping knowledge assortment (which on this instance is mocked by printing some register values). It reacts to the primary character despatched (g), and the whereas loop begins executing. Nevertheless it would not react to a subsequent s character, it appears I am unable to get Arduino’s consideration.

Maybe that is associated to this reply however I am unsure. If that is the issue, I may use some basic recommendations about work across the limitations. If it is one thing else, properly clearly I would like a suggestion for that as properly!

// Take heed to the Serial Port for Directions and Print Register Values

unstable boolean begin = false;  // flag to start out register inspection; international

void setup() {
  Serial.start(9600);
  if (Serial) {
    Serial.println("Arduino listening...");
    Serial.println("Enter g or s at any time");  // g = go, s = cease
  }
}

void loop() {
  unsigned char knowledge;
  if (Serial.obtainable()) {
    knowledge = Serial.learn();
    Serial.println(knowledge); // just for troubleshooting
    if (knowledge == 'g') {
      begin = true;
    }
    else  if (knowledge == 's') {
      begin = false;
    }
  }
  
  whereas (begin) {
    // print the values of some ADC registers
    Serial.print("ADMUX  register: ");
    print_bin(ADMUX);
    Serial.print("ADCSRA register: ");
    print_bin(ADCSRA);
    Serial.println(" ");
    delay(1000);
  }
}


void print_bin(byte aByte) {
  for (int8_t aBit = 7; aBit >= 0; aBit--) {
    if (aBit == 3) {
      Serial.print(" ");
    }
    Serial.print(bitRead(aByte, aBit) ? '1' : '0');
  }
  Serial.println(" ");
}

Surroundings:
Model: 2.3.0
Date: 2024-02-07T13:40:21.377Z
CLI Model: 0.35.2

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles