I have a working piece of code that measures force with an AD converter and movement with an encoder strip. Data is transported to a PC by USB to an VB6 application. I need to add a motor control from VB6 (simple motor on motor off) to the arduino UNO. So I started with the basics of that but got stuck.
I have piece of code that switch on and off a led with processing through serial (before I implement it in VB6) . This also works fine and when I change the state in processing the led goes off imediatly and vise versa. So I took this code and pasted it in the force/motion code. The behaviour I get is that the led switch on and off in a random eratic delay. I know this is a basic problem but I am overlooking something very simple I think.
Any guidance would be appriciated.
Code for LED on off
const int ledPin = 8; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
}
}
}
Code for force and movement with led on off added.
// Interrupt information optical encoder
// 0 on pin 2
// 1 on pin 3
//default connection arduino NANO/UNO TM7709 24 bit ADC
//DRDY = pin 11 >MOSI
//ADIO = pin 12 >MISO
//SCLK = pin 13 >SCK
//default connection arduino MEGA TM7709 24 bit ADC
//DRDY = pin 21 >MOSI
//ADIO = pin 22 >MISO
//SCLK = pin 28 >SCK
#define TM7710_DRDY 3
#define TM7710_ADIO 4
#define TM7710_ADIO_OUT() DDRB|=1<<4
#define TM7710_ADIO_IN() DDRB&=~(1<<4)
#define Set_TM7710_SCLK() PORTB|=1<<5
#define Set_TM7710_ADIO() PORTB|=1<<4
#define Clr_TM7710_SCLK() PORTB&=~(1<<5)
#define Clr_TM7710_ADIO() PORTB&=~(1<<4)
unsigned char x[3];
long Result;
float vref=4.57;
int motorValue ;
const int ledPin6 = 6; // the pin that the LED is attached to
const int ledPin7 = 7; // the pin that the LED is attached to
int led = 8;
int incomingByte; // a variable to read incoming serial data into
enum PinAssignments
{
encoderPinA = 2, // rigth
encoderPinB = 3, // left
};
volatile unsigned int encoderPos = 5000; // a starting counterposition for the encoder
unsigned int lastReportedPos = 1; // change management
static boolean rotating=false; // debounce management
// interrupt service routine vars
boolean A_set = false;
boolean B_set = false;
void setup()
{
DDRB|=1<<5 ;
Serial.begin(9600);// set baudrate for PC too!
TM7710_Init();
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
// turn on pullup resistors
digitalWrite(encoderPinA, HIGH);
digitalWrite(encoderPinB, HIGH);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, doEncoderB, CHANGE);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
pinMode(led,OUTPUT);
}
void loop()
{
/------------------------------------------ problem part ----------------------------------------
if (Serial.available() > 0)
{
incomingByte = Serial.read();
if (incomingByte == '1')
{
digitalWrite(ledPin6, HIGH);
digitalWrite(ledPin7, LOW);
}
if (incomingByte == '0')
{
digitalWrite(ledPin6, LOW);
digitalWrite(ledPin7, HIGH);
}
}
/------------------------------------------------------------------------------------
double volt = Result * vref /16 / 6.912;
while((PINB&(1<<TM7710_DRDY))==(1<<TM7710_DRDY));
TM7710_start();
TM7710_write(0x7F);
TM7710_ADIO_IN();
for(unsigned char j=0;j<3;j++)
{
x[j]=TM7710_read();
}
TM7710_ADIO_OUT();
TM7710_stop();
Result=x[0];
Result = Result * 256;
Result = Result + x[1];
Result = Result * 256;
Result = Result + x[2];
Result = Result - 6912000;
Serial.print ("A,");
Serial.println (volt,0);
Serial.print("B,");
Serial.println(encoderPos, DEC);
Serial.print("C,");
Serial.println (motorValue);
rotating = true; // reset the debouncer
if (lastReportedPos != encoderPos) {
lastReportedPos = encoderPos;
}
}
void TM7710_start(void)
{
Clr_TM7710_ADIO();
delayMicroseconds(1);
Clr_TM7710_SCLK();
delayMicroseconds(1);
}
void TM7710_stop(void)
{
Clr_TM7710_ADIO();
delayMicroseconds(1);
Set_TM7710_SCLK();
delayMicroseconds(1);
Set_TM7710_ADIO();
delayMicroseconds(1);
}
void TM7710_write(unsigned char dd)
{
unsigned char i;
for(i=8;i>0;i--)
{
if(dd&0x80)
Set_TM7710_ADIO();
else
Clr_TM7710_ADIO();
delayMicroseconds(1);
Set_TM7710_SCLK();
delayMicroseconds(1);
Clr_TM7710_SCLK();
dd<<=1;
}
}
unsigned char TM7710_read(void)
{
unsigned char data=0,i;
for(i=0;i<8;i++)
{
Set_TM7710_SCLK();
data=data<<1;
if((PINB&(1<<TM7710_ADIO))==(1<<TM7710_ADIO))
{
data=data+1;
}
delayMicroseconds(1);
Clr_TM7710_SCLK();
delayMicroseconds(1);
}
return data;
}
void TM7710_Init()
{
TM7710_ADIO_OUT();
delay(100);
TM7710_stop();
TM7710_start();
TM7710_write(0xBF);
TM7710_write(0x20); //ADC preamp Gain=128
TM7710_stop();
}
// Interrupt on A changing state
void doEncoderA(){
// debounce
if ( rotating ) delay (1); // wait a little until the bouncing is done
// Test transition, did things really change?
if( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
encoderPos += 1;
rotating = false; // no more debouncing until loop() hits again
}
}
// Interrupt on B changing state, same as A above
void doEncoderB(){
if ( rotating ) delay (1);
if( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter - 1 if B leads A
if( B_set && !A_set )
encoderPos -= 1;
rotating = false;
}
}