Hello!
I'm having trouble using both PWM output, analogRead, and Serial.print functions and could really use some advice.
I'm working on a project that will require reading four analog inputs, mapping and comparing the inputs, driving an H-bridge IC with PWM, and also sending out I2C to another IC.
Currently, I'm reading the four analog inputs, mapping them, and driving the H-bridge IC. I would REALLY like to monitor the analog inputs on my computer to see what is going on, but whenever I try to use a Serial.print command the motor's performance becomes extremely jerky and erratic.
I understand that I'm asking the Arudino to do a lot of things simultaneously, but I really need to take a look at what the Arduino is doing in real-time..
Here's the code without any serial commands:
#include <EEPROM.h>
int TPS1;
int TPS2;
int TPS1min = EEPROM.read(0);
int TPS1max = EEPROM.read(1)+765;
int TPS2min = EEPROM.read(2)+765;
int TPS2max = EEPROM.read(3);
int PPS1;
int PPS2;
int PPS1min = 136;
int PPS1max = 966;
int PPS2min = 59;
int PPS2max = 460;
int a;
int b;
void setup()
{
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A4,INPUT);
pinMode(A5,INPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,INPUT);
pinMode(8,INPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
//TCCR0B = TCCR0B & 0b11111000 | 0x02;
}
//
void loop()
{
PPS1 = map(analogRead(A0),PPS1min,PPS1max,0,1000);
PPS2 = map(analogRead(A1),PPS2min,PPS2max,0,1000);
TPS1 = map(analogRead(A4),TPS1min,TPS1max,0,1000);
TPS2 = map(analogRead(A5),TPS2min,TPS2max,0,1000);
PPS1 = constrain(PPS1,0,1000);
a = PPS1 - TPS1;
b = TPS1 - PPS1;
//
if ( PPS1 >= 970 ) //***WOT SWITCH***
{
digitalWrite(5,LOW);
digitalWrite(6,LOW);
}
if ( PPS1 <= 10 ) //***FULLY CLOSED SWITCH***
{
if ( TPS1 > 15 )
{
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}
else
{
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
}
}
else
{
if ( PPS1 > TPS1 ) //SOFT UP TO 2 DEGREES DIFFERENCE
{
analogWrite(5,constrain(255/a,0,255));
digitalWrite(6,LOW);
}
else //SOFT UP TO 3 DEGREES DIFFERENCE
{
if ( b <= 30 )
{
analogWrite(5,constrain(b*8.5,0,255));
digitalWrite(6,LOW);
}
else
{
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}
}
}
}
... and here's the code with the serial commands...
#include <EEPROM.h>
int TPS1;
int TPS2;
int TPS1min = EEPROM.read(0);
int TPS1max = EEPROM.read(1)+765;
int TPS2min = EEPROM.read(2)+765;
int TPS2max = EEPROM.read(3);
int PPS1;
int PPS2;
int PPS1min = 136;
int PPS1max = 966;
int PPS2min = 59;
int PPS2max = 460;
int a;
int b;
void setup()
{
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A4,INPUT);
pinMode(A5,INPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,INPUT);
pinMode(8,INPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
Serial.begin(9600);
//TCCR0B = TCCR0B & 0b11111000 | 0x02;
}
//
void loop()
{
PPS1 = map(analogRead(A0),PPS1min,PPS1max,0,1000);
PPS2 = map(analogRead(A1),PPS2min,PPS2max,0,1000);
TPS1 = map(analogRead(A4),TPS1min,TPS1max,0,1000);
TPS2 = map(analogRead(A5),TPS2min,TPS2max,0,1000);
PPS1 = constrain(PPS1,0,1000);
a = PPS1 - TPS1;
b = TPS1 - PPS1;
Serial.print("PPS1: ");
Serial.print(PPS1);
Serial.print("PPS2: ");
Serial.print(PPS2);
Serial.print("TPS1: ");
Serial.print(TPS1);
Serial.print("TPS2: ");
Serial.print(TPS2);
//
if ( PPS1 >= 970 ) //***WOT SWITCH***
{
digitalWrite(5,LOW);
digitalWrite(6,LOW);
}
if ( PPS1 <= 10 ) //***FULLY CLOSED SWITCH***
{
if ( TPS1 > 15 )
{
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}
else
{
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
}
}
else
{
if ( PPS1 > TPS1 ) //SOFT UP TO 2 DEGREES DIFFERENCE
{
analogWrite(5,constrain(255/a,0,255));
digitalWrite(6,LOW);
}
else //SOFT UP TO 3 DEGREES DIFFERENCE
{
if ( b <= 30 )
{
analogWrite(5,constrain(b*8.5,0,255));
digitalWrite(6,LOW);
}
else
{
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}
}
}
}