hi there.my first post, i hope i'm in the right place.
just started using arduino for my graduation project at my university.
i like to manage my own stuff, so i searched and searched and i put learned the language a bit, and put together a little program.
basically what it does, is control a stepper. the setup is an easydriver, stepper motor, ir sensor with remote control and a light sensor.using the stepper library, controlling is a charm.
the problem is,that i have now put it all in a single program so they work together and i stumbled upon a fault.i have to say i'm pretty much stuck.
i'm finding this difficult to explain, so i'll try my best.
when my program is doing something, it's reading the changes in lighting, but when when the motor is not moving, it just gets stuck inside the getIRKey function.when i press any buton on the remote, the light sensor changes it's value also starting to read again.
i have deleted everything not important, like the stepper motor controls, to get the code it's simplest form possible. any help is appreciated! thank you
#include <LiquidCrystal.h> /
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#define LIGHT_PIN A0
#define IR_PIN 12
#define LED_PIN 13
int desired = 100;
int button;
int start_bit = 2200;
int bin_1 = 1000;
int bin_0 = 400;
void setup()
{
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
pinMode(IR_PIN, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Desired:");
lcd.setCursor(10,0);
lcd.print(desired);
lcd.setCursor(15,0);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Actual:");
lcd.setCursor(15,1);
lcd.print("%");
}
void loop()
{
lcd.setCursor(10,0);
lcd.print(desired);
lcd.print(" ");
light();
getIRKey();
}
int light()
{
int val_light = analogRead(LIGHT_PIN);
val_light = map(val_light, 0, 1023, 0, 100);
lcd.setCursor(10, 1);
lcd.print(val_light,DEC);
lcd.print(" ");
return val_light;
}
void getIRKey()
{
int data[12];
int i;
while (pulseIn(IR_PIN, HIGH) < start_bit);
for(i = 0 ; i < 11 ; i++)
data[i] = pulseIn(IR_PIN, HIGH);
for(i = 0 ; i < 11 ; i++) //Parse them
{
if(data[i] > bin_1) //is it a 1?
data[i] = 1;
else if(data[i] > bin_0) //is it a 0?
data[i] = 0;
else
break; //Flag the data as invalid; I don't know what it is! Return -1 on invalid data
}
int result = 0;
for(i = 0 ; i < 11 ; i++)
if(data[i] == 1) result |= (1<<i);
switch(result)
{
case 64:
desired=100;
break;
case 1088:
desired=0;
break;
case 128:
if (desired < 100) { desired=desired++; }
break;
case 1152:
if (desired > 0) {desired=desired--;}
break;
}
return;
}