Author Topic: First time coding  (Read 2530 times)

0 Members and 1 Guest are viewing this topic.

Offline CanobiTopic starter

  • Regular Contributor
  • *
  • Posts: 94
  • Country: gb
First time coding
« on: November 05, 2015, 12:11:12 pm »
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.


Code: [Select]
#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.

 

Offline rs20

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
Re: First time coding
« Reply #1 on: November 05, 2015, 12:22:25 pm »
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 long at most.
 

Offline CanobiTopic starter

  • Regular Contributor
  • *
  • Posts: 94
  • Country: gb
Re: First time coding
« Reply #2 on: November 05, 2015, 02:57:37 pm »
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 long at most.

Thanks rs20, I'm on it :)

One 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.
 

Offline CanobiTopic starter

  • Regular Contributor
  • *
  • Posts: 94
  • Country: gb
Re: First time coding
« Reply #3 on: November 05, 2015, 08:33:38 pm »
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: [Select]

    int buttonPin = 2;
    int ledPin = 9;

int state = HIGH;
int reading;
int previous = LOW

long 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;
else
   state = 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: [Select]
int buttonPin = 2;
int flickerPin = 5;
int ledPin = 9;

int state = HIGH;
int reading;
int previous = LOW

long time = 0;
long debounce = 200;

#define delay = 1000

void 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;
else
   state = HIGH;


time = millis();
      }

      digitalWrite (ledPin, state);
delay, (delay);
      digitalWrite (ledPin, LOW);
      digitalWrite (flickerPin, HIGH);
else
      digitalWrite (flickerPin, LOW);
      digitalWrite (ledPin, HIGH);
delay, (delay);
      digitalWrite (ledPin, state);

       previous = reading;


}
 

Offline rs20

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
Re: First time coding
« Reply #4 on: November 05, 2015, 08:52:18 pm »
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: [Select]

    int buttonPin = 2;
    int ledPin = 9;

int state = HIGH;
int reading;
int previous = LOW

long 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;
else
   state = HIGH;


time = millis();
      }

      digitalWrite(ledPin, state);

       previous = reading;

}

That 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.

One 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.

They'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.
« Last Edit: November 06, 2015, 02:12:24 am by rs20 »
 

Offline rs20

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
Re: First time coding
« Reply #5 on: November 05, 2015, 08:56:29 pm »
In fact, I strongly recommend that you start with a few basic tutorials, e.g.:

https://www.arduino.cc/en/Tutorial/Blink

And I mean, actually do the tutorial, rather than just read it!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf