im having a problem, i just bought an arduino and i was wondering if anyone could help, here is my code. (i am just trying to get two leds to fade in and out).
int ledCount = 2;
int ledPins [ ] = {11,12 };
int brightness = 0;
int delayTime = 10;
void setup() {
pinMode(ledPins, OUTPUT);
}
void loop() {
while(brightness < 255)
{
analogWrite(ledPins, brightness);
delay(delayTime);
brightness = brightness + 1;
}
while(brightness > 0)
{
analogWrite(ledPins, brightness);
delay(delayTime);
brightness = brightness - 1;
}
}
You are passing an array to analogWrite or pinMode, where it is expecting a uint8_t.
Arduino pin manipulation functions will only handle a single pin at a time. There are ways around that, by directly manipulating the AVR/ARM GPIO registers, but those can be finicky (not recommended for use unless you really need speed).
The reason it says int * is because under the hood, arrays in C/C++ are represented as pointers.
If you want to analogWrite or pinMode to both LEDs, you will have to call the function once for each LED. Example:
analogWrite(ledPins[0], brightness);
analogWrite(ledPins[1], brightness);
Or
for(int currentLED = 0;currentLED < ledCount;i++){
analogWrite(ledPins[currentLED], brightness);
}
In the context of your program:
int ledCount = 2;
int ledPins [] = {11, 12};
int brightness = 0;
int delayTime = 10;
#define INCREASE 1
#define DECREASE 2
int brightness_change = INCREASE;
void setup(){
for(int i = 0;i < ledCount;i++){
pinMode(i, OUTPUT);
}
}
void loop(){
while(brightness < 255 && brightness_change == INCREASE){
brightness = brightness + 1;
}
while(brightness > 0 && brightness_change == DECREASE){
brightness = brightness - 1;
}
if(brightness == 255){
brightness_change = DECREASE;
}
if(brightness == 0){
brightness_change = INCREASE;
}
for(int current_led = 0;current_led < ledCount;current_led++){
analogWrite(current_led, brightness;
}
delay(delayTime;
}
Not tested, but it should work.
Related
I am using arduino Mega 2560 to control some vibration motors with touchdesigner through Serial Communication. I maped the pixels to control each motors, It works for a few seconds and gets stuck very soon. Is there anything wrong with my code?
Here is my arduino sketch:
#define MOTOR_COUNT 12
int motors[MOTOR_COUNT];
void setup()
{
Serial.begin(115200);
Serial.println("Ready to receive frames.");
for (int i = 0; i <= 12; i++) {
pinMode(i, OUTPUT);
}
void loop()
{
if (Serial.available())
{
char c = Serial.peek();
if (!(c >= '0' && c <= '9'))
{
Serial.read(); // Discard non-digit character
}
else if (Serial.read() == '\n')
{
for (uint16_t i = 0; i < MOTOR_COUNT; i++)
{
motors[i] = Serial.parseInt();
Serial.print("motor ");
Serial.print(i);
Serial.print(":");
Serial.println(motors[i]);
if (i <= 12) {
analogWrite(i + 2, motors[i]);
}
}
}
}
}
Ok, basically I'm trying to enable and disable voids in the code. Here is the code:
what I'm trying to create here is a dimmer. I want to enable and disable some of the voids to create that. Also when the value of "brightness" changes in one of the "for" loops, I can't transfer that value to other loops. Please help
int sensorPin = A0;
int sensorValue = 0;
int LED = 10;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
if ( analogRead < 30){
Down() = false;
}
if ( analogRead > 31) {
Up() = false;
}
}
void Up () {
for ( int brightness = 0; sensorValue < 30; brightness = brightness + 5 ) {
analogWrite(LED, brightness);
delay(100);
}
}
void Down () {
for ( int brightness = brightness; sensorValue > 31; brightness = brightness - 5 ) {
analogWrite(LED, brightness);
delay(100);
}
}
Still does not know what you want do.
But if I undestand it right (from the comments), the code should be:
int sensorPin = A0;
int sensorValue = 0;
uint8_t lightBrightness = 0;
int LED = 10;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
// Serial.println(sensorValue);
if ( sensorValue < 30){
if(lightBrightness < 255) lightBrightness++;
analogWrite(LED, lightBrightness );
}
if ( sensorValue > 31) {
if(lightBrightness > 0) lightBrightness--;
analogWrite(LED, lightBrightness );
}
delay(100); // << just to see fading the light
}
I am trying to set the timer after which the audio will automatically played once but it is not playing anything just a noise but when i run the audio program separately it work perfectly good
This is my code please help me out where i am doing wrong.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>
int timer1_counter;
#define SD_ChipSelectPin 4
TMRpcm tmrpcm;
unsigned long time_now = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ledPin = 13;
const byte interruptPin1 = 2;
const byte interruptPin2 = 3;
int counter2 = 0;
volatile byte state = LOW;
int count = 0;
int limit = 0;
bool TimerFlag = false;
int deviceTime = 0;
int set = 5;
bool soundplayflag = false;
void setup()
{
lcd.begin();
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(interruptPin1, INPUT_PULLUP);
pinMode(interruptPin2, INPUT_PULLUP);
pinMode(set, INPUT_PULLUP);
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Please Select:");
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 31250; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
// initialize timer1
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
if (soundplayflag == true)
{
counter2 = counter2+1;
Serial.println(counter2/2);
}
}
void loop()
{
//unsigned long currentMillis = millis();
int up = digitalRead(interruptPin1);
int down = digitalRead(interruptPin2);
int setbutton = digitalRead(set);
delay(230);
if (up == 0)
{
Serial.println("Entering up");
count++;
if (count >= 0) {
lcdprint(count);
}
}
else if (down == 0)
{
Serial.println("Entering down");
count--;
if (count >= 0) {
lcdprint(count);
}
}
else if (setbutton == 0)
{
Serial.println("Entering set");
soundplayflag=true;
deviceTime = count;
}
if (deviceTime > 0)
{
if (deviceTime == counter2)
{
soundplayflag=false;
Serial.println("Hello world");
PlaySound();
counter2 = 0;
}
}
}
int lcdprint(int a)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Please Select:");
lcd.setCursor (7,1);
lcd.print(a);
Serial.println(a);
lcd.setCursor (10,1);
lcd.print("Min");
}// end of lcdprint(int a)
void PlaySound()
{
for (int i = 0; i < 10; i++)
{
tmrpcm.setVolume(5);
tmrpcm.play("3.wav");
delay(1000);
}// end of for loop
}// end of void PlaySound()
my expected output is it should play the sound when i set the time
It seems TMRpcm uses Timer1, which conflicts with your TIMER1_COMPA_vect. That would explain why it's working if you run nothing else.
Maybe try to use #define USE_TIMER2?
This game has 4 leds and 4 buttons. the game is turning RANDOMLY those leds ON and OFF.
The player should be able to push the right button whenever he sees one led to be turned ON.
The leds should be turning ON and OFF with incerasing speed, so reaction time of a player is shorter and shorter
I have this code but i just know how to add more leds and buttons.
const int BUTTON1 = A0;
const int BUTTON2 = A1;
const int BUTTON3 = A2;
const int BUTTON4 = A3;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int ran;
int right = 0;
int ledOrder[9];
int guessOrder[9];
void setup()
{
Serial.begin(9600);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
pinMode(BUTTON3,INPUT);
pinMode(BUTTON4,INPUT);
}
void randomLed() {
for (int i = 0; i < 9; i++) {
ran = random(1,20);
if (ran < 11) {
digitalWrite(LED1,HIGH);
delay(500);
digitalWrite(LED1,LOW);
ledOrder[i] = 1;
}
else {
digitalWrite(LED2,HIGH);
delay(500);
digitalWrite(LED2,LOW);
ledOrder[i] = 2;
}
delay(500);
}
}
void btnClick() {
int ans = 0;
while (ans < 9) {
if (digitalRead(BUTTON1) == HIGH) {
guessOrder[ans] = 1;
ans++;
while (digitalRead(BUTTON1) == HIGH) {
}
}
else if (digitalRead(BUTTON2) == HIGH) {
guessOrder[ans] = 2;
ans++;
while (digitalRead(BUTTON2) == HIGH) {
}
}
}
}
void loop() {
Serial.print("Press button1 to start \n");
while (digitalRead(BUTTON1) == LOW) {
}
randomLed();
btnClick();
for (int i = 0; i < 9; i = i + 1) {
Serial.print("Guess: ");
Serial.print(guessOrder[i]);
Serial.print(" Answer: ");
Serial.print(ledOrder[i]);
if (guessOrder[i] == ledOrder[i]) {
Serial.print(" Right");
right++;
} else {
Serial.print(" Wrong");
}
Serial.print("\n ");
}
Serial.print(right);
Serial.print("/9\n");
delay(2000);
}
If I understand your question correctly, you would like to increase the led blink rate(when the LED's come on) each time the user gets the previous game right. If this is true you should try to put loop in for/if the user got the the previous one(game) right it will increase blink rate by a simple increment( I++;). Comment if you feel this is clarified enough or if your asking for something else
I have to make a program where a switch causes an led to change brightness each time the switch is clicked. The code below causes the LED to change brightness as needed but it does not stop changing if the switch is held down. I need it to only change once each time the switch is clicked. How do I make it change once when clicked and not loop through the other brightnesses?
const int ledPin = 10;
int brightness = 0;
boolean up = 1;
int steps = 0;
int inpin = 2;
int reading;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode (inpin, INPUT);
}
void loop() {
do{
reading = digitalRead(inpin);
}while (reading != HIGH);
analogWrite(ledPin, brightness);
delay(1000);
if (up)
{
brightness = brightness + 63;
}
else {
brightness = brightness - 63;
}
steps = steps + 1;
if (steps == 4)
{
up = !up;
steps = 0;
}
}
Once the brightness has been changed, wait for the switch to be released.