Hi all,
I am making a firefighter-racecar to learn the program basics.
3 ardiuno's 3 xbee's joystickschield and the big easy driver and polulu motor driver etcetera...
I ran into a problem with giving pulses to the easydriver. My code is receiving data from serial port and adds it to variables so i have a simple remote control.
Almost Everything is working right now, sending pwm and direction to the DC motor driver, steering with the racecar servo.
All remote with the joystick schield, ardiuno and xbee...
The code that doenst work is the code for giving pulses to the easydriver to step...
I think it can not use a DELAY or my code delays because the stepper motor turns really slow...
RECEIVER
/////////////////INCULDE////////////////
#include <Servo.h>
/////////////////XBEE///////////////////
boolean checkBegin = false;
byte incomingByte;
int buf[6];
int i = 0;
int r = 0;
int gas;
int stuur;
int btn_1;
int btn_2;
int btn_3;
int btn_4;
/////////////////GAS////////////////////
int gasDir = 5; // GASGEVEN DIRECTION
int gasOut = 6; // GASGEVEN MOTOR
int gasstop = 0; // GASGEVEN PAUZE
int gasvoor = 0; // GASGEVEN VOOR
int gasachter = 0; // GASGEVEN ACHTER
/////////////////STUUR//////////////////
Servo myservo;
/////////////////STEPPER////////////////
//OUTPUTS
int STEP = 11; // PIN 2 = STEP
int STEPDIR = 10; // PIN 3 = DIR
//int SLEEP = 7; // PIN 7 = SLP
//int MS1 = ?; // PIN = MS1
//int MS2 = ?; // PIN = MS2
//INPUTS
int RANGE = A6;
//SOFTWARE
//int SWEEP_LEFT= 0;
//int SWEEP_RIGHT= 0;
/////////////////SETUP//////////////////
void setup(){
Serial.begin(19200); // serialsnelheid
//GAS
pinMode(gasDir, OUTPUT);
pinMode(gasOut, OUTPUT);
pinMode(13, OUTPUT);
//STUUR
myservo.attach(9); // attaches the servo on pin 9
myservo.write(90);
//STEPPER INPUT
//pinMode(RANGE, INPUT);
//analogRead(RANGE);
//STEPPER OUTPUT
pinMode(STEPDIR, OUTPUT); // set pin 3 to output
pinMode(STEP, OUTPUT); // set pin 2 to output
//pinMode(SLEEP, OUTPUT); // set pin 12 to output
//digitalWrite(SLEEP, HIGH);
}
/////////////////BEGIN//////////////////
void loop () {
if (Serial.available() > 0) {
digitalWrite(13, LOW);
r=0;
incomingByte = Serial.read();
if(checkBegin == true){
buf[i] = int(incomingByte);
i++;
if(i==6){
checkBegin = false;
processPackage();
}
// BUFFER VULLEN
}else{
// BEGIN BUFFER CHECK
i = 0;
if(int(incomingByte) == 255){
checkBegin = true;
}
}
}
// NIET BESCHIKBAAR RESET
else if((Serial.available() == 0) && (r < 32000)){
r++;
}
else {
gasstop = 0;
analogWrite (gasOut, gasstop);
digitalWrite(13, HIGH); // set the LED on
// int stuur = 60;
//btn_1 = HIGH;
//btn_2 = HIGH;
//btn_3 = HIGH;
//btn_4 = HIGH;
}
} // einde loop
/////////////////BEGIN//////////////////
void processPackage(){
//VULVARS
gas = buf[0];
stuur = buf[1];
btn_1 = buf[2];
btn_2 = buf[3];
btn_3 = buf[4];
btn_4 = buf[5];
//MIDDEN
if (gas > 125 && gas < 135)
{
gasstop = 0;
analogWrite (gasOut, gasstop);
}
//LAAG
else if (gas < 125)
{
gasachter = map (gas, 125, 0, 0, 200);
digitalWrite(gasDir, HIGH); //Reverse motor direction, 1 high, 2
analogWrite (gasOut, gasachter);
}
//HOOG
else if (gas > 135)
{
gasvoor = map (gas, 135, 250, 0, 200);
digitalWrite(gasDir, LOW); //Reverse motor direction, 1 high, 2
analogWrite (gasOut, gasvoor);
}
/////////////////STUUR//////////////////
stuur = map(stuur, 0, 179, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(stuur); // sets the servo position according to the scaled value
/////////////////STEPPER////////////////
// STEPPER BUTTONS
if (btn_1 == LOW) {
gotoRight();
digitalWrite(STEP, LOW);
delayMicroseconds(100);
digitalWrite(STEP, HIGH);
delayMicroseconds(100);
}
if (btn_4 == LOW) {
gotoLeft();
digitalWrite(STEP, LOW);
delayMicroseconds(100);
digitalWrite(STEP, HIGH);
delayMicroseconds(100);
}
if (btn_2 == LOW) {
//
}
if (btn_3 == LOW) {
//
}
}// EINDE PROCES PACKAGE
// STEPPER FUNCTIONS
void gotoLeft(){
digitalWrite(STEPDIR, LOW);
//delay(5);
}
void gotoRight(){
digitalWrite(STEPDIR, HIGH);
// delay(5);
}
SENDER
byte buf[7]; // an array of pin numbers to which LEDs are attached
// byte buf2[] = {1,2,3,4,5,6};
int len = 7; // the number of pins (i.e. the length of the array)
int analogPin1 = 0; int analogPin0 = 1; int analogPin2 = 2; int analogPin3 = 3;
int digitalPin3 = 3; int digitalPin4 = 4; int digitalPin5 = 5; int digitalPin6 = 6;
int val0 = 0; int val1 = 0; // variable to store the value read ana
int val2 = 0; int val3 = 0; int val4 = 0; int val5 = 0; // variable to store the value read dig
int sensorValue1 = 0;
int sensorValue2 = 0;
void setup(){
Serial.begin(19200); // setup serial
pinMode(digitalPin3, INPUT); // sets the digital pin 3 as input
digitalWrite(digitalPin3, HIGH); // sets the digital pin 3 high
pinMode(digitalPin4, INPUT); // sets the digital pin 4 as input
digitalWrite(digitalPin4, HIGH); // sets the digital pin 4 high
pinMode(digitalPin5, INPUT); // sets the digital pin 5 as input
digitalWrite(digitalPin5, HIGH); // sets the digital pin 5 high
pinMode(digitalPin6, INPUT); // sets the digital pin 6 as input
digitalWrite(digitalPin6, HIGH); // sets the digital pin 6 high
}
void loop(){
sensorValue1 = analogRead(analogPin0); //gas
sensorValue2 = analogRead(analogPin3); //stuur
//val0 = analogRead(A0); // read the input pin
//val1 = analogRead(A1); // read the input pin
val0 = map (sensorValue1, 0, 1023, 0, 250);
val1 = map (sensorValue2, 0, 1023, 0, 180);
val2 = digitalRead(3); // read the input pin
val3 = digitalRead(4); // read the input pin
val4 = digitalRead(5); // read the input pin
val5 = digitalRead(6); // read the input pin
buf[0] = byte (255); // check voor begin an array
buf[1] = byte (val0);
buf[2] = byte (val1);
buf[3] = byte (val2);
buf[4] = byte (val3);
buf[5] = byte (val4);
buf[6] = byte (val5);
Serial.write(buf, len);
//Serial.write(buf2, len);
//Serial.print(val0); // debug value
//Serial.print(val1); // debug value
//Serial.print(val2); // debug value
//Serial.print(val3); // debug value
//Serial.print(val4); // debug value
//Serial.println(val5); // debug value
delay(100);
}
//void analog1(){}
//void analog2(){}


