AgTalk Home
AgTalk Home
Search Forums | Classifieds | Skins | Language
You are logged in as a guest. ( logon | register )

Anybody here messing with Arduino stuff?
View previous thread :: View next thread
   Forums List -> Computer TalkMessage format
 
btruck
Posted 1/16/2018 12:40 (#6509635 - in reply to #6509500)
Subject: RE: Anybody here messing with Arduino stuff?



MN
Here's the code I wrote. I am NOT a programmer. I'm sure there are more efficient ways to do this, better, etc.

There are two toggles, one for for/rev, the other for locking the trans in a particular gear. There are 3 led's for for/neu/rev, and led's for gears 1-5.
If someone leaves the tractor in for or rev it just leaves the tractor in neutral and flashes all the led's.

It might be easier to paste it back into the arduino editor to read it.

const int shiftuppin = 3; //the number of the shift up pin
const int shiftdownpin = 2; //the number of the shift down pin
const int forpin = 4; //number of forward range pin
const int revpin =5; //number of rev pin

const int ledpins[] = {10,9,8,7,6}; //the leds for gears 1-5
const int lencnt =5; // total length of led arrays

const int forledpin = 13; //pin for forward led
const int revledpin = 12; //pin for reverse led
const int neutledpin = 11; //pin for reverse led

long debouncedelay = 100; //delay for debouncing


int shiftposition =0; // sets the shiftposition to 0 initially
int shiftupstate = 0;
int shiftdownstate = 0;

int lastshiftupstate = 0;
int lastshiftdownstate = 0;
int actuatorposition[]={25,50,75,100,125};
int neutralposition =10;
int revposition = 0;
int forrngstate=0;
int revrngstate=0;
int lastforrngstate=0;
int lastrevrngstate=0;


void setup(){
Serial.begin(9600);
pinMode(shiftuppin, INPUT);
pinMode(shiftdownpin, INPUT);
pinMode(forpin, INPUT);
pinMode(revpin, INPUT);

//int actuatorposition[]={0, 60, 120};

for (int p=0; p ){
pinMode(ledpins[p], OUTPUT);
digitalWrite(ledpins[p],LOW);} //sets 5-1 led pins to output

pinMode(forledpin, OUTPUT);
pinMode(revledpin, OUTPUT);
pinMode(neutledpin, OUTPUT);
digitalWrite(forledpin,LOW);
digitalWrite(revledpin,LOW);

Serial.println("on");

Serial.print(0xC0, BYTE);
Serial.print(neutralposition, BYTE);

while((digitalRead(forpin) ==HIGH) || (digitalRead(revpin) ==HIGH)){ //error loop for not in neutral

digitalWrite(revledpin,HIGH);
delay(10);
digitalWrite(neutledpin,HIGH);
delay(10);
for (int p=0; p ){
//pinMode(ledpins[p], OUTPUT);
delay(10);
digitalWrite(ledpins[p],HIGH);} //sets 5-1 led pins to output
delay(10);


digitalWrite(revledpin,LOW);
delay(10);
digitalWrite(neutledpin,LOW);
delay(10);
for (int p=0; p ){
//pinMode(ledpins[p], OUTPUT);
delay(10);
digitalWrite(ledpins[p],LOW);} //sets 5-1 led pins to output
delay(10);

Serial.print(0xC0, BYTE);
Serial.print(neutralposition, BYTE); // end of forward range programming
}

}

void loop(){


forrngstate =digitalRead(forpin);
revrngstate =digitalRead(revpin);

if ((forrngstate==HIGH) && (lastforrngstate==forrngstate)){
digitalWrite(revledpin, LOW);
//digitalWrite(ledpins, LOW);
digitalWrite(neutledpin, LOW);
delay(debouncedelay);
while (digitalRead(forpin)){

shiftupstate =digitalRead(shiftuppin); //start of forward range programming
shiftdownstate = digitalRead(shiftdownpin);

if(shiftupstate > lastshiftupstate){
delay(debouncedelay);
if(shiftupstate > lastshiftupstate){
if (shiftposition != (lencnt-1)){
shiftposition++;}
}
}
if(shiftdownstate > lastshiftdownstate){
delay(debouncedelay);
if(shiftdownstate > lastshiftdownstate){
if(shiftposition !=0){
shiftposition--;}
}
}

for (int p=0; p ){
//pinMode(ledpins[p], OUTPUT);
digitalWrite(ledpins[p],LOW);}

digitalWrite(ledpins[shiftposition],HIGH); //lights the led corresponding to the requested shift position
Serial.println(shiftposition); // prints the position to the serial monitor

lastshiftupstate = shiftupstate;
lastshiftdownstate=shiftdownstate;

Serial.print(0xC0, BYTE);
Serial.print(actuatorposition[shiftposition], BYTE); // end of forward range programming
}
}

if ((revrngstate==HIGH) && (lastrevrngstate==revrngstate)){ // reverse range programming
digitalWrite(revledpin, HIGH);
//digitalWrite(ledpins, LOW);
digitalWrite(neutledpin, LOW);
for (int p=0; p ){
pinMode(ledpins[p], OUTPUT);
digitalWrite(ledpins[p],LOW);} //sets 5-1 led pins to output

Serial.print(0xC0, BYTE);
Serial.print(actuatorposition[revposition], BYTE);
}

if (revrngstate==LOW && forrngstate==LOW){ //neutral range programming
digitalWrite(revledpin, LOW);
//digitalWrite(ledpins, LOW);
digitalWrite(neutledpin, HIGH);
for (int p=0; p ){
pinMode(ledpins[p], OUTPUT);
digitalWrite(ledpins[p],LOW);} //sets 5-1 led pins to output
Serial.print(0xC0, BYTE);
Serial.print(actuatorposition[neutralposition], BYTE);

}

lastforrngstate = forrngstate;
lastrevrngstate = revrngstate;

}
Top of the page Bottom of the page


Jump to forum :
Search this forum
Printer friendly version
E-mail a link to this thread

(Delete cookies)