got compile error with SoftwareSerial.h
hi! can please me code? attach copy below.
i got these error message
i'm using arduino due, ide 1.5.7
any appreciated. thanks.
code: [select]
#include <softwareserial.h>
// define pins used softwareserial communication
#define rxpin 2
#define txpin 3
// set new softwareserial port, named "myserial" or whatever want call it.
softwareserial myserial = softwareserial(rxpin, txpin);
// name analog input pins
int gyro_pin = 1; // connect gyro x axis (4x output) analog input 1
int accel_pin = 5; // connect accelerometer x axis analog input 5
int steeringpot = 3; // connect steering potentiometer analog input 3
int gainpot = 2; // connect gain potentiometer analog input 2
// name digital i/o pins
int engage_switch = 4; // connect engage button digital pin 4
int ledpin = 13;
// value hold final angle
float angle = 0.00;
// following 2 values should add equal 1.0
float gyro_weight = 0.98;
float accel_weight = 0.02;
// accelerometer values
int accel_reading;
int accel_raw;
int accel_offset = 511;
float accel_angle;
float accel_scale = 0.01;
// gyroscope values
int gyro_offset = 391;
int gyro_raw;
int gyro_reading;
float gyro_rate;
float gyro_scale = 0.025; // 0.01 default
float gyro_angle;
float loop_time = 0.05;
// engage button variables
int engage = false;
int engage_state = 1;
// timer variables
int last_update;
int cycle_time;
long last_cycle = 0;
// motor speed variables
int motor_out = 0;
int motor_1_out = 0;
int motor_2_out = 0;
int m1_speed = 0;
int m2_speed = 0;
int output;
// potentiometer variables
int steer_val;
int steer_range = 7;
int steer_reading;
int gain_reading;
int gain_val;
// end of variables
void setup(){
// start serial monitor @ 9600bps
serial.begin(9600);
// define pinmodes tx , rx:
pinmode(rxpin, input);
pinmode(txpin, output);
// set data rate softwareserial port
myserial.begin(9600);
// set engage_switch pin input
pinmode(engage_switch, input);
// enable arduino internal pull-up resistor on engage_switch pin.
digitalwrite(engage_switch, high);
// tell arduino use aref pin analog voltage, don't forget connect 3.3v to
aref!
analogreference(external);
}
void loop(){
// start loop getting reading accelerometer , converting angle
sample_accel();
// read gyroscope estimate angle change
sample_gyro();
// combine accel , gyro readings come "filtered" angle reading
calculate_angle();
// read values of each potentiometer
read_pots();
// make sure bot level before activating motors
auto_level();
// update motors new values
update_motor_speed();
// check loop cycle time , add delay necessary
time_stamp();
// debug serial monitor
serial_print_stuff();
}
void sample_accel(){
// read , convert accelerometer value
accel_reading = analogread(accel_pin);
accel_raw = accel_reading - accel_offset;
accel_raw = constrain(accel_raw, -90, 90);
accel_angle = (float)(accel_raw * accel_scale);
}
void sample_gyro(){
// read , convert gyro value
gyro_reading = analogread(gyro_pin);
gyro_raw = gyro_reading - gyro_offset;
gyro_raw = constrain(gyro_raw, -391, 391);
gyro_rate = (float)(gyro_raw * gyro_scale) * -loop_time;
gyro_angle = angle + gyro_rate;
}
void calculate_angle(){
angle = (float)(gyro_weight * gyro_angle) + (accel_weight * accel_angle);
}
void read_pots(){
// read , convert potentiometer values
// steering potentiometer
steer_reading = analogread(steeringpot); // want map range between -1 and
1, , set steer_val
steer_val = map(steer_reading, 0, 1023, steer_range, -steer_range);
if (angle == 0.00){
gain_reading = 0;
}
// gain potentiometer
gain_reading = analogread(gainpot);
gain_val = map(gain_reading, 0, 1023, 32, 64);
}
void auto_level(){
// enable auto-level turn on
engage_state = digitalread(engage_switch);
if (engage_state == 1){
engage = false;
}
else {
if (engage == false){
if (angle < 0.02 && angle > -0.02)
engage = true;
else {
engage = false;
}
}
else {
engage = true;
}
}
}
void update_motor_speed(){
// update motors
if (engage == true){
if (angle < -0.4 || angle > 0.4){
motor_out = 0;
}
else {
output = (angle * -1000); // convert float angle integer format
motor_out = map(output, -250, 250, -gain_val, gain_val); // map angle
}
// assign steering bias
motor_1_out = motor_out + (steer_val);
motor_2_out = motor_out - (steer_val);
// test , correct invalid values
if(motor_1_out > 64){
motor_1_out = 64;
}
if(motor_1_out < -64){
motor_1_out = -64;
}
if(motor_2_out > 64){
motor_2_out = 64;
}
if(motor_2_out < -64){
motor_2_out = -64;
}
// assign final motor output values
m1_speed = 64 + motor_1_out;
m2_speed = 192 + motor_2_out;
}
else{
m1_speed = 0;
m2_speed = 0;
}
// write final output values sabertooth via softwareserial
myserial.print(m1_speed, byte);
myserial.print(m2_speed, byte);
}
void time_stamp(){
// check make sure has been 50 milliseconds since last recorded time-stamp
while((millis() - last_cycle) < 50){
delay(1);
}
// once loop cycle reaches 50 ms, reset timer value , proceed
cycle_time = millis() - last_cycle;
last_cycle = millis();
}
void serial_print_stuff(){
// debug serial monitor
serial.print("accel: ");
serial.print(accel_angle); // print accelerometer angle
serial.print(" ");
serial.print("gyro: ");
serial.print(gyro_angle); // print gyro angle
serial.print(" ");
serial.print("filtered: ");
serial.print(angle); // print filtered angle
serial.print(" ");
serial.print(" time: ");
serial.print(cycle_time); // print loop cycle time
serial.println(" ");
}
i got these error message
code: [select]
fatal error: softwareserial.h: no such file or directory
i'm using arduino due, ide 1.5.7
any appreciated. thanks.
the due has 4 hardware serial ports. why using softwareserial @ all?
Arduino Forum > Using Arduino > Programming Questions > got compile error with SoftwareSerial.h
arduino
Comments
Post a Comment