Go Down

Topic: Arduino and HALL sensors problem (Read 934 times) previous topic - next topic

jocoj100

#10
Jan 26, 2013, 11:46 pm Last Edit: Jan 27, 2013, 12:25 am by jocoj100 Reason: 1
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():
Code: [Select]


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?

jocoj100

Hi!

I put the 10k resistor, and thing works perfectly!

Thanks anyway!

LOCK!

adfrancois

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.

jocoj100

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:  :)
Code: [Select]
  #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);
      }
    } 
  }

adfrancois

#14
May 03, 2013, 07:38 am Last Edit: May 03, 2013, 02:48 pm by adfrancois Reason: 1
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.

Go Up