Show Posts
Pages: 1 [2]
16  Products / The Arduino Starter Kit / Project 13, library error! on: June 23, 2013, 12:17:42 pm
I can't really use the CapacitiveSensor header.

Here's my code (copied from the book):

Code:
#include <CapacitiveSensor.h>
CapacitiveSensor capSensor = CapacitiveSensor(4,2);

int threshold = 1000;
const int ledPin = 12;

void setup(){
 
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
 
}

void loop(){
 long sensorValue = capSensor.capacitiveSensor(30);
 Serial.print(sensorValue);
 
 
 if(sensorValue > threshold){
   digitalWrite(ledPin, HIGH);
 }
 else{
   digitalWrite(ledPin, LOW);
 }
 delay(10);
 
}

So, two questions:

1. How can I fix the header bug, so that Arduino recognizes this: "#include <CapacitiveSensor.h>"?

2. Do I need to hook the sensor wire to something specific provided in the Starter Kit, or can I connect it to anything conductive?



Thanks in advance!
17  Using Arduino / Programming Questions / Re: Communication with the PC problem! on: May 12, 2013, 02:34:23 am
Thanks guys! I added "pinMode(5, OUTPUT);" & changed the first line in the loop to "while(Serial.available()){", now it works just as intended. Thx!
18  Using Arduino / Programming Questions / Communication with the PC problem! on: May 11, 2013, 03:04:08 pm
Hello. I wrote some code after watching a tutorial on Youtube. Basically, I'd like to control an LED through the communication window.

However, the code does not work. Please take a look at it.


Code:
int ledPin = 5;

void setup(){
  Serial.begin(9600);
}


void loop(){

  while(Serial.available() == 0);

     int val = Serial.read() - '0';

if(val == 1){
  digitalWrite(ledPin, HIGH); // this doesn't executed
  Serial.println("LED ON"); // this gets executed
}
else if(val == 0){
  digitalWrite(ledPin, LOW);
  Serial.println("LED OFF"); this gets executed
}
else{
  Serial.println("ERROR!");
}

Serial.flush();

}

Also, the Serial.flush() doesn't work. If I type in "asd" in the window, I get three "ERROR!" lines, instead of just one.

Please help me control the LED and figure the Serial.flush() out!

Thx in advance!
19  Products / The Arduino Starter Kit / PROJECT 10 small modification! on: April 24, 2013, 08:03:08 am
Hi guys. I just finished project 10 (gave up on my own code and pretty much copied whatever was in the book  smiley-mr-green).

I want to make a small modification:

When I hold the motorSwitch button down, I want to wait 2 seconds before the motor starts spinning. However, when I want to shut it down, I just press the switch.

Can someone please explain how I can do this in a smart way (all of my ideas were terrible)?

The code so far:

Code:
int enablePin = 9;
int OnOffMotorSwitch = 5;
int directionSwitch = 4;

int OnOffMotorState = 0;
int directionState = 0;

int previousMotorState = 0;
int previousDirectionState = 0;

int motorEnable = 0;
int directionEnable = 0;

void setup(){
pinMode(enablePin, OUTPUT);
pinMode(OnOffMotorSwitch, INPUT);
pinMode(directionSwitch, INPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(6, OUTPUT);

digitalWrite(enablePin, LOW);

Serial.begin(9600);

}


void loop(){
  int motorDC = analogRead(A0) / 4;
  int OnOffMotorState = digitalRead(OnOffMotorSwitch);
  int directionState = digitalRead(directionSwitch);

 if(OnOffMotorState != previousMotorState){
   if(OnOffMotorState == HIGH){
     motorEnable = !motorEnable;
     }
   }
 
 if(directionState != previousDirectionState){
  if(directionState == HIGH){
   directionEnable = !directionEnable;
  }
 }
 
 if(motorEnable == true){
  analogWrite(enablePin, motorDC);
 }
 else{
   analogWrite(enablePin, 0);
 }
 
 
 if(directionEnable == true){
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
 }
 else{
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
 }
 

 
  previousMotorState = OnOffMotorState;
  previousDirectionState = directionState;
 
}

Thanks in advance.
20  Products / The Arduino Starter Kit / Re: Project 8 (DIGITAL HOURGLASS) on: April 16, 2013, 10:09:30 am
Thanks for the explanation! I didn't try the timer but it looks like it could be very useful. Nice one!
21  Products / The Arduino Starter Kit / Re: Project 8 (DIGITAL HOURGLASS) on: April 16, 2013, 09:34:46 am
Thx for the advice guys. I couldn't spot my bug so I just wrote new (and more efficient) code, and now it works just as it should. Here it is:

Code:
void setup(){
 Serial.begin(9600);
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);
 for(int i = 7; i < 11; i++){
   pinMode(i, OUTPUT);
 }
 pinMode(12, OUTPUT);
}

unsigned long previousTime = 0;
int interval = 2000;
int l = 2;

void loop(){
unsigned long currentTime = millis();
int buttonPin = digitalRead(12);

 if(currentTime - previousTime > interval){
   previousTime = currentTime;
   
   digitalWrite(l, HIGH);
   l++;
   
     if(l == 4){
       l = 7;
     }
     if(l == 11){
       l--;
     }
 }
 if(buttonPin){
   for(int j = 2; j < 11; j++){
     digitalWrite(j, LOW);
       if(j == 4){
         j = 6;
       }
       previousTime = currentTime;
       l = 2;
 }
 
 
}
}

What does debounce mean?
22  Products / The Arduino Starter Kit / Project 8 (DIGITAL HOURGLASS) on: April 15, 2013, 11:06:49 am
Hello!
I just finished project 8, but it wasn't very stimulating so I tried to add a button instead of a tilt sensor (well, mainly because I couldn't find a tilt sensor in my Starter-kit lol). So basically, the button is supposed to reset all LED's, and it works, atleast sort of.
The problem is this: I have to hold the reset button down for the same amount of time as my "interval" variable for it to work. Look at the code please.

Code:
void setup(){
 Serial.begin(1200);
 pinMode(12, INPUT);
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(10, OUTPUT); 
 

}
int outPin = 2;
int interval = 3000; // this is how long I have to hold the button down in order for it to reset
unsigned long previousTime = 0;

void loop(){
  int inPin = digitalRead(12);
  unsigned long currentTime = millis();


  if(currentTime - previousTime  > interval){
    previousTime = currentTime;
 
    digitalWrite(outPin, HIGH);
    outPin++;
      if(outPin == 4 && digitalRead(3) == HIGH){ // I had to do this because I didn't connect the LED's in order (1..2..3..4..5 etc)
      outPin = 7;
      }
if(inPin){
 
  for(int i = 2; i <11; i++){
    digitalWrite(i, LOW);
    if(i == 3){
      i = 6;
      }
    outPin = 2;
    previousTime = currentTime;
    inPin = LOW;
}
}

}
}

Can someone please spot my bug? Please ask if you need me to explain my code a bit clearer, I know it's not the most handsome code out there!

Thanks in advance!

23  Products / The Arduino Starter Kit / Re: Project 04 on: March 28, 2013, 05:00:58 am
I have the same issues.
24  Products / The Arduino Starter Kit / Re: Project 3 issues! on: March 27, 2013, 04:41:01 pm
Thx again for replying.

Yes I have done that.


Here is the full code.

Code:
void setup(){
 
 pinMode(2, INPUT);
 pinMode(3, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(11, OUTPUT);
 pinMode(12, OUTPUT);
 
}
 
 
 
void loop(){
 
  int button = 0;
 
  button = digitalRead(2);

 
  if(button == HIGH){
   
    analogWrite(3, 30);
    delay(75);
    analogWrite(5, 60);
    delay(75);
    analogWrite(6, 90);
    delay(75);
    analogWrite(9, 120);
    delay(75);
    analogWrite(10, 150);
    delay(75);
    analogWrite(11, 180);
    delay(75);
    analogWrite(12, 210);
    delay(75);
    analogWrite(3, 0);
    delay(100);
    digitalWrite(5, LOW); // I used digitalWrite here because I can't spot the difference anyways.
    delay(75);
    digitalWrite(6, LOW);
    delay(75);
    digitalWrite(9, LOW);
    delay(75);
    digitalWrite(10, LOW);
    delay(75);
    digitalWrite(11, LOW);
    delay(75);
    digitalWrite(12, LOW);
    delay(75);
   
   
   
}
else{
    digitalWrite(3, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
 
}
}
25  Products / The Arduino Starter Kit / Re: Project 3 issues! on: March 27, 2013, 02:50:49 pm
The port is not damaged, it works well when I'm doing other projects.. Ahwell, I can try looking into it when I'm more experienced!

You seem to know a whole lot, so do u mind if I ask why how the analogWrite function works?

I wanted to make a few LEDs shine with different brightness, this is part of the code:

 
Code:
analogWrite(3, 30);
 analogWrite(5, 60);
 analogWrite(6, 90);
 analogWrite(9, 120);

However, I can't see any brightness-level difference. Do you know why?

Thanks!
26  Products / The Arduino Starter Kit / Re: Project 3 issues! on: March 27, 2013, 06:24:48 am
Hello, thx for replying.

There is nothing wrong with the LED, I tried 7-8 different ones.


As for my baselineTempt, I set it to the temperature that my sensor measured. Of course my room temperature is not 28.7 C, but if I set the baselineTempt to 22 C or so, it would pretty much always stay in the same mode.
27  Products / The Arduino Starter Kit / Project 3 issues! on: March 26, 2013, 05:20:27 pm
Hello. Today I began doing some Arduino projects (I love it thus far!) and it went well. However, during project 3, one of my LEDs doesn't glow much. I'll post the code, hopefully that will help you understand my issue.


Code:
const float baselineTempt = 28.70;
 
void setup(){

 
 Serial.begin(1200);
 
  for(int pinNumber = 2; pinNumber < 5; pinNumber++){
 
  pinMode(pinNumber, OUTPUT);
  pinMode(pinNumber, LOW);
  } 
}

void loop(){
 
 int sensorVal = analogRead(A0);
 float voltage = (sensorVal/1024.0) * 5.0;
 float temperature = (voltage - .5) * 100;

 
Serial.print("Sensor value: ");
Serial.println(sensorVal);
Serial.print("Voltage: ");
Serial.println(voltage);
Serial.print("degrees C: ");
Serial.println(temperature);
Serial.println("    ");

if(temperature <= baselineTempt){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
}
else if(temperature < (baselineTempt + 2) && temperature > baselineTempt){
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
}
else if(temperature >= (baselineTempt + 2) && temperature < (baselineTempt + 4)){
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
}
else if(temperature >= baselineTempt + 4){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
 
}
delay(1);
}

Basically, the OUTPUT port 3 LED won't glow much. I feel that there is not enough voltage or something. Even if I try without a resistor, it barely glows.

So, question 1: Do you know why it (the output 3 port) doesn't glow? I don't think it's a code issue. ||EDIT|| I just noticed that the output port 3 is reserved for PMW. Could this perhaps explain why it glows so vaguely? If this is the case, can I change it to a static 5V output?

Question 2: The temperature sensor (TMP36) feels sort of dull.. I have to press it hard for it to increase it's temperature, almost too hard. I know it's not high-tech but still, I have nothing to compare it with. Should it be smoother or is it working as intended?


Thanks in advance.
Pages: 1 [2]