Home | Community | Message Board


This site includes paid links. Please support our sponsors.


Welcome to the Shroomery Message Board! You are experiencing a small sample of what the site has to offer. Please login or register to post messages and view our exclusive members-only content. You'll gain access to additional forums, file attachments, board customizations, encrypted private messages, and much more!

Shop: PhytoExtractum Buy Bali Kratom Powder, Kratom Powder for Sale   Kraken Kratom Red Vein Kratom   Bridgetown Botanicals CBD Concentrates

Jump to first unread post Pages: 1
Invisible404
error
 User Gallery


Registered: 08/20/10
Posts: 14,539
Programming/coding with Arduino * 1
    #21319469 - 02/23/15 07:34 PM (8 years, 11 months ago)

SUP! got myself an arduino Uno R3 microcontroller board.

i'm trying to code it so that the potentiometer controlls the brightness of the multi-color LED, and whenever i press the push button, it changes the color, from red to green to blue and then back to red but only when i push the button.

i'd like to get some pointers and i'd like to learn how to code properly with this. it's based off of C



she's all hooked up on the bread board and plugged in properly with restors and shit. now i just need to understand the coding better.

potentiometer hooked up to Analog pin 0
red led pin = digital pin 5
green led pin = digital pin 6
blue led pin = digital pin 7
push button = digital pin 2



this is what i have so far for the coding... i'm missing a bunch and not completely sure how to go about it



===================================================================================
int analogPin = 0;    // select the input pin for the potentiometer
int ledPinR = 5; //declares pin 5 as red led
int ledPinG = 6; // declares pin 6 as green led
int ledPinB = 7; // declares pin 7 as the blue led

}

void setup () {
  pinMode(2, OUTPUT); //pin 2 is now output
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(A0, INPUT);
}

void loop() {
    sensorValue = analogRead(sensorPin);  //reads the val from the potentiometer
digitalWrite (ledPinR, HIGH) // gives power to red led, needs to be condtional on pushbutton...?
digitalWrite (ledPinG, HIGH) // gives power to green led, needs to be conditional on pushbutton...?
digitalWrite (ledPinB, HIGH) // gives power to blue led, needs to be conditional on pushbutton...?



=========================================================================================
can someone help me get this going properly pretty please


Edit-my setup


Edited by 404 (02/24/15 06:19 AM)


Extras: Filter Print Post Top
Invisible404
error
 User Gallery


Registered: 08/20/10
Posts: 14,539
Re: Programming/coding with Arduino [Re: 404] * 1
    #21320007 - 02/23/15 09:21 PM (8 years, 11 months ago)

note: for some reason i can't even upload any new code to the board now - "Builder folder disappeared or could not be written"


Extras: Filter Print Post Top
OfflineNova

Registered: 10/16/02
Posts: 1,365
Last seen: 5 years, 7 months
Re: Programming/coding with Arduino [Re: 404]
    #21320373 - 02/23/15 10:31 PM (8 years, 11 months ago)

If you need a conditional based on a button being pushed just use a basic bool-if statement in the loop aka

if (pushbutton) {
loadcolor x;
x=x+1;
}
if (x>2) {
then x=0;
}

where x starts at 0 then it would cycle from 0 1 2 then back to 0. Thats about all I can help :shrug:


Extras: Filter Print Post Top
Invisible404
error
 User Gallery


Registered: 08/20/10
Posts: 14,539
Re: Programming/coding with Arduino [Re: Nova] * 1
    #21321310 - 02/24/15 06:19 AM (8 years, 11 months ago)

that helps a lot, i still haven't really learned how to bring in basic algebra into the equation.
do i have to define x as the color from the LED? or just the led itself? because the LED i have is a multi-pin one cause it's an RGB LED


Extras: Filter Print Post Top
Invisible404
error
 User Gallery


Registered: 08/20/10
Posts: 14,539
Re: Programming/coding with Arduino [Re: 404] * 1
    #21323338 - 02/24/15 03:45 PM (8 years, 11 months ago)

updated:


const int sensorPin = A0;    // select the input pin for the potentiometer
const int ledR = 5;
const int ledG = 6;
const int ledB = 7;// select the pin for the LED (or
int sensorValue = 0;  // variable to store the value coming from the sensor
const int pshBtn = 2;

// variables will change:
int buttonState = 0;        // variable for reading the pushbutton status


void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledR, OUTPUT);
  // declare pushbutton as an input
  pinMode(pshBtn, INPUT);
  //led will be a color and
 
}
int pshBtn = x;
void loop() {
  if (pshBtn) {
    ledR = x;
  x = x + 1; }
  if (x > 2) {
    then x = 0;
  }
}
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);   
  // turn the ledPin on
  digitalWrite(ledR, HIGH); 
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);         
  // turn the ledPin off:       
  digitalWrite(ledR, LOW); 
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);                 
}

now the question is how to implement the algebra with the pushbutton? if i do it that way, that prevents me from having to write the last 3rd of the coding out again for the other led colors, right?

also, it asked me to define "loadcolor" so i didn't know how to implement that part.


Extras: Filter Print Post Top
OfflineNova

Registered: 10/16/02
Posts: 1,365
Last seen: 5 years, 7 months
Re: Programming/coding with Arduino [Re: 404] * 1
    #21323680 - 02/24/15 04:58 PM (8 years, 11 months ago)

I have no idea how to program directly to hardware but ill try to help clear up some coding stuff.

the pushbutton would not be a constant. it would have to be a boolean input based on whether the button is pushed (true). So when the button is pressed, somehow (once again dont know hardware) the hardware needs to recognize this and the data needs to be input to the software.

A debug would be something just like to get a printout of 1 everytime the button is pressed so you know the software is recognizing when the button is pressed.

Instead of 'x' you should probably create a (not constant) int variable called Light_Cycle or something like that because 'x' will be confusing.

-variables-
bool Is_Button_Pushed;
int Light_Cycle = 0;

-Loop area-
(read input from button and load that into the is_button_pushed boolean default will be false but if it gets input it will need to be flagged true)
if (Is_Button_Pushed) {
(output to pin) Light_Cycle;
Light_Cycle = Light_Cycle + 1;
}
if (Light_Cycle > 2) {
Light_Cycle = 0;
}

So this is just looping, checking for input from the button, if there is it would go to the first pin (red) and then the variable would increase from 0 to 1 so that the next time it is pushed it would know to ouput to green. Then once its at 2 (blue) it will reset back to 0 (red).

The software is going to need to send some sort of output to the pin. Use the light_cycle (which is based on is_button_pressed) to output to the pin corresponding to the color. I can't help you much here because I have no idea how input and output to a circuit is handled.


Edited by Nova (02/24/15 05:01 PM)


Extras: Filter Print Post Top
Jump to top Pages: 1

Shop: PhytoExtractum Buy Bali Kratom Powder, Kratom Powder for Sale   Kraken Kratom Red Vein Kratom   Bridgetown Botanicals CBD Concentrates


Similar ThreadsPosterViewsRepliesLast post
* Excellent Article!!!!! (Marijuana Codes: What stoners can learn from Christians) Psilocybeingzz 1,887 11 04/11/07 09:27 AM
by memes
* Need a free audio editing program (that saves the files too) World Spirit 1,244 18 12/13/05 09:14 PM
by Simisu
* Need a program to make DVD covers... YouEnjoyMyself 962 14 01/28/14 04:00 AM
by cruelstone
* 404's RC/robotics/Arduino master thread
( 1 2 all )
404 1,190 29 06/15/16 03:10 PM
by 404
* Quick: Anyone know a postal code for any city in Maryland. Banez 732 8 12/14/06 07:43 PM
by Banez
* I need a Microsoft word-like program
( 1 2 all )
DIRTYMAN 1,938 24 02/22/07 07:05 PM
by VoidOfsPg
* I need an program
( 1 2 all )
Individual 1,455 24 06/13/07 12:07 PM
by WhiskeyClone
* I wan't to start programming LuNaTiX 550 11 07/28/07 11:57 PM
by Papaver

Extra information
You cannot start new topics / You cannot reply to topics
HTML is disabled / BBCode is enabled
Moderator: Entire Staff
586 topic views. 4 members, 35 guests and 22 web crawlers are browsing this forum.
[ Show Images Only | Sort by Score | Print Topic ]
Search this thread:

Copyright 1997-2024 Mind Media. Some rights reserved.

Generated in 0.026 seconds spending 0.008 seconds on 14 queries.