-2

I have simplified the question very much. I am trying to send the encoder motor feed back to Processing which will be displayed on the processing. But the error is coming something like:

error disabling serialEvent for "COM3" null

Processing:

import processing.serial.*;

Serial port;
int index = 0;
String lmotor, rmotor, data, status;

void setup()
{
    size(500, 500);
    port = new Serial(this, "COM3", 9600);
    port.bufferUntil('A');
}

void serialEvent(Serial port)
{
    if(data != null)
    {
        println(data);
        data = port.readStringUntil('A');
        data = data.substring(0, data.length()-1);
        index = data.indexOf(",");
        lmotor = data.substring(0, index);
        rmotor = data.substring(index+1, data.length());
    }
}

void draw()
{
    background(255);
    textSize(100);
    fill(random(0, 255), 0, random(0, 255));
    text(lmotor, 50, 150);
    textSize(100);
    fill(0, random(0, 255), random(0, 255));
    text(rmotor, 300, 150);
}

Arduino/AVR code:

#include <avr/io.h>
#include <util/delay.h>

#define SETBIT(ADDRESS,   BIT) (ADDRESS|=(1<<BIT));
#define CLEARBIT(ADDRESS, BIT) (ADDRESS&=~(1<<BIT));
#define CHECKBIT(ADDRESS, BIT) (ADDRESS & (1<<BIT));

char c;
int sfl=0, sfr=0, y=0, temp;

void interrupt_init()
{
    cli();
    EIMSK = 0x03;
    EICRA = 0x0A;
    sei();
}

void encoder_pin_config_init()
{
    DDRD = DDRD & 0xF3;
    // Internal pull-up resistors
    SETBIT(PORTD, PD3);
    SETBIT(PORTD, PD2);
}

void byte_init (int baud)
{
    UBRR0H = (baud>>8);                  // Shift the register right by 8 bits
    UBRR0L = baud;                       // Set baud rate
    UCSR0B |= (1<<TXEN0)  | (1<<RXEN0);  // Enable receiver and transmitter
    UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); // 8-bit data format
}

void byte_transmit (unsigned char data)
{
    while (!(UCSR0A & (1<<UDRE0)))  // Wait while register is free
        ;
    UDR0 = data;                    // Load data in the register
}

unsigned char byte_receive (void)
{
    while(!(UCSR0A) & (1<<RXC0))   // Wait while data is being received
        ;
    return UDR0;                   // Return 8-bit data
}

void setup()
{
    interrupt_init();
    byte_init(103);
    encoder_pin_config_init();

    Serial.begin(9600);

    pinMode(4, HIGH);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);

    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
}

void loop()
{
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    temp = sfl;
    while(temp>0)
    {
        y = temp%10;
        c = y + '0';
        byte_transmit(c);
        temp = temp/10;
    }
    byte_transmit(',');
    temp = sfr;
    while(temp>0)
    {
        y = temp%10;
        c = y + '0';
        byte_transmit(c);
        temp = temp/10;
    }
    byte_transmit('A');

    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(4, LOW);
}

ISR(INT0_vect)
{
    sfl++;
}

ISR(INT1_vect)
{
    sfr++;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saad Anwar
  • 41
  • 7
  • Indent your code properly. This is not easy to read. – TomServo Jun 18 '17 at 20:37
  • @JLH i have edited th equestion you may check it again now. – Saad Anwar Jun 20 '17 at 01:07
  • What kind of error? Compile error? Run time error? During flashing of the microcontroller? When does it happen? Under what circumstances? Where is the error displayed? Inside the [Arduino IDE](https://en.wikipedia.org/wiki/Arduino_IDE) on Windows? Where exactly? – Peter Mortensen Sep 06 '22 at 15:35
  • [A similar error](https://stackoverflow.com/questions/26070466/arduino-and-processing-code-error-disabling-serialevent) - *"in Processing application"* (whatever that is - in the environment / IDE? At run time?) – Peter Mortensen Sep 06 '22 at 19:46
  • The same error is in *[Arduino + Proccesing code error “disabling_serialevent()”](https://stackoverflow.com/questions/42618052/)*. Is it an exception that is thrown and then shown somewhere in the [Processing](https://en.wikipedia.org/wiki/Processing_%28programming_language%29) editor? An output window? – Peter Mortensen Sep 08 '22 at 09:42
  • OK, the OP has left the building (*"Last seen more than 1 year ago"*). But maybe someone else can chime in? It seems to be a common error. – Peter Mortensen Sep 08 '22 at 10:10

2 Answers2

0

You're hardcoding COM3. The port may not be correct.

From the Serial reference:

// Example by Tom Igoe

import processing.serial.*;

// The serial port:
Serial myPort;

// List all the available serial ports:
printArray(Serial.list());

// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[0], 9600);

// Send a capital A out the serial port:
myPort.write(65);

Notice that this code uses the Serial.list() function to list the valid ports. Try using this to figure out which ports are being used.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • pretty sure about the COM3 thing because i went into device manager and checked it before writing it into the code also the bot is being controlled by GUi perfectly through bluetooth but the encoder feedback from bot is unable to come to processing and we are getting the error as stated above. – Saad Anwar Jun 18 '17 at 22:37
  • i have posted the minimal example haveing the similar error. – Saad Anwar Jun 20 '17 at 01:08
0

One thing you must do is use the volatile keyword for the variables you're modifying inside the ISRs. Please read up here on Stack Overflow—or anywhere—on why you need volatile. Your code is almost guaranteed to not work if you modify a plain int inside an ISR. That's the first thing that jumps right out at me.

The other thing that I wonder about is using low-speed serial to communicate motor position encoding. Unless your motor is moving very slowly, you'll want to use fast interrupts to catch your encoder signals. This is very unconventional and is likely not to work as you expect.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TomServo
  • 7,248
  • 5
  • 30
  • 47
  • well 9600 is the baud rate which i think is sufficient to transmit the data . and yes i am using interrupts which are pretty fast to catch the encoder signals as i have pre checked it before trying serial communication on the arduino serial monitor. the encoders are working perfectly fine with interrupts fast enough to catch the encoder signal. the only thing in which i doubt the problem is in either transmission or in receiving the data in processing. – Saad Anwar Jun 20 '17 at 01:26