Guys
First, I am fairly new to this game, so please forgive my ignorance and appartent stupidity at times.
I am putting together a sketch to enable me to control multiple O/Ps using one of those little chinese keypads.
I have, with help from a couple of you guys, managed to get the array indexing to work (first bash at arrays).
I have hit another snag with pin 3 and one of my functions. All the function is doing, at the moment, is fading a LED, and the Serial.print is just so that I can see what is happening within the code. Once all the separate button functions work, then I can move on to writing the real functions. One step at a time.
The problem:
#include <IRremote.h>
#include <boarddefs.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>
const int RECV_PIN = 11;
unsigned long Code[] = {0xFF6897, 0xFF30CF, 0xFF18E7, 0xFF7A85, 0xFF10EF, 0xFF38C7, 0xFF22DD, 0xFF02FD};
//==============================================================
const byte Button_0_Pin = 2;
const byte Button_1_Pin = 3; // PWN pin
const byte Button_2_Pin = 4;
const byte Button_3_Pin = 5; // PWM pin
const byte Button_4_Pin = 6; // PWM pin
const byte Button_5_Pin = 7;
byte Button_0_Flag = 0;
byte Button_0_Pressed_Flag = 0;
byte Button_1_Flag = 0;
byte Button_1_Pressed_Flag = 0;
byte Button_2_Flag = 0;
byte Button_3_Flag = 0;
byte Button_4_Flag = 0;
byte Button_4_Pressed_Flag = 0;
byte Button_5_Flag = 0;
byte Button_5_Pressed_Flag = 0;
byte Button_Down_Flag = 0;
byte Button_Down_Pressed_Flag = 0;
byte Down_Running_Flag = 0;
byte Button_Up_Flag = 0;
byte Button_Up_Pressed_Flag = 0;
byte Up_Running_Flag = 0;
byte Pin_1_val = 0;
byte Light_Setting = 128;
unsigned long currentMillis = 0;
unsigned long Button_0_Millis = 0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
/* pinMode(4,OUTPUT);
digitalWrite(4,LOW);*/
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
currentMillis = millis();
if (irrecv.decode(&results))
{
delay(250);
for (byte i = 0; i <= 7; i++)
{
if (Code[i] == results.value)
{
if (i == 0)
{
if (Button_0_Pressed_Flag == 0)
{
Button_0_Pressed_Flag = 1;
digitalWrite(2, HIGH);
Button_0_Millis = millis();
}
else
{
Button_0_Pressed_Flag = 0;
digitalWrite(2, LOW);
break;
}
}
if (i == 1)
{
if (Button_1_Pressed_Flag == 0)
{
Button_1_Pressed_Flag = 1;
}
else
{
Button_1_Pressed_Flag = 0;
digitalWrite(3, LOW);
break;
}
}
/* if(i == 2)
{
// To define
}
if(i == 3)
{
// To define
} */
if (i == 4)
{
if (Button_4_Pressed_Flag == 0)
{
Button_4_Pressed_Flag = 1;
analogWrite(6, Light_Setting);
}
else
{
Button_4_Pressed_Flag = 0;
digitalWrite(6, LOW);
break;
}
}
if (i == 5)
{
if (Button_5_Pressed_Flag == 0)
{
digitalWrite(7, HIGH);
Button_5_Pressed_Flag = 1;
}
else
{
digitalWrite(7, LOW);
Button_5_Pressed_Flag = 0;
}
}
if (i == 6)
{
if (Button_Down_Flag == 0)
{
Button_Down_Flag = 1;
Button_Down();
}
else
{
Button_Down_Flag = 0;
}
}
if (i == 7)
{
if (Button_Up_Flag == 0)
{
Button_Up_Flag = 1;
Button_Up();
}
else
{
Button_Up_Flag = 0;
}
}
}
}
irrecv.resume(); // Receive the next value
}
Button_0();
Button_1();
Button_Down();
Button_Up();
}
//============================================================
// FUNCTIONS
//============================================================
void Button_0()
{
if ((Button_0_Pressed_Flag == 1) && (Button_0_Flag == 0))
{
if (currentMillis - Button_0_Millis >= 500)
{
digitalWrite(2, LOW);
Button_0_Flag = 1;
Button_0_Millis = millis();
}
}
if ((Button_0_Pressed_Flag == 1) && (Button_0_Flag == 1))
{
if (currentMillis - Button_0_Millis >= 500)
{
digitalWrite(2, HIGH);
Button_0_Flag = 0;
Button_0_Millis = millis();
}
}
}
//============================================================
void Button_1()
{
if (Button_1_Pressed_Flag == 1)
{
if (Button_1_Flag == 0)
{
delay(40);
Serial.println(Pin_1_val);
Pin_1_val += 5;
analogWrite(3, Pin_1_val);
if (Pin_1_val >= 255)
{
Button_1_Flag = 1;
}
}
else
{
dealay(40);
Serial.println(Pin_1_val);
Pin_1_val -= 5;
analogWrite(3, Pin_1_val);
if (Pin_1_val <= 0)
{
Button_1_Flag = 0;
}
}
}
}
//=============================================================
void Button_Down()
{
if ((Button_4_Pressed_Flag == 1) && (Button_Down_Flag == 1))
{
if (Light_Setting > 0)
{
delay(25);
Light_Setting--;
Serial.println(Light_Setting);
Serial.println(Button_4_Pressed_Flag);
if (Light_Setting <= 0)
{
Button_Down_Flag = 0;
;
}
analogWrite(6, Light_Setting);
}
}
}
//===========================================================
void Button_Up()
{
if ((Button_4_Pressed_Flag == 1) && (Button_Up_Flag == 1))
{
if (Light_Setting < 255)
{
delay(25);
Light_Setting++;
Serial.println(Light_Setting);
Serial.println(Button_4_Pressed_Flag);
if (Light_Setting >= 255)
{
Button_Up_Flag = 0;
;
}
analogWrite(6, Light_Setting);
}
}
}
//==============================================================
When the code for button 1 (0xFF30CF) is received, it sets the Button_1_Pressed_Flag and can then access the code in function Button_1(). Here in lies the problem. All the LED does is blink when Pin_1_val is >about 200.
If I change the analogWrites to pin 5, it works.
In case I had a bad connection or faulty gate, I ran the example “Fading” on Pin 3 it works.
Any ideas?