cattledog:
Where did you find that?
That was a guess on my part. Libraries are new to me. In fact C is new to me. I know I'm dating myself, but my first programming language was Fortran. I learned Basic on my own.
Anyway, that's my excuse and I'm sticking to it.
So, Okay, now I know where the code for the US sensor is, thanks. The IRremote library I use is Ken Shirriff's. It does use interrupts. I tried disabling the interrupts, but could not get the IR to function then. In the code, the IR sensor is started in the SETUP before the LOOP. Killing the Interrupts before the US sensor code runs gives me accurate distance readings, but kills the IR control.
Ultimately, this is a learning experience. I'll likely have to use something other than an IR remote. Also, so long as the error is consistent, I can live with it while I figure out how to use the data for autonomous operation. But I do like to understand what is happening.
My Code:
/*
IR Control of Dagu 5 , 4 motor robot chasis using an IR remote
Code snippets stolen from various sources and edited/rewritten by Barb. Fall 2015
IR Receiver Breakout (SEN-8554): Supply voltage of 2.5V to 5.5V
Attach
OUT: To pin A0 on Arduino
GND: GND
VCC: 3.3 V
This uses Ken Shirriff's code found on GitHub:
https://github.com/shirriff/Arduino-IRremote/
Base IR Robot code:IR_robot_V3b
Setup to gather test data for US range finder performance
evaluation.
V4_TUS_V1:
Timing 2.5 second scan rate
US Scan code:
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
Modified by Barb November 2015
Development code for Dagu Rover 5 IR/autonomous project
Serial output formatted to CSV format
Version 1b - Use Ultrasonic code without Library
Movement controlled maunually
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin A5
Trig to Arduino pin A4
*/
int Distance;
#define echoPin A5 // Echo Pin
#define trigPin A4 // Trigger Pin
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
//IR setup
#include <IRremote.h>
int RECV_PIN = A0;
IRrecv irrecv(RECV_PIN);
decode_results results;
uint16_t lastCode = 0; // This keeps track of the last code RX'd
//Movement cotrol variable setup
int SPEED = 200; //Starting speed value for forward or reverse
int STOPTest = 0; //Used to check last motor status
int LASTSpeed; //Unused
//Low battery test setup
const float LOW_BATTERY_VOLTAGE = 5.8;
const byte BATTERY_CHECK = 2; // A2
const byte RED_LED = 11; // warning LED
//IR Remote button codes
#define POWER 0x10EFD827 //No Function
#define A 0x10EFF807 //Slower
#define B 0x10EF7887 //Reset default speed
#define C 0x10EF58A7 //Faster
#define UP 0x10EFA05F //Move forward
#define DOWN 0x10EF00FF //Move backward
#define LEFT 0x10EF10EF //Turn left
#define RIGHT 0x10EF807F //Turn right
#define SELECT 0x10EF20DF //Stop
//Set up the motor outputs
#define DIRECTIONA 4 //LF Channel 1
#define MOTORA 5
#define DIRECTIONC 7 //LR Channel 2
#define MOTORC 6
#define DIRECTIONB 8 //RF Channel 3
#define MOTORB 9
#define DIRECTIOND 12 //RR Channel 4
#define MOTORD 10
void setup()
{
//Initialize motor control output pins
pinMode (MOTORA, OUTPUT);
pinMode (DIRECTIONA, OUTPUT);
pinMode (MOTORB, OUTPUT);
pinMode (DIRECTIONB, OUTPUT);
pinMode (MOTORC, OUTPUT);
pinMode (DIRECTIONC, OUTPUT);
pinMode (MOTORD, OUTPUT);
pinMode (DIRECTIOND, OUTPUT);
Serial.begin(9600); //for trooubleshooting
irrecv.enableIRIn(); // Start the IR receiver
pinMode (RED_LED, OUTPUT); // low battery warning LED
pinMode(trigPin, OUTPUT); // US Sensor
pinMode(echoPin, INPUT); // US Sensor
}
void MotorControl ()
{
analogWrite (MOTORA, SPEED);
analogWrite (MOTORB, SPEED);
analogWrite (MOTORC, SPEED);
analogWrite (MOTORD, SPEED);
return;
}
void loop()
{
//Ultrasonic Range Finder
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
//noInterrupts();
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
if (distance >= maximumRange || distance <= minimumRange){
// Send a negative number to computer
Serial.println("-1");
}
else {
// Send the distance to the computer using Serial protocol, and
Serial.println(distance);
}
//Delay 50ms before next reading.
delay(50);
//interrupts();
//Low Voltage test routine
int voltage = analogRead (BATTERY_CHECK);
// 1024 would be 5V on the pin
// we have a voltage divider so the read voltage is half the battery
float volts = voltage / 1024.0 * 5.0 * 2.0;
if (volts < LOW_BATTERY_VOLTAGE)
{
SPEED = 0;
STOPTest = 0;
MotorControl();
digitalWrite (RED_LED, HIGH); // flash LED as warning
delay (50);
digitalWrite (RED_LED, LOW);
delay (950);
return; // don't try to use motors now
}
//IR receive routine
if (irrecv.decode(&results))
{
/* read the RX'd IR into a 16-bit variable: */
uint16_t resultCode = (results.value & 0xFFFF);
/* The remote will continue to spit out 0xFFFFFFFF if a
button is held down. If we get 0xFFFFFFF, let's just
assume the previously pressed button is being held down */
if (resultCode == 0xFFFF)
resultCode = lastCode;
else
lastCode = resultCode;
//COntrol sequence
switch (resultCode)
{
case POWER:
break;
case A: //Slower
if (STOPTest == 1)
if (SPEED > 90){
SPEED = SPEED - 10;
MotorControl();}
break;
case B:
SPEED = 200;
break;
case C: //Faster
if (STOPTest == 1)
if (SPEED < 250){
SPEED = SPEED + 10;
MotorControl();}
break;
case UP:
if (STOPTest == 0) SPEED = 200;
STOPTest = 1;
digitalWrite (DIRECTIONA, 0);
digitalWrite (DIRECTIONB, 0);
digitalWrite (DIRECTIONC, 0);
digitalWrite (DIRECTIOND, 0);
MotorControl();
break;
case DOWN:
if (STOPTest == 0) SPEED = 200;
STOPTest = 1;
digitalWrite (DIRECTIONA, 1);
digitalWrite (DIRECTIONB, 1);
digitalWrite (DIRECTIONC, 1);
digitalWrite (DIRECTIOND, 1);
MotorControl();
break;
case LEFT:
if (STOPTest == 0){
digitalWrite (DIRECTIONA, 1);
digitalWrite (DIRECTIONB, 0);
digitalWrite (DIRECTIONC, 1);
digitalWrite (DIRECTIOND, 0);
analogWrite (MOTORA, 150);
analogWrite (MOTORB, 150);
analogWrite (MOTORC, 150);
analogWrite (MOTORD, 150);
}
else{
analogWrite (MOTORA, 0);
analogWrite (MOTORB, SPEED);
analogWrite (MOTORC, 0);
analogWrite (MOTORD, SPEED);
}
break;
case RIGHT:
if (STOPTest == 0){
digitalWrite (DIRECTIONA, 0);
digitalWrite (DIRECTIONB, 1);
digitalWrite (DIRECTIONC, 0);
digitalWrite (DIRECTIOND, 1);
analogWrite (MOTORA, 150);
analogWrite (MOTORB, 150);
analogWrite (MOTORC, 150);
analogWrite (MOTORD, 150);
}
else{
analogWrite (MOTORA, SPEED);
analogWrite (MOTORB, 0);
analogWrite (MOTORC, SPEED);
analogWrite (MOTORD, 0);
}
break;
case SELECT:
STOPTest = 0;
SPEED = 0;
MotorControl();
break;
default:
break;
}
irrecv.resume();
}
}
Barb