Arduino & Processing 3: "Dimmer" Built-In Example - arduino

Okay, so I'm using an Arduino Uno on a Windows 10 PC, as well as Processing 3. I'm attempting to follow the directions on the Arduino website for controlling the brightness of an LED by moving my across the PC screen, by having the Arduino software cooperate w/ Processing. (Here's the link to the project I'm talking about: https://www.arduino.cc/en/Tutorial/Dimmer) It seems so simple, yet it's not quite working. The LED will glow, but its brightness won't vary with the mouse's X position, and its glow is flickering. I get no error codes.
As regards the circuit, I have an LED connected to PWM pin 9, with a 200-ohm resistor to ground; I am certain that the polarity of the LED is correctly set up. The website isn't clear how exactly Processing & the Arduino software should collaborate to make this happen. (So I would greatly appreciate an explanation thereof if possible.) The Arduino & Processing codes are below. I don't understand what I'm doing wrong?
Here is my Arduino code:
const int ledPin = 9; // the pin that the LED is attached to
void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
byte brightness;
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
brightness = Serial.read();
// set the brightness of the LED:
analogWrite(ledPin, brightness);
}
}
Here's my Processing code:
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
void setup() {
size(256, 150);
arduino = new Arduino(this, "COM3", 9600);
}
void draw() {
background(constrain(mouseX, 0, 255));
arduino.analogWrite(9, constrain(mouseX, 0, 255)); //
}

For those of you who wish to know, here is my functional code:
Arduino portion:
const int ledPin = 9; // the pin that the LED is attached to
void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
int brightness;
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
brightness = Serial.read();
// set the brightness of the LED:
analogWrite(9, brightness);
}
delay(10);
}
Processing portion (on Processing 3):
import processing.serial.*;
import cc.arduino.*;
Serial arduino;
void setup() {
size(256, 150);
arduino = new Serial(this, "COM3", 9600);
}
void draw() {
background(constrain(mouseX, 0, 255));
arduino.write(constrain(mouseX, 0, 255)); //
}

Related

Arduino sleep while digital pin is HIGH

I want to make the Arduino Pro Mini run on a 3.7v (4.2v when fully charged) LI-Ion battery.
The project is I will use an IR sensor to control the relay. Based on the IR code received, I will toggle the relay digital output pin (HIGH or LOW). Initially, the arduino is set to deep sleep mode and when it receives an External Interrupt (pin 2 on pro-mini), it will process the IR code and switch on the relay.
//Interrupts using Arduino
//Circuit Digest
#include "LowPower.h"
#include <IRremote.h>
volatile int output = LOW;
int i = 0;
#define RECV_PIN 2
volatile boolean sleepEnabled = true;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
irrecv.enableIRIn();
pinMode(RECV_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(RECV_PIN), buttonPressed1, RISING); // function for creating external interrupts at pin2 on Rising (LOW to HIGH)
}
void loop()
{
Serial.println(i);
++i;
delay(1000);
output = LOW;
digitalWrite(13, output); //Turns LED ON or OFF depending upon output value
if (sleepEnabled == true) {
Serial.println("Going to sleep");
delay(1000);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
}
delay(500);
readIR();
}
void buttonPressed1() //ISR function excutes when push button at pinD2 is pressed
{
sleepEnabled = false;
}
void readIR() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
if (results.value == 0xff4ab5) {
sleepEnabled = true;
}
}
irrecv.resume(); // Receive the next value
}
When the pro-mini goes back to sleep, everything is turned off.
How can I make the pro-mini consume minimal power while the relay pin is HIGH ?

Joystick with switch control for Arduino Uno R3 will not display switch push

I am trying to get my code to show a switch input from a joystick on an Arduino Uno.
When I view it on the serial monitor, I see the x and y axis being displayed when I move the joystick.
When I click it I do not get anything to show up. I have the switch set to pin 2 on the Arduino.
There are no errors when I compile the code.
int pin_x = A0;
int position_x = 0;
int pin_y = A1;
int position_y = 0;
int pin_z = 2; // switch pin
int position_z = LOW;
void setup() {
Serial.begin(9600); // initialize serial communications at 9600 bps
pinMode(pin_x, INPUT); // set pin mod as INPUT
pinMode(pin_y, INPUT);
pinMode(pin_z, INPUT); // switch pin
}
void read(){
position_x = analogRead(pin_x);
position_y = analogRead(pin_y);
position_z = digitalRead(pin_z);
}
void show(){
Serial.print(" X:"); //print information to Serial Monitor
Serial.print(position_x);
Serial.print(" Y:");
Serial.print(position_y);
Serial.print(" Z:");
Serial.print(position_z);
}
void loop() {
read();
show();
delay(500);
}
There are several methods you could use to find the error, here are a couple suggestions:
read the datasheet of your joystick
use a multimeter to check conductivity of the two switch pins
hook something else to your switch input, check that the input is working
That said, the way your code is written it expects your switch pin to be connected to Vcc, check if that is really the case.
Try this:
void setup() {
Serial.begin(9600); // initialize serial communications at 9600 bps
pinMode(pin_x, INPUT); // set pin mod as INPUT
pinMode(pin_y, INPUT);
pinMode(pin_z, INPUT); // switch pin
digitalWrite(pin_z, HIGH);
}
Otherwise code looks correct. The switch on a Joystick is indeed digital, while the others are analog, you got that right.
Here's a video on how to work with joysticks, maybe it helps: https://www.youtube.com/watch?v=MlDi0vO9Evg

Send a Analog Value from Arduino A to Analog Pin of Arduino B

I'm trying to Read values from LM35 sensor on Arduino UNO and send it to another Arduino board through a PWM pin and an analog Pin
When I run this project, The Serial Emulator of Arduino A is showing right values but Second one is always 0.00.
Here is my first Arduino Code:
int pin = 2;
int TempPin = A0;
int pinAnalog = 3;
void setup() {
pinMode(3, OUTPUT);
Serial.begin(9600);
}
void loop() {
float tmp = analogRead(TempPin);
float Result = (tmp/1024.0) * 500;
Serial.println(Result);
analogWrite(pinAnalog, Result);
delay(3000);
}
And Here is My Second Arduino Code:
void setup() {
Serial.begin(9600);
}
void loop() {
float res = analogRead(A0);
Serial.println(res);
delay(3000);
}
What's wrong with my project or code?
I understand this is an exercise only, as PWM itself is not suitable to feed analogRead. (better measure pulse durations, if you really want to use it for data transmission.)
For a 400 Hz PWM you need a RC Value of e.g. 20 ms to filter the PWM pulses reasonably.
(e.g 1µF * 20k)
As you work in a 3sec Cycle, much bigger values are fine as well.
BTW: Sender could be simplified to:
const byte inPin = A0;
const byte outPin = 3;
void setup() {
Serial.begin(9600);
}
void loop() {
byte tmp = analogRead(inPin)/4; // 0 .. 255
analogWrite(outPin, tmp);
Serial.println((int)tmp);
delay(3000);
}

Using Arduino library in Processing and enabling internal pull-up resistor

I am trying to made this project and using an Arduino library in Processing (Firmata) for serial communication. For this project, it requires a pull-up resistor, which Arduino analog pins also have, and need to be enabled with code. While I tried a lot of times it's still not working. I am wondering, is the Arduino library on processing capable for enable pull-up resistor? Has anyone done that before?
My sensor is connected to 5v on one end, and on the other is ground and A0.
I uploaded standardFirmata to Arduino. Here is my processing code:
import cc.arduino.*;
import org.firmata.*;
import processing.serial.*;
Arduino arduino;
int A1;
int A2;
void setup() {
size(800, 500);
arduino = new Arduino(this, Arduino.list()[1], 57600);
arduino.pinMode(A1, Arduino.INPUT_PULLUP);
arduino.pinMode(A2, Arduino.INPUT_PULLUP);
}
void draw() {
background(255);
stroke(0);
if ((arduino.analogRead(A1) != 0) {
rect(150, 100, 100, 300);
fill(#BFA4E5);
}
if (arduino.analogRead(A2) != 0) {
rect(250, 100, 100, 300);
fill(#BFA4E5);
}
}
You could enable your pull-up resistor with:
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);

Send IR values using infrared emitter led on Arduino

i have Arduino Mega and an IR Emitting LED and i want to send data "Hex Data" that i choose using this LED and i have tried the IRRemote Library and i have successfully used the IRrecv class, but when using IRsend i didn't get any signal and have tried to look at the led through the mobile camera
the IR Emitter Pin is PWM 3 and have connected it to 3.3V once and to 5V once
#include <IRremote.h>
IRsend irsend;
void setup()
{
Serial.begin(9600);
}
void loop() {
if (Serial.read() != -1) {
for (int i = 0; i < 3; i++) {
irsend.sendSony(0xa90, 12); // Sony TV power code
delay(40);
}
}
}
and for the receiver:
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
}
any help is appreciated :) Hiso
i Have looked at the IRRemote.cpp library you refereed to and in the header file you can see that each Arduino board have a unique PWM pin that is used to transmit infrared data so use PWM 9 it's assured to work on Arduino Mega

Resources