Go Down

Topic: noob at C code, help with 6 inputs and 6 outputs (Read 128 times) previous topic - next topic

turbothis

Sep 11, 2017, 10:01 pm Last Edit: Sep 11, 2017, 11:22 pm by turbothis
hi, new to figuring this code stuff out
not sure if this is in the correct forum
i can "see" what is happening and can adjust info like parameters but dont know how to assemble it all

my project is a 6 speed auto transmission with 6 solenoid control
i got an uno (unfortunately china copy it looks like) that i want to input 6 micro switchs (5V) for the gear selection

output will be on/off to a mosfets on a board
6 total again

the out puts will have certain pairing of solenoids for gear selection

i think i am heading down the right road with my copy and pasting of some C code pieces
i dont know how to link the input pin to a combo of output pins

i need these inputs to activate these outputs
sensorpin A0 =  solpin 3 and 9
sensorpin A1 =  solpin 3 and 6
sensorpin A2 =  solpin 3 and 5
sensorpin A3 =  solpin 3 and 9 and 10
sensorpin A4 =  solpin 5 and 9 and 10
sensorpin A5 =  solpin 6 and 9 and 10

not sure if i need to scrap my start text file and start another
i am pretty sure there is several ways to achieve this function but i have no real experience so i am open to any help and idea on the simple path on this
thanks matt

turbothis

well i have learned a lot in a day. lol
i like the "button" command or whatever you call it for the input
the digitalwrite would be good 5V into my mosfet board

check it out....

Code: [Select]
#include <Button.h>

Button button1 = Button(2);  //1st gear
Button button2 = Button(3);
Button button3 = Button(4);
Button button4 = Button(5);
Button button5 = Button(6);
Button button6 = Button(7);  //6th gear



void setup(){
 pinMode(8,OUTPUT);   //solenoid 1
 pinMode(9,OUTPUT);
 pinMode(10,OUTPUT);
 pinMode(11,OUTPUT);
 pinMode(12,OUTPUT);   //solenoid 5
 
}

void loop(){ //not sure if this is latching/holding on even after the button is released?  i found a thing "uniquePress" inplace of "isPressed"
 if(button1.isPressed()){
     digitalWrite(8,HIGH);
     digitalWrite(11,HIGH);
 }
 if(button2.isPressed()){
       digitalWrite(8,HIGH);
       digitalWrite(10,HIGH);
 }
 if(button3.isPressed()){
     digitalWrite(8,HIGH);
     digitalWrite(9,HIGH);
 }
 if(button4.isPressed()){
       digitalWrite(8,HIGH);
       digitalWrite(11,HIGH);
       digitalWrite(12,HIGH);
 }
 if(button5.isPressed()){
     digitalWrite(9,HIGH);
     digitalWrite(11,HIGH);
     digitalWrite(12,HIGH);
 }
 if(button6.isPressed()){
       digitalWrite(10,HIGH);
       digitalWrite(11,HIGH);
       digitalWrite(12,HIGH);
 }
 else{
     digitalWrite(11,HIGH);     //this would be park/reverse
 }
}

Grumpy_Mike

#2
Sep 13, 2017, 06:06 am Last Edit: Sep 13, 2017, 06:12 am by Grumpy_Mike
It is a good idea to make variable names meaningful so instead of :-
Code: [Select]

Button button1 = Button(2);  //1st gear
Button button2 = Button(3);
Button button3 = Button(4);
Button button4 = Button(5);
Button button5 = Button(6);
Button button6 = Button(7);  //6th gear

Why not use:-
Code: [Select]

Button gear1 = Button(2);  //1st gear
Button gear2 = Button(3);
Button gear3 = Button(4);
Button gear4 = Button(5);
Button gear5 = Button(6);
Button gear6 = Button(7);  //6th gear


So you would now use code like this:
Code: [Select]

if(gear1.isPressed()){
     digitalWrite(8,HIGH);
     digitalWrite(11,HIGH);
 }

Quote
//not sure if this is latching/holding on even after the button is released?
Yes it is. When the button is being pressed the code in the if statement gets repeatedly run setting your two outputs high, which is fine. When you release it that code is no longer run and in the absence of anything else being pressed nothing will change. However when you press another button the two previous outputs will stay high and two new ones will go high. So instead of just setting the outputs high in the if statements you must also set the others low.

like this:-

Code: [Select]

if(gear1.isPressed()){
     digitalWrite(8,HIGH);
     digitalWrite(11,HIGH);
     digitalWrite(9,LOW);
     digitalWrite(10,LOW);
     digitalWrite(12,LOW);
 }


Quote
not sure if this is in the correct forum
It is not. Much better in Programming or Project Guide


turbothis

outstanding!
that all makes sense
thanks for the reply
i will update the code and post in the other forum for future help

Go Up