Croatia
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« on: January 25, 2013, 06:37:36 pm » |
Hi! I'm doing this gear indicator for my motorcyle, but I want to do it using the Arduino. http://www.electronics-lab.com/projects/automotive/006/index.htmlThis is my code: int topGear=6;
int upSensor=3; int downSensor=4; int nSensor=2;
//These are outputs to 4026 int clk=1; int reset=0;
//This is the current gear int gear=0;
void setup() { pinMode(upSensor, INPUT); pinMode(downSensor, INPUT); pinMode(nSensor, INPUT); pinMode(clk, OUTPUT); pinMode(reset, OUTPUT); } int GearUP=0; int GearDOWN=0;
void loop() { delay(50); GearUP=digitalRead(upSensor); GearDOWN=digitalRead(downSensor); if(GearUP==LOW) { if(gear < topGear) { gear++; } else { gear=0; } delay(250); } delay(50); if(GearDOWN=LOW) { if(gear>0) { gear--; } } ShowDigit(gear); } void ShowDigit(int digit) { if(gear < topGear) { digitalWrite(reset,HIGH); for(int i=0;i<digit;i++) { digitalWrite(clk, HIGH); delay(100); digitalWrite(clk,LOW); delay(100); } } else { digitalWrite(reset,HIGH); } }
I think that is problem in my reading of Hall effect sensor state, I'm using Attiny85 and A3144 Hall sensors. I tried basic example of blinking led every second, and everything work's fine, so it isn't problem in micro controller. Thanks!
|
|
|
|
« Last Edit: January 26, 2013, 06:08:16 am by jocoj100 »
|
Logged
|
|
|
|
|
Manchester (England England)
Online
Brattain Member
Karma: 269
Posts: 25395
Solder is electric glue
|
 |
« Reply #1 on: January 25, 2013, 06:41:08 pm » |
As you have not said what problems you are having with that code is is hard to say what is wrong with it.
|
|
|
|
|
Logged
|
|
|
|
|
Croatia
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #2 on: January 25, 2013, 07:00:08 pm » |
Simply, something doesn't work, and I can't realize where is problem. Is my logic of getting state of the hall sensors well written?
|
|
|
|
|
Logged
|
|
|
|
|
Milton Keynes UK
Offline
Tesla Member
Karma: 88
Posts: 6287
-
|
 |
« Reply #3 on: January 25, 2013, 07:21:36 pm » |
Write a sketch that does nothing but read from one of the sensors and print the value it gets. Does it display the correct values? If so, do the same for the other sensor. Don't bother worrying about what else might be happening in the sketch until you know that it's reading the sensors correctly.
|
|
|
|
|
Logged
|
|
|
|
|
Croatia
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #4 on: January 25, 2013, 07:32:04 pm » |
Problem is that I have Tiny85, on my breadboard and I'm programming it using USBAsp, and I don't know can I establish serial communication between computer and arduino.
But when I connect this hall sensor to 1 pin to 5V, 2 pin to GND, and 3 pin to one probe of my multimeter, and other probe I connect to GND. On my meter it shows about 3 V, and when I put the magnet it shows about 1mV.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Online
Brattain Member
Karma: 269
Posts: 25395
Solder is electric glue
|
 |
« Reply #5 on: January 26, 2013, 03:28:10 am » |
It would help if you post all the code that you are using. That code has undefined variables in it and will not compile. I would not expect it to work anyway because a loop executies quickly so I would expect it to zip up or down almost immediately to the limit.
|
|
|
|
|
Logged
|
|
|
|
|
Croatia
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #6 on: January 26, 2013, 06:09:00 am » |
I edited code, and it compiles now, edited code is in the first post.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Online
Brattain Member
Karma: 269
Posts: 25395
Solder is electric glue
|
 |
« Reply #7 on: January 26, 2013, 01:34:54 pm » |
So what have you done about the problem I told you about in reply #5? That will stop it from working.
But another thing:- Looking at the code you seem to put the reset high on the counter, then you pulse it, which does nothing because the reset is high, then you leave it high. Unless you put the reset low it will not count. So:- Reset high Reset low pulse the counter.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 5
Posts: 112
|
 |
« Reply #8 on: January 26, 2013, 03:39:39 pm » |
on shift up It appears to give a alternating 1/0 and if you force an initial gear number, it does count down on shift down
You also need to de-bounce the gear lever (wait for it to drop back to central position)
|
|
|
|
|
Logged
|
For whom does the clock pulse? It pulses for you!
|
|
|
|
Offline
Full Member
Karma: 5
Posts: 112
|
 |
« Reply #9 on: January 26, 2013, 03:52:32 pm » |
Typical error .... if(GearDOWN=LOW) You need another "=" Seems to work now Why does 6 roll to 0? does your gearbox do that?
|
|
|
|
|
Logged
|
For whom does the clock pulse? It pulses for you!
|
|
|
|
Croatia
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #10 on: January 26, 2013, 05:46:23 pm » |
Hi all, first of all thanks on help. I studied this code and realized that it's full of bugs, so I decided today to write the complete new code. And I write the method's to show the digit and reset the counter and they works. And only problem remains how to fetch data from the hall sensors. So, I have 2 hall sensor's, one which register gear up, and one which register gear down. Let's call them UP and DOWN. This is the code from my void loop(): int UP=0; int DOWN=1;
int UPstate=HIGH; int DOWNstate=HIGH; void loop() { UPstate=digitalRead(UP); if(UPstate==LOW) { increment(); } DOWNstate=digitalRead(DOWN); if(DOWNstate==LOW) { decrement(); } }
EDIT: Should I use the resistor between hall sensor output and input pins?
|
|
|
|
« Last Edit: January 26, 2013, 06:25:56 pm by jocoj100 »
|
Logged
|
|
|
|
|
Croatia
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #11 on: January 26, 2013, 07:41:52 pm » |
Hi!
I put the 10k resistor, and thing works perfectly!
Thanks anyway!
LOCK!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 2
|
 |
« Reply #12 on: May 02, 2013, 12:24:11 pm » |
Interesting, I am just busy with the Attiny85 and the gear indicator. Can you share your code with me. I am currently using the example I got from the Suzuki Vstrom, but I am more familiar with the arduino C++ code than AVR C++
Thanks in advance.
|
|
|
|
|
Logged
|
|
|
|
|
Croatia
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #13 on: May 02, 2013, 02:07:01 pm » |
Hi adfrancois! You mentioned that you have v strom, I think that you have the wires which are coming out of gearbox, which can be used for gear sensor so try to investigate that a little more because that principle would be better, because you don't have to use hall sensors, and it's mechanical so it's 1000% bullet proof! This is my code, comments and some variable names are in Croatian so feel free to ask if something isn't clear:  #include <EEPROM.h> const int brojac = 1; //clk pin 4026 const int r=0; //reset pin 4026 const int n=2; const int gornji=3; //UP sensor const int donji=4; //Down sensor int trenutnaBrzina=0; //Current gear //Adress to store in eprom current gear const int adresa=0; byte spremljenaBrzina; void Spremi(byte b) { EEPROM.write(adresa,b); } //Method to show number using 4026 IC void prikaziBroj(int broj) { if(broj>trenutnaBrzina) { for(int i=0;i<broj-trenutnaBrzina;i++) { digitalWrite(brojac,HIGH); digitalWrite(brojac,LOW); } trenutnaBrzina=broj; } else { reset(r); for(int i=0;i<broj;i++) { digitalWrite(brojac,HIGH); digitalWrite(brojac,LOW); } trenutnaBrzina=broj; } } //This method is called when UP sensor is activated void visa() { if(trenutnaBrzina==1) { prikaziBroj(0); } else if(trenutnaBrzina==0) { prikaziBroj(2); } else if(trenutnaBrzina+1 <7) { prikaziBroj(trenutnaBrzina+1); } Spremi(trenutnaBrzina); }
//This method is called when DOWN sensor is activated void niza() { if(trenutnaBrzina==0) { prikaziBroj(1); } else if(trenutnaBrzina==2) { prikaziBroj(0); } else if(trenutnaBrzina>2 && trenutnaBrzina!=1) { prikaziBroj(trenutnaBrzina-1); } Spremi(trenutnaBrzina); } //Reset 4026 void reset(int pin) { digitalWrite(pin,HIGH); digitalWrite(pin,LOW); } void setup() { pinMode(r,OUTPUT); digitalWrite(r,LOW); delay(1000); pinMode(brojac, OUTPUT); pinMode(gornji,INPUT); pinMode(donji,INPUT); pinMode(n,INPUT); digitalWrite(n,HIGH); digitalWrite(gornji,HIGH); digitalWrite(donji,HIGH); reset(r); spremljenaBrzina=EEPROM.read(adresa); prikaziBroj(spremljenaBrzina); } //UP, DOWN, N values int gornjiVrij=HIGH; int donjiVrij=HIGH; int nVrij; void loop() { delay(100); nVrij=digitalRead(n); if(nVrij==LOW) { prikaziBroj(0); delay(100); Spremi(0); } else { gornjiVrij=digitalRead(gornji); if(gornjiVrij==LOW) { visa(); delay(100); } delay(100); donjiVrij=digitalRead(donji); if(donjiVrij==LOW) { niza(); delay(100); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 2
|
 |
« Reply #14 on: May 03, 2013, 12:38:15 am » |
Thanks a lot. No I don't have a Vstrom, but I found some code referencing to this bike.
Your code is running, but seems I have a problem with the hall sensors, they don't pickup the signal. (I discovered I use linear, should go for digital) It only counts down when I bring PB4 to ground. Also port PB3 seems to be defect, while it doesn't work when I bring this to ground. I will try this weekend with another chip.
Have a nice weekend.
|
|
|
|
« Last Edit: May 03, 2013, 07:48:21 am by adfrancois »
|
Logged
|
|
|
|
|
|