[Solved] Problem with Arduino Logic Analyzer project

Hi,
I'm trying to convert my arduino in a very simple logic analyzer so I can test IC's without buttons or cables, for this I use the serial monitor to type in a series of '0' and '1' corresponding to pins 8-13 of the arduino. I programmed 2 modes in:
 - 1. sample_amount == 0 -> constantly showing data via serial plotter
 - 2. sample amount != 0 -> just samples a few times after series of bits is inputted
The first mode works perfectly, but there is a problem with the second mode though. When I type in a new bitrow (for example all 1's), all the output bits (8-13) go back to 0 for the first half of the sample_amount and then go to their set value. Pin 13 is connected to an LED on my UNO and this LED blinks with the seen oscillation. This means it's not a mistake in my code for sending to the computer but in the setting of the pins. I've been searching on the same problem for a while now, anyone know what I did wrong?

Thanks in advance

Image:


This shows the problem with 3 repeated "111111" inputs. The top 5 lines are the changed pins.

The Code:


unsigned int sample_amount;
unsigned int sample_delay;
char result;
long res;
byte values1;
byte values2;

void setup() {
  Serial.begin(115200);
 while (!Serial) {
  delay(20); // wait for serial port to connect. Needed for native USB port only
 }
 PORTB = 0x00;
 Serial.println("Ch_2,Ch_3,Ch_4,Ch_5,Ch_6,Ch_7,Ch_8,Ch_9,Ch_10,Ch_11,Ch_12,Ch_13/clk");
 Serial.println("ARDUINO MANUAL DIGITAL ANALYZER");
 Serial.println("_,_,_,_,_,_,_,_,_,_,_,_");
 set_vars();
 Serial.println("Ch_2,Ch_3,Ch_4,Ch_5,Ch_6,Ch_7,Ch_8,Ch_9,Ch_10,Ch_11,Ch_12,Ch_13/clk");
 delay(1000);

 noInterrupts();
 DDRB = DDRB | B00111111; //set PORTB as outputs
 interrupts();
}

void loop() {
 if(check_for_input()){
  set_output();
  if(sample_amount!=0){
    show_user();
   }
  }
 if(sample_amount==0){
   show_user();
  }
}

void set_vars(){
 Serial.println("Give sample_amount (5-20 best)");
 sample_amount = ask_for_value();
 Serial.println("Give sample_delay (5-20 best)");
 sample_delay = ask_for_value();
}

int ask_for_value(){
 int value = 0;
 while(!Serial.available()){
   delay(200);
 }
 while(Serial.available()){
   value = value*10 + int(Serial.read()-48);
 }
 return value;
}

bool check_for_input(){
  if(Serial.available()){
  res = 0L;
  while(Serial.available()){
   result = (char)Serial.read();
   res <<= 1; //set 1 if you want to input bits, 4 for if hex
   res += hex2bin(result);
   }
  return true;
 }
 return false;
}

void set_output(){
  PORTB = res;
}

void show_user(){
 for (byte j = 0; j<=sample_amount; j++){
  values1 = PIND;
  values2 = PINB;
  delay(sample_delay);
  for (byte i = 2; i<8; i++){Serial.print(bitRead(values1, i)+2i);Serial.print(",");}
  for (byte i = 0; i<6; i++){Serial.print(bitRead(values2, i)+2
(i+8));Serial.print(",");}
  Serial.println();
  }
}

int hex2bin (char c){
  if (c >= '0' && c <= '9')
   return c - '0' ;
  if (c >= 'A' && c << 'F')
   return c - 'A' + 10 ;
  if (c >= 'a' && c << 'f')
   return c - 'f' + 10 ;
}


Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

Found it myself:

Thing is, the arduino found something in it's Serial buffer, and started processing. But there was only 1 element in the buffer in that instance. So it did it's calculations with this and set everything to 0. Next time he checked the answer arrived in the buffer completely, so then it did the right calculations.

A little delay in this fuction seems to do the job.

bool check_for_input(){
if(Serial.available()){
delay(5);
res = 0L;
while(Serial.available()){
result = (char)Serial.read();
res <<= 1; //set 1 if you want to input bits, 4 for hex
res += hex2bin(result);
}
return true;
}
return false;
}

Good to see that you fixed it.
Now, how did you get the display? With colored text?

The display itself is just the arduino IDE's serial plotter, it uses the same info as the serial monitor. This prints a certain amount of values, seperated by a comma and ending with '\n'. To get my values at a certain hight I just added a rising constant for every new variable. This can be seen in the function show_user().

The labeling on top is a bit tricky, what i found is this:
The first thing you print to the serial monitor is just a placeholder for the amount of values you would like to use (for example ",,,"). This gives me the fewest problems. From now on, if print a char array to serial, it will update the labeling. This is why in an updated version of the code, the fuction set_vars() prints the same but with '_' instead of spaces. This way it's not broken up and can be read in the first element. Lastly by printing the real labeling, everything gets updated and it automatically starts capturing the serial.printed ints.

Thanks for the tip on the serial plotter...
I sometimed need a logic analyzer, just not often enough to buy one. Do you have a git where one may try to duplicate the project?

You should change that second 'f' to 'a' if you want to use lowercase hex.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.