First time coding - Page 1
Forum section: electronics / general

author: Canobi
author_rank: Regular Contributor
timestamp: November 05, 2015, 12:11:12 pm
content: Hi guysI'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.Code:#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 # 15const int flickerPin = 5; //flicker LED to mosfet gateconst int ledPin = 9; //constant LED channel to mosfet gateint brightness = 0; // how bright the LED isint fadeAmount = 5; // how many points to fade the LED by#define DELAY_(1000)const int buttonPin = 2; // momentary switch on 2int buttonPushCounter = 0;int buttonState = 0;int lastButtonState = 0;int chip selectPin = 10; // WT588D chip selectint regulatorPin = A1; // regultor wake up pinint busyPin = A0; // WT588D busy lineconst int groundpin = 18; // analog input pin 4 -- groundconst int powerpin = 19; // analog input pin 5 -- voltagebool 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-updigitalWrite(regulatorPin, HIGH); //switch on LP2992 regulatordigitalWrite(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 effectdelay(1000);saber_is_on = false;}else //Saber is off, so turn it on// add sound off codefor(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 effectdelay(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.

author: rs20
author_rank: Super Contributor
timestamp: November 05, 2015, 12:22:25 pm
content: You should start with a program that just turns the LED on when the button is pressed, and is off otherwise. Forget about dimming and everything else for now; get something that compiles and works.The sketch should only be about 20 lines longat most.

author: Canobi
author_rank: Regular Contributor
timestamp: November 05, 2015, 02:57:37 pm
content: Quote from: rs20 on November 05, 2015, 12:22:25 pmThanks rs20, I'm on itOne thing I'm still not clear on is whether I need to define the RAW and ground pins as inputs or whether they're a given.

author: Canobi
author_rank: Regular Contributor
timestamp: November 05, 2015, 08:33:38 pm
content: I found a switch tutorial which covers button state and led on/off which onmy needed a few minor changes. I confess I don't quite get the language yet but the structure is simple enough and on the face of it, it looks like a much neater way to handle things.Code:int buttonPin = 2;int ledPin = 9;int state = HIGH;int reading;int previous = LOWlong time = 0;long debounce = 200;void setup (){pinMode (buttonPin, INPUT);pinMode (ledPin, OUTPUT);digitalWrite(BUTTON_PIN, HIGH);}void loop(){reading = digitalRead(buttonPin)if (reading == HIGH && previous == LOW && millis() - time > debounce) {if (state == HIGH);state = LOW;elsestate = HIGH;time = millis();}digitalWrite(ledPin, state);previous = reading;}This is my attempt to expand the code to include the second led pin, though I've probably missed a trick somewhere.What it should do is when the button is pressed the led comes on for 1 second, then swaps over to the other led pin until the button is pressed a second time. It then swaps the led pins back again and waits 1 second before turning off.Code:int buttonPin = 2;int flickerPin = 5;int ledPin = 9;int state = HIGH;int reading;int previous = LOWlong time = 0;long debounce = 200;#define delay = 1000void setup (){pinMode (buttonPin, INPUT);pinMode (flickerPin, OUTOUT);pinMode (ledPin, OUTPUT);digitalWrite(BUTTON_PIN, HIGH);}void loop(){reading = digitalRead(buttonPin)if (reading == HIGH && previous == LOW && millis() - time > debounce) {if (state == HIGH);state = LOW;elsestate = HIGH;time = millis();}digitalWrite (ledPin, state);delay, (delay);digitalWrite (ledPin, LOW);digitalWrite (flickerPin, HIGH);elsedigitalWrite (flickerPin, LOW);digitalWrite (ledPin, HIGH);delay, (delay);digitalWrite (ledPin, state);previous = reading;}

author: rs20
author_rank: Super Contributor
timestamp: November 05, 2015, 08:52:18 pm
content: Quote from: Canobi on November 05, 2015, 08:33:38 pmThat code clearly doesn't compile, and when the button is pressed, the "reading" variable will go low which means the "if" statement will be skipped over, so the LED will never respond to the button press. And yet, you've just gone on to add a second LED in the second snippet, having obviously never tested or debugged the simple case!!You need to compile and test your simple programs, rather than try to have it compiled by internet forums. It takes 10 seconds to have the computer attempt compilation for you, but forums will take about a day per iteration, at which rate it'll be halfway through next year before we're done!If you encounter an error that you don't understand, please feel free to ask.Quote from: Canobi on November 05, 2015, 02:57:37 pmThey're "a given", as you put it. The fact that you attempted to define pin A5 as the "Raw pin" suggests to me that you used an external, physical wire to connect Raw to A5 ... if so, you need to disconnect that immediately because Raw carries the raw voltage from the external power supply (if present) which can carry 9 volts or more. This will blow up your Arduino, Arduino inputs can only go to 5V.

author: rs20
author_rank: Super Contributor
timestamp: November 05, 2015, 08:56:29 pm
content: In fact, I strongly recommend that you start with a few basic tutorials, e.g.:https://www.arduino.cc/en/Tutorial/BlinkAnd I mean, actually do the tutorial, rather than just read it!