programming in arduino

Status
Not open for further replies.

Abderrahim Milano

Newbie level 5
Joined
Apr 7, 2014
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
85
PHP:
5.4 Code source du prototype avec laser
#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);
int Signal1 = 2, Signal2 = 3;
int STATE, PreSTATE;
const int NUM_KEYS = 5;
const int adc_key_val[NUM_KEYS] ={30, 150, 360, 535, 760 };
int key = -1, oldkey = -1;
boolean en_position = false, lasers_detectes = false, lasers_non_detectes = false, fin_affichage = false;
char buf1[50], buf2[50];
unsigned long depart, fin, hauteur;
void setup()
{
pinMode(Signal1, INPUT);
pinMode(Signal2, INPUT);
pinMode(11,INPUT);
lcd.init();
lcd.clear();
STATE = 0; // 0 : mise en position; 1: attend le saut; 2 :le saut; 3 : atterrissage; 4 : calcul et affichage;
PreSTATE = 5; // un état avant l'état
}
void loop()
{
//---Condition---
switch( STATE )
{
case 0:
case 5:
//touche pressé
if( key_detector() ) en_position = true;
else en_position = false;
break;
case 1:
//au moins un laser présent
if( digitalRead(Signal1)==LOW || digitalRead(Signal2)==LOW ) lasers_detectes = true;
else lasers_detectes = false;
break;
case 3:
//aucun laser présent
if( digitalRead(Signal1)==HIGH && digitalRead(Signal2)==HIGH ) lasers_non_detectes = true;
else lasers_non_detectes = false;
break;
// case 5:
// if( digitalRead(11) == HIGH ) fin_affichage = true;
// else fin_affichage = false;
default:
break;
}
//---Transition---
switch( STATE )
{
case 0:
if( en_position )
{
STATE=1;
en_position = false;
oldkey = -1;
}
break;
case 1:
Page 45
if( lasers_detectes )
{
STATE=2;
lasers_detectes = false;
}
break;
case 2:
STATE=3;
break;
case 3:
if( lasers_non_detectes )
{
STATE=4;
lasers_non_detectes = false;
}
break;
case 4:
STATE=5;
break;
case 5:
if( en_position )
{
STATE=0;
en_position = false;
oldkey = -1;
}
break;
default:
STATE=0;
break;
}
//---Action---
switch( STATE )
{
case 0:
hauteur=fin=depart=0;
//affichage
sprintf(buf1, "En position!");
sprintf(buf2, " ");
affiche_STATE();
break;
case 1:
sprintf(buf1, "Sautez!");
sprintf(buf2, " ");
affiche_STATE();
break;
case 2:
//prend la valeur de départ
depart = micros();
break;
case 4:
if(fin==0)
{
//prend la valeur finale
fin = micros();
//calcul
if( fin>depart ) hauteur = Calcul_de_Hauteur( fin-depart );
else hauteur = Calcul_de_Hauteur ( 0xFFFFFFFF-depart+fin );
//affichage
// sprintf(buf1, "Distance:");
// sprintf(buf2, "%lu mm", hauteur);
Page 46
// sprintf(buf1, "Temps de vol:");
// sprintf(buf2, "%lu us", fin-depart);
sprintf(buf1, "%lu mm", hauteur);
sprintf(buf2, "%lu us", fin-depart);
affiche_STATE();
}
break;
default:
break;
}
}
unsigned long Calcul_de_Hauteur(unsigned long temps)
{
unsigned long hauteur;
hauteur = temps/1000;
hauteur *= hauteur;
hauteur /= 8;
hauteur *= 981;
hauteur /= 100000;
return hauteur;
}
void affiche_STATE(void)
{
char test;
if(PreSTATE!=STATE)
{
lcd.clear();
lcd.cursorTo(1, 0);
lcd.printIn(buf1);
lcd.cursorTo(2, 0);
lcd.printIn(buf2);
PreSTATE=STATE;
}
}
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
boolean key_detector()
{
int adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) // if keypress is detected
{
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey)
Page 47
{
oldkey = key;
return true;
}
}
return false;
}
i have this code and i dont have arduino i whish to use pic 16f877A
can any bady help me to use this programme
 

What problem are you facing?Is the code not getting compiled?/the code is getting compiled but the expected result is not coming?
 

my prbobleme i want to change the lcd4bit and use the LiquidCrystal and i dont use the key
pelase can help to programmte it
 

my prbobleme i want to change the lcd4bit and use the LiquidCrystal and i dont use the key
pelase can help to programmte it

I do not understand that.Could you please explain again..?
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…