Programming Code

Hi guys,

Desperate for help with deadline around the corner! I am hopeless with electronics, programming arduino and have been to my tutor to help me understand it all - but he is absolutely useless!

Could someone please help me out with the the code for the following steps.

  • Turn switch on (device on)
  • Constant blue LED light turns on with switch
  • Reed switch is activated (when magnet is bought near reed switch)
  • Blinking red LED light turns on (when Reed switch is activated)
  • after two seconds of the magnet not being near the reed switch, the blinking red LED turns back to a constant blue LED
  • Turn switch off (to turn device off)

I have the Leostick 5v usb sized arduino.

PLEASE PLEASE PLEASE could someone help! Thanks guys! M

What have you written so far? Anything? Defined any pins to be used, created any variables, tried any examples from the Learning section, draftefd a schematic of how things will be connected?

MontannaD:
… but he is absolutely useless!

What have you tried?

Do you want us to do your project for you?

I am hopeless with electronics …

What course are you doing? Perhaps it was the wrong choice.

Programming arduino and electronics are not core in the degree, hence why I don’'t know.

This is as far a I have gotten with coding, I think some steps might be missing.

int switch = 1;
int reedSwitch = 2;
int ledRed = 3
int ledBlue = 4;

void setup() {
pinMode(switch, INPUT);
pinMode(reedSwitch, INPUT);
pinMode(ledRed, OUTPUT); // sets the digital pin as output
pinMode(ledBlue, OUTPUT); // sets the digital pin as output
switch = LOW;
reedSwitch = 0;
}

void loop() {
if (digitalRead(switch) == HIGH) {
digitalWrite(ledRed, HIGH);
}
else if (digitalRead(reedSwitch) > 1000) {
digitalWrite(ledRed, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledRed, LOW); // sets the LED off
delay(1000); // waits for a second
}
else if (digitalRead(switch) == LOW) {
digitalWrite(ledRed, LOW); //Makes sure the LED is off when not in use
digitalWrite(ledBlue, LOW); //Makes sure the LED is off when not in use
}
}

For starters, when I plug my leostick arduino into the computer (mac) and click upload, it says the coding has uploaded, but a new network device toolbar pops up, and the actual leostick arduino does not respond to the feedback.
Any Suggestions?

I have tried the circuit schematics, blew a few LEDs.
I really am trying, but with no guidance from tutor, how am I meant to learn. Tried tutorials online and did not understand.

MontannaD: Any Suggestions?

Learn how to upload a sketch to your Arduino and run the examples to blink LEDs and read/write over the serial port before you try to write anything of your own. You need to walk before you can run.

Any Suggestions?

Don't attach a switch to the TX pin (pin 1). You'll want that pin for debugging your program.

  pinMode(switch, INPUT);

You are not using the internal pullup resistor, so you need an external pullup or pulldown resistor and more complex wiring. You do have the switch wired with an external resistor, right?

  else if (digitalRead(reedSwitch) > 1000) {

The digitalRead() function returns either HIGH (1) or LOW(). Neither value is greater than 1000.

  switch = LOW;

Meaningful names are important. If you used Pin in the name of variables that hold pin numbers, silly mistakes like this would be avoided. You'd never type

   switchPin = LOW;

would you?

Some of your scenario are things the operator does (turn a switch on). Some are things that the program should do (turn an LED on, read a reed switch, etc.). You seem to be trying to make the Arduino turn the switch on or off, rather than reading whether the operator did.

So let’s work with what you have.
First, external connections"
You need 220, 270 ohm resistors between pins 3 & 4 and your LEDs. You say you blew some LEDs, hopefully you did not blow the IO pins, or the '328 itself, by connecting them directly. Make the wiring this: pin to resistor lead, other resistor lead to LED Anode, LED Cathode to Gnd.
Move the switch on pin 1 to pin 5. You need pins 0,1 free for the device to talk to the computer. Wire the switch so that when it is pressed, the pin is connected to Gnd.
Do the same with the reed switch - when the reed switch closes, it connects the pin to Gnd.

Now the code. You’re close, just needs some tweaking. Compare this to yours, you can see what’s changed.

// this is a comment, add as a line, or add at the end of a line
int manual_switch = 5;   //moved from pin 1, changed name
int reedSwitch = 2;
int ledRed = 3;
int ledBlue = 4;

byte  switchState ; // variable to store the state of the switch if needed
byte  reedState ; // variable to store the state of the switch if needed

void setup() {
  pinMode(manual_switch, INPUT);
digitalWrite(manual_switch, HIGH); // enables internal pullup resistor. Pin will read as HIGH when switch is open
  pinMode(reedSwitch, INPUT);
digitalWrite (reedSwitch, HIGH); // pullup resistor enabled
  pinMode(ledRed, OUTPUT);      // sets the digital pin as output
  pinMode(ledBlue, OUTPUT);      // sets the digital pin as output

Serial.begin(9600); // enable serial interface for sending messages out
}

void loop() {
// if switch is not pressed, the red LED stays off
  if (digitalRead(manual_switch) == HIGH) {
  digitalWrite(ledRed, LOW);
  }
// but when its pressed, make the LED flash
// don't need to read it again, has already been read
  else  {
    digitalWrite(ledRed, HIGH);   // sets the LED on
Serial.println ("Red on!");  // add a debugging statement - if the LED is not on, check the wiring.  Perhaps change pins if you suspect the pin is blown
    delay(1000);                  // waits for a second
    digitalWrite(ledRed, LOW);    // sets the LED off
Serial.println ("Red off!");
    delay(1000);                  // waits for a second
  }
// do similar for blue LED?
// if switch is not closed, the blue LED stays off
  if (digitalRead(reedSwitch) == HIGH) {
  digitalWrite(ledBlue, LOW);
  }
// but when its pressed, make the LED flash
// don't need to read it again, has already been read
  else  {
    digitalWrite(ledBlue, HIGH);   // sets the LED on
Serial.println ("Blue on!");
    delay(1000);                  // waits for a second
    digitalWrite(ledBlue, LOW);    // sets the LED off
Serial.println ("Blue off!");
    delay(1000);                  // waits for a second
  }
} // end loop