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

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);

Related

how do i read and output pwm with a black pill board?

I am making an stm32 based motor speed controller for a DC motor. But I can't read PWM off my receiver and I can't make my MOSFET vary the output! I need help because now it's only on or off! I am using a RobotDyn BlackPill and an STP36NF06L Mosfet. And also I use Arduino ide with the STM board.
Code:
int Motor = PA15;
int rc = PB1;
int s;
void setup() {
pinMode(Motor, OUTPUT);
pinMode(rc, INPUT);
digitalWrite(Motor, LOW);
}
void loop() {
if(s = (map(pulseIn(rc, HIGH), 1100, 1900, 0, 255)) > 200) {
digitalWrite(Motor, HIGH);
}
else{
digitalWrite(Motor, LOW);
}
delay(10);
}
If you want to read an external PWM signal you can use a timer in input capture mode, such that one channel triggers on the rising edge and the other triggers on the falling edge then compute the period and as such the frequency and duty cycle of the incoming signal. It is well documented in the reference manual and there are various examples and even YouTube tutorials of input capture mode.

How to communicate with HM-19 BLE Module and scan using ultrasonic sensor

I am working on my senior project for school, and part of what I need to do is use an HM-19 Bluetooth 5.0 module to connect to another Bluetooth 5.0 module and establish a master slave connection.
I can do that just fine, but when I include the code needed for my ultrasonic sensor to do scan, my commands to my HM-19 don't return anything and I can't do any of the basic functions such as finding connections. I have tested it with and without the ultrasonic sensor code and the problem occurs when I use the sensor portion of the code.
TO BE CLEAR, all I am trying to do is just have my Bluetooth 5.0 chip connect to another and do normal commands while also inputting into my serial monitor a distance when I put my hand in front. THIS IS JUST A TEST, once I get that done I will move to what I really want to do.
IT'S JUST A STARTING POINT IN A PROJECT. I have a function call for my sensor and my bluetooth chip in the void loop, that's all that is in there.
I just want to know how to fix this issue. How can I scan with my ultrasonic sensor and send commands to my Bluetooth module? Any help would be greatly appreciated.
[Here are the results when the sensor is commented][1] and [here are the unsuccessful results that results in an infinite loop where I can't get to the portion of the code that returns what the chip says][2]. Lastly, although most of the links include stuff for the HM-10, the commands are basically the same for the HM-19. I'm adding more because stack overflow won't let me edit this post until there are more characters or something. I hope you have a good day/evening person reading this.
Here is my code:
// SerialIn_SerialOut_HM-10_01
//
// Uses hardware serial to talk to the host computer and AltSoftSerial for communication with the bluetooth module
//
// What ever is entered in the serial monitor is sent to the connected device
// Anything received from the connected device is copied to the serial monitor
// Does not send line endings to the HM-10
//
// Pins
// BT VCC to Arduino 5V out.
// BT GND to GND
// Arduino D8 (SS RX) - BT TX no need voltage divider
// Arduino D9 (SS TX) - BT RX through a voltage divider (5v to 3.3v)
//
#include <AltSoftSerial.h>
AltSoftSerial BTserial;
// https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
char c=' ';
boolean NL = true;
const int trigPin = 9;
const int echoPin = 10;
float duration, distance;
boolean wait_your_turn = false; //My attempt to make sure the sensor and the Bluetooth module don't interfere with each other
//if I'm sending data from the serial monitor to the bluetooth module and vice versa it switches to true and the bluetooth module
//does its thing, so the sensor doesn't get in the way.
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
Serial.print("Sketch: "); Serial.println(__FILE__);
Serial.print("Uploaded: "); Serial.println(__DATE__);
Serial.println(" ");
BTserial.begin(9600);
Serial.println("BTserial started at 9600");
}
void loop()
{
Bluetooth();
Sensor();
}
void Sensor(){
if((wait_your_turn == true))
{}
else
{
Serial.println("Scanning for stuff.");
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
if(distance <= 20)
{
Serial.println(distance);
delay(500);
}
}
}
void Bluetooth()
{
if (Serial.available())
{
if(wait_your_turn == false)
Serial.println("Serial is available");
wait_your_turn = true;
while(Serial.available()>0)
c = Serial.read();
Serial.write(c);
if(c!=10&c!=13)
BTserial.print(c);
}
if (BTserial.available())
{
// Serial.print("We are at the bluetooth portion.");
while(BTserial.available())
c = BTserial.read();
Serial.print(c);
wait_your_turn = false;
}
}
[1]: https://i.stack.imgur.com/Dn4i0.png
[2]: https://i.stack.imgur.com/s9Ifv.png
Sorry, I forgot about this question. I figured it out. What I did was have 1 Arduino control the Ultrasonic sensor and send a character to the other Arduino when something was in range of the sensor. The other Arduino would then read the character and based on the character send it would perform an action. Thank you everyone who commented and have a great rest of your days.

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

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)); //
}

C code equivalent to serial monitor commands like Serial.begin,serial.flush,serial.read,serial.available,etc

Hi I am trying to learn coding microcontrollers on my own. I am trying to code my arduino board (ATMEGA8A-PU) in embedded C using the arduino ide itself. I have blinked my LED so far. Now I am trying to control its state using the serial monitor(sending "on" lights it up and "off" switches it off). But I don't know the C commands to do it.I successfully did it using the arduino Serial commands.
int led = 13; // Pin 13
void setup()
{
pinMode(led, OUTPUT); // Set pin 13 as digital out
// Start up serial connection
Serial.begin(9600); // baud rate
Serial.flush();
}
void loop()
{
String input = "";
// Read any serial input
while (Serial.available() > 0)
{
input += (char) Serial.read(); // Read in one char at a time
delay(5); // Delay for 5 ms so the next char has time to be received
}
if (input == "on")
{
digitalWrite(led, HIGH); // on
}
else if (input == "off")
{
digitalWrite(led, LOW); // off
}
}
So please help.
You are using Serial class and functions like begin(), print() etc from the Serial class which is C++ language. These are the C++ commands native to the Arduino development environment. So anyway you ar using C/C++ commands here.

Powering DC Motor with Spark Core and arduino motor shield R3

I am trying to run a motor shield without the shield shield with a spark core. I got an Arduino to run the motor shield using this code :
int a = 12;
int abrake = 9;
int aspeed = 3;
void setup()
{
// Initialize D0 pin as output
pinMode(a, OUTPUT);
pinMode(abrake, OUTPUT);
}
// This routine loops forever
void loop()
{
digitalWrite(a, HIGH);
digitalWrite(abrake, LOW);
analogWrite(aspeed, 225);
}
I then wiped the arduino so it wasn't running any code but was powering the shield.
Then I tried to use a spark core to control the motor shield.
I hooked
D0 -> 12
D1 -> 9
D0 -> 3
And used this code :
int a = D0;
int abrake = D1;
int aspeed = D2;
void setup()
{
// Initialize D0 pin as output
pinMode(a, OUTPUT);
pinMode(abrake, OUTPUT);
}
// This routine loops forever
void loop()
{
digitalWrite(a, HIGH);
digitalWrite(abrake, LOW);
analogWrite(aspeed, 225);
}
My wiring :
Is this way even possible?
Am i doing something wrong?
Is there an easier solution?
AnalogWrite to a Digital Pin will only work on D0 and D1 since they have PWM capabilities only. I am guessing it is just ignoring the analog write. Get out the old meter and check.
Here is a link to their super helpful docs.
http://docs.spark.io/hardware/#spark-core-datasheet-pins-and-i-o

Resources