• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C and Arduino help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.
So how's it going?

I can never figure out the code, i'm just not familiar with the syntax yet...
Having said that, I think I have the idea of how it would work. I have pinMode(10, INPUT) (and 11 and 12 too).

Then inside the void loop we would have a big if statement saying "if pin 10 receives the 5v - call the buzz function with certain parameters, else if pin 11 receives 5v then call the function with different parameters etc?
 
Yeah! That's the one.
You can either store the pin state in a variable and then base the if off that stateswitch1State = digitalRead(10);
if (switch1State == 1){dostuff}, or you can call the digital read in the if: if (digitalRead(10) == 1){dostuff}

Either way works, different situations may call for one or another flavors, for this situation it doesn't really matter much.

If you're feeling advanced you can use a "boolean" instead of an int or a byte for the variable. A boolean can store either a 0(false) or a 1(true).
So you can say:
boolean switch1State;
switch1State = digitalRead(10);
if (switch1State) {dostuff} //this will do stuff if the switch returns 1, it is functionally identical to if (switch1State == 1), or if (switchstate == TRUE), if you want it to trigger on a 0 us:
if (!switch1State) {dostuff} instead, it's exactly the opposite.

Whether you use a boolean or a byte or an int doesn't really matter at this point, it only matters if you're trying to make you code execute faster (a boolean check takes fewer clock cycles than an integer check, for instance), or save on ram (a boolean takes one bit, an integer takes 16 bits).
 
Yeah! That's the one.
You can either store the pin state in a variable and then base the if off that stateswitch1State = digitalRead(10);
if (switch1State == 1){dostuff}, or you can call the digital read in the if: if (digitalRead(10) == 1){dostuff}

Either way works, different situations may call for one or another flavors, for this situation it doesn't really matter much.

If you're feeling advanced you can use a "boolean" instead of an int or a byte for the variable. A boolean can store either a 0(false) or a 1(true).
So you can say:
boolean switch1State;
switch1State = digitalRead(10);
if (switch1State) {dostuff} //this will do stuff if the switch returns 1, it is functionally identical to if (switch1State == 1), or if (switchstate == TRUE), if you want it to trigger on a 0 us:
if (!switch1State) {dostuff} instead, it's exactly the opposite.

Whether you use a boolean or a byte or an int doesn't really matter at this point, it only matters if you're trying to make you code execute faster (a boolean check takes fewer clock cycles than an integer check, for instance), or save on ram (a boolean takes one bit, an integer takes 16 bits).

Here is my code.......it compiles....but I should probably test it at Uni on Monday.

/*
*This program will play a tone at a specified frequency for a specified period of time depending on which button is pressed.
*/

int targetPin = 2;
boolean buttonState10;
boolean buttonState11;
boolean buttonState12;

void setup(){
pinMode(targetPin, OUTPUT); //set a pin for buzzer output
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
}

void loop(){
buttonState10 = digitalRead(10);
buttonState11 = digitalRead(11);
buttonState12 = digitalRead(12);

if (buttonState10 == 1)
{buzz(2, 523, 1000); // the function call for pin10
delay(1000); //delay for 1 sec before looping again
}
else if (buttonState11 == 1)
{buzz(2, 554, 1000); // the function call for pin11
delay(1000); //delay for 1 sec before looping again
}
else if (buttonState12 == 1)
{buzz(2, 587, 1000); // the function call for pin11
delay(1000); //delay for 1 sec before looping again
}
}
void buzz(int targetPin, long frequency, long length){
long delayValue = 1000000/frequency/2;
long numCycles = frequency * length / 1000;

for(long i=0; i < numCycles; i++){
digitalWrite(targetPin,HIGH);
delayMicroseconds(delayValue);
digitalWrite(targetPin,LOW);
delayMicroseconds(delayValue);
}
}
 
That ought to work, you may have to swap the 1s for 0s in your if statements, it depends on how you have the buttons wired.

Does the course not give you an Arduino to play with at home?
 
ahhh right, when the button is pressed the 5V does NOT go to the pin.
Yeah, we can borrow the Arduinos short term and we got told where they're sold.
 
A little update on how things are going....I have been a bit complacent with this subject, other subjects I'm doing have had some big assignments and I have a Cisco exam today for my networking unit...

Anyway, I have tomorrow off and I need to catch up on two workshops - they're both regarding analogue inputs and environment sensors.

I'm just reading through them now, we're to use potentionmeters, fans, LEDs, transistors etc.

We're building up towards an assignment where we make a climate control device.
 
Post 'em up if you have a moment, I've been enjoying doing the class projects. It's helpful to me, too :D
 
Task 1 is to make a circuit to control a fan and create a function
setSpeed(int targetPin, int level);

Where targetPin is the pin connected to the transistor that is in turn connected to the fan, level represents the resultant speed of the fan.

Then we continue to use the function in task 2 and 3

Task 2
Use a potentiometer to control the number of LEDs switched on by reading
the ADC value of the potentiometer and switching on LEDs depending on this
value.

Task 3:
Use a potentiometer to control the speed of a fan by reading the ADC value of the potentiometer and adjusting the speed of the fan.
 
Should point out to the teacher that fans dislike being fed PWM (the exception being PWM fans, which generally want 25kHz PWM), and some fan datasheets specifically recommend against it :D
 
lol I'm in no position to point anything out to the teacher...he seems like a smart guy who knows what he's doing.

I'm trying task 1 now, here's my code so far...

/*
* Workshop 4 Task 01
* This program will switch a fan on for 1 second, then off for 1 second
*/

void setup(){

}
void loop(){
setSpeed(8,1000);
}

void setSpeed(int targetPin, int level){
//set digital pin output to "level"
digitalWrite(targetPin,level);
delay(1000); //wait for a second
//set the digital pin output to low
digitalWrite(targetPin,LOW);
delay(1000); //wait for a second
}

A few questions really....
1. Is it OK to have nothing in void setup?
2. When using targetPin in the function...what's the deal with scopes and declarations etc? I tried to set targetPin for digital output in the void setup and it gave me errors.
3. Why does this need to be connected to a transistor?
 
You can set targetpin to output in the function, it's clunky but it works.
Or you can set 8 to output before that.

That said, pin8 doesn't have PWM so you won't be able to do anything but turn a fan on and off with it.
If you want to do PWM you'll need to use one of the following: 3, 5, 6, 9, 10, 11.
Then you can "analogWrite(pinNumber, Level);"
With Level being from 0 (full off) to 255 (full on).

You'll need to use a transistor to switch the fan, it'll chunk the Arduino if you connect it directly. (Unless its a four wire fan and you hook the PWM line to the Arduino).
 
You can set targetpin to output in the function, it's clunky but it works.
Or you can set 8 to output before that.

That said, pin8 doesn't have PWM so you won't be able to do anything but turn a fan on and off with it.
If you want to do PWM you'll need to use one of the following: 3, 5, 6, 9, 10, 11.
Then you can "analogWrite(pinNumber, Level);"
With Level being from 0 (full off) to 255 (full on).

You'll need to use a transistor to switch the fan, it'll chunk the Arduino if you connect it directly. (Unless its a four wire fan and you hook the PWM line to the Arduino).

Oh, I think a fundamental just clicked....when you call the function, you are assigning values to those variables in the brackets in the function line?

So it's like saying targetPin = 3;
level = 255;

then having the function code inside the void loop but you keep it modular this way?


Here's my updated code....
/*
* Workshop 4 Task 01
* This program will switch a fan on for 1 second, then off for 1 second
*/


void setup(){
}

void loop(){
setSpeed(3,255);
}


void setSpeed(int targetPin, int level){
//set pwm pin output to "level"
analogWrite(targetPin,level);
delay(1000); //wait for a second

//set the pwm pin output to low
analogWrite(targetPin,LOW);
delay(1000); //wait for a second
}
 
Oh, I think a fundamental just clicked....when you call the function, you are assigning values to those variables in the brackets in the function line?

So it's like saying targetPin = 3;
level = 255;

then having the function code inside the void loop but you keep it modular this way?


Here's my updated code....
/*
* Workshop 4 Task 01
* This program will switch a fan on for 1 second, then off for 1 second
*/


void setup(){
}

void loop(){
setSpeed(3,255);
}


void setSpeed(int targetPin, int level){
//set pwm pin output to "level"
analogWrite(targetPin,level);
delay(1000); //wait for a second

//set the pwm pin output to low
analogWrite(targetPin,LOW);
delay(1000); //wait for a second
}

analogWrite doesn't know what LOW is, it'll need to be "analogWrite(targetPin, 0);", same end result that you're looking for.

At the top of void setspeed, put "pinMode(targetPin, OUTPUT)", otherwise you're limited to something like 0.25ma of signal.
I think analogWrite might do it automatically, I don't remember, but it's best not to rely on automatics like that.
 
OK I think I'm done with that first part....now onto the second one where we turn on LEDs depending on the potentiometer.

I used a switchCase statement.....I guess it would work but I'm thinking it was inefficient. (Any time I'm copy-pasting something 4 times and altering it slightly each time, I realise this is technically "incorrect").

Anyway...

/*
* Workshop 4 Task 02
* This program will switch on a variable number of LEDs depending on a potentiometer
*/

int ledPin11 = 11;
int ledPin12 = 12;
int ledPin13 = 13;
int potenPin = 3;
int powerLevel;

void setup(){
//Declare the LED pins as output
pinMode(ledPin11, OUTPUT);
pinMode(ledPin12, OUTPUT);
pinMode(ledPin13, OUTPUT);
//Declare the input pin
pinMode(potenPin, INPUT);
}

void loop(){
powerLevel = analogRead(potenPin);
int range = map(powerLevel, 0, 255, 0, 3);

// do something different depending on the
// range value:
switch (range) {
case 0: // no LEDs
Serial.println("All LEDs off");
break;
case 1: // 1 LED
Serial.println("First LED on");
digitalWrite(ledPin11, HIGH);
break;
case 2: // 2 LEDs
Serial.println("First 2 LEDs on");
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin12, HIGH);
break;
case 3: // 3 LEDs
Serial.println("First 3 LEDs on");
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin12, HIGH);
digitalWrite(ledPin13, HIGH);
break;
}
}

It compiles....I'm not sure if I've missed anything though.
 
That's actually an excellent way to do it, really.

Couple minor things:
You'll need to turn the LEDs off if you want to be able to turn the pot the other way and have something happen.
Analog input range is 0-1023.
Other than that, it looks great.
 
That's actually an excellent way to do it, really.

Couple minor things:
You'll need to turn the LEDs off if you want to be able to turn the pot the other way and have something happen.
Analog input range is 0-1023.
Other than that, it looks great.

/*
* Workshop 4 Task 02
* This program will switch on a variable number of LEDs depending on a potentiometer
*/

int ledPin11 = 11;
int ledPin12 = 12;
int ledPin13 = 13;
int potenPin = 3;
int powerLevel;

void setup(){
//Declare the LED pins as output
pinMode(ledPin11, OUTPUT);
pinMode(ledPin12, OUTPUT);
pinMode(ledPin13, OUTPUT);
//Declare the input pin
pinMode(potenPin, INPUT);
}

void loop(){
powerLevel = analogRead(potenPin);
int range = map(powerLevel, 0, 1023, 0, 3);

// do something different depending on the
// range value:
switch (range) {
case 0: // no LEDs
Serial.println("All LEDs off");
digitalWrite(ledPin11, LOW);
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin13, LOW);
break;
case 1: // 1 LED
Serial.println("First LED on");
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin13, LOW);
break;
case 2: // 2 LEDs
Serial.println("First 2 LEDs on");
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin12, HIGH);
digitalWrite(ledPin13, LOW);
break;
case 3: // 3 LEDs
Serial.println("All 3 LEDs on");
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin12, HIGH);
digitalWrite(ledPin13, HIGH);
break;
}
}

How's that look? I'm going onto workshop 5 now...I haven't been doing Task 3 for each workshop because the lecturer said it wasn't necessarily needed.

Workshop 5 is about thermistors and calculating temp.

Task 1:
Complete and test a program that uses a simple lookup table to print the
temperature from a thermistor to the serial console. Test it by holding the
thermistor between two fingers and watch the resistance drop, voltage increase
and temperature increase.

Task 2:
Improve the structure of your program by creating, implementing and using
the following functions:
int getTemperatureUsingLookup(int pin); //gets the temperature from a
thermistor attached to an analog pin
int getTemperatureInKelvin(int pin); //returns the temperature in K.


int getTemperatureInCelcius(int pin); //returns the temperature in C.


int getTemperatureInFarenheit(int pin); //returns the temperature in F
Ensure your program can support different types of thermistors and resistors
by the use of appropriate functions and variables. Test your equations with
different resistors; does it still work?

oh and a quick question....for analog pins are you supposed to use pinMode(A0, INPUT) <----- like A for analog? Or..when you say INPUT does it assume you mean the analog pin?
 
Last edited:
analogRead automatically sets the pin to input, so you don't have to worry about that.
You can pinMode it like that if you want though.

Your LED program looks like it should work!


I've been wondering how to make lookup tables, so I'm not going to be a huge help on that one :D
I'll look into it though, it's something I need to figure out.
 
analogRead automatically sets the pin to input, so you don't have to worry about that.
You can pinMode it like that if you want though.

Your LED program looks like it should work!


I've been wondering how to make lookup tables, so I'm not going to be a huge help on that one :D
I'll look into it though, it's something I need to figure out.

uh oh....Bob can't help...I'm screwed lol
Seriously though, I was just looking at this example http://www.arduino.cc/playground/ComponentLib/Thermistor

There are a few other examples of thermistor programs on the Arduino site too. I'll try to disect them...but you might understand them better than I.
 
That one makes sense to me, it's just horrendously crude is all.
I want to find one that you calibrate by setting a few variables and it extrapolates the temp between 'em.
I don't know that it's even possible without writing the equation myself (not likely to happen, heh, more likely to give me a massive headache).
 
Back