Hi guys
I'm having a go at writing a bit of C++ for an arduino lightsaber fx project and I could do with some pointers in one or two places.
Parts of the code have been kindly donated by an arduino.cc forum member working on an almost identical project. Unfortunately there were enough differences between our systems that I couldn't use the code in full but at least its prompted me to learn a useful skill set to go alongside my EE course.
The rest I've lifted from examples and modified where necessary, or written from scratch but it all needs stitching together properly.
#define WT588D_CS 12 //Module pin "P02" or pin # 11
#define WT588D_SCL 11 //Module pin "P03" or pin # 10
#define WT588D_SDA 13 //Module pin "P01" or pin # 9
#define WT588D_BUSY 4 //Module pin "BUSY" or pin # 15
const int flickerPin = 5; //flicker LED to mosfet gate
const int ledPin = 9; //constant LED channel to mosfet gate
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
#define DELAY_(1000)
const int buttonPin = 2; // momentary switch on 2
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int chip selectPin = 10; // WT588D chip select
int regulatorPin = A1; // regultor wake up pin
int busyPin = A0; // WT588D busy line
const int groundpin = 18; // analog input pin 4 -- ground
const int powerpin = 19; // analog input pin 5 -- voltage
bool saber_is_on;
void setup ()
{
pinMode(ledPin, OUTPUT);
pinMode(flickerPin, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
pinMode(chipselectPin, OUTPUT);
pinMode(regulatorPin, OUTPUT);
pinMode(busyPin,INPUT);
}
{ boolean handle_button() { int button_pressed = !digitalRead(BUTTON_PIN); // pin low -> pressed return button_pressed;
saber_is_on = false;}
{
digitalWrite(BUTTON_PIN, HIGH); // connect internal pull-up
digitalWrite(regulatorPin, HIGH); //switch on LP2992 regulator
digitalWrite(groundpin, LOW); // do I need this line?
digitalWrite(powerpin, HIGH); // do I need this line?
Serial.begin(9600);
}
void loop()
{
if(digitalRead(button) == LOW) //button is pressed
{
if(saber_is_on) //Saber is on, so turn it off
{
digitalWrite, nfetgatePin = LOW;
digitalWrite, ledPin = HIGH;
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 1000 milliseconds to see the dimming effect
delay(1000);
saber_is_on = false;
}
else //Saber is off, so turn it on
// add sound off code
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
/add sound on code
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 1000 milliseconds to see the dimming effect
delay(1000);
digitalWrite, ledPin = LOW;
digital Write, nfetgatePin = HIGH;
saber_is_on = true;
}
}
}
While I've already defined the pins for other parts of the system, I thought getting the blade LED working as I want would probably be a good place to start.