Using servo library in tinkercad causing weird behaviour - c++

I am using analogWrite() in my code. Everything works fine when I am not using the servo library. In other words, everything is fine when I disable this line //servo_9.attach(9); If I enable this line, the PWM will be wrong and weird. Any idea why the Tinkercad simulator is acting weird?
#include <Servo.h>
int led =9;
Servo servo_9;
void setup() {
servo_9.attach(9);
Serial.begin(9600);
}
void loop() {
for(int n=0;n<255;n++){
analogWrite(led,n);
delay(5);
}
}
This video shows what is happening:
click here

The documentation for the servo library clearly states.
On boards other than the Mega, use of the library disables
analogWrite() (PWM) functionality on pins 9 and 10, whether or not
there is a Servo on those pins.

Related

How to communicate with ATTiny85/Digispark via USB on PC?

Recently I'm trying to make a simple LED controller with ATTiny85/Digispark.
I tried to use DigiCDC lib to perform data IO
but it does not work on my PC (win10 x64).
test code:
#include <DigiCDC.h>
void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
SerialUSB.begin();
SerialUSB.println("hello world");
}
void loop()
{
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
delay(200);
digitalWrite(0, LOW);
digitalWrite(1, LOW);
delay(800);
SerialUSB.println("ping");
int ava = SerialUSB.available();
int buffer[ava];
// read buffer
for(int step = 0; step < ava; step++)
buffer[step] = SerialUSB.read();
// write buffer back
for(int step = 0; step < ava; step++)
SerialUSB.print(buffer[step]);
SerialUSB.println("==line end==");
delete buffer;
}
Official demo mentioned here (Arduino IDE - Files - Examples - DigiCDC - Echo) also did not work.
Once program is compiled and uploaded onto board, Windows shows a "Unknown USB device" notification. And no usable serial port devices can be found.
Are there missing some drivers?
Or DigiCDC lib is simply not working on Win10?
Or I should use another lib to achieve communication between PC and ATTiny85/Digispark via USB?
I would first check if the Blink sketch works to rule out driver issues. The ATTINY85 I have uses PIN '1' for the onboard LED. For reference, I got my devices here:
https://www.amazon.com/gp/product/B0836WXQQR/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&th=1
Also, make sure you don't plug the ATTINY into the USB port until the Arduino IDE asks for it. So, just click the 'Upload' button and wait for it to compile and ask for the board. I'm assuming you know how to set up the IDE to work with the ATTINY. If not, this tutorial goes over that part:
https://www.instructables.com/Attiny85-USB-Development-Board-LED-Blinking-With-A/

Arduino USB Serial communication is throwing error, "avrdude: ser_open(): can't set com-state for "\\.\COM3"

I am learning to program Arduino Uno board. To be honest, I am an absolute beginner to it and I just started learning it today. I have finished my first lesson on blinking an LED light. That went well without any issues.
Now, I am trying to send data from my Arduino board to another device through USB serial port communication. For now, I am following this tutorial to get the serial port software working. https://www.arduino.cc/en/Tutorial/LibraryExamples/SoftwareSerialExample.
So I am trying to compile the following code in the IDE before uploading to the Arduino board.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Native USB only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(38400);
mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}
When I compiled it, I am getting the following error.
An error occurred while uploading the sketch
avrdude: ser_open(): can't set com-state for "\\.\COM3"
What is wrong with my code and how can I fix it?

Arduino Uno Wifi library not working

I have recently purchased an Arduino Uno WIFI. It says it already has the ESP8266 wifi module integrated making it WIFI ready. I have successfuly connected to my wifi and wifi console. I have also used the test WebServer Blink test to play around with the pin 13 rest api commands. The problem im having is going beyond this example. I searched for WIFI documentation but can only find this documentation for the WIFI-Shield which is not working for my arduino.
I see in the example they import the #include <ArduinoWiFi.h> but i cannot find this libraries documentation. Is there anyother library I can use with this new arduino wifi? Does anyone have experience with this? I have tried to use the #include <WIFI.h> but it says that I don't have the wifi sheild.
ERROR:
WebServerBlink.ino:14:23: error: 'class ArduinoWifiClass' has no member named 'status'
CODE:
#include <Wire.h>
#include <ArduinoWiFi.h>
/*
on your borwser, you type http://<IP>/arduino/webserver/ or http://<hostname>.local/arduino/webserver/
http://labs.arduino.org/WebServerBlink
*/
void setup() {
pinMode(13,OUTPUT);
Wifi.begin();
Wifi.println("WebServer Server is up");
Wifi.println(Wifi.status()); //Line 14:23:: This will not work
}
void loop() {
while(Wifi.available()){
process(Wifi);
}
delay(50);
}
void process(WifiData client) {
// read the command
String command = client.readStringUntil('/');
// is "digital" command?
if (command == "webserver") {
WebServer(client);
}
if (command == "digital") {
digitalCommand(client);
}
}
void WebServer(WifiData client) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<head> </head>");
client.print("<body>");
client.print("Click<input type=button onClick=\"var w=window.open('/arduino/digital/13/1','_parent');w.close();\"value='ON'>pin13 ON<br>");
client.print("Click<input type=button onClick=\"var w=window.open('/arduino/digital/13/0','_parent');w.close();\"value='OFF'>pin13 OFF<br>");
client.print("</body>");
client.println("</html>");
client.print(DELIMITER); // very important to end the communication !!!
}
void digitalCommand(WifiData client) {
int pin, value;
// Read pin number
pin = client.parseInt();
// If the next character is a '/' it means we have an URL
// with a value like: "/digital/13/1"
if (client.read() == '/') {
value = client.parseInt();
digitalWrite(pin, value);
}
// Send feedback to client
client.print(F("Pin D"));
client.print(pin);
client.print(F(" set to "));
client.print(value);
client.print(EOL);
}
There is a big difference between Arduino Uno WIFI (http://www.arduino.org/products/boards/arduino-uno-wifi) from arduino.org and the Arduino WiFi Shield (www.arduino.cc/en/Main/ArduinoWiFiShield) from arduino.cc.
This is a good starting point for your Arduino Uno WIFI:
http://www.arduino.org/learning/getting-started/getting-started-with-arduino-uno-wifi
The next important point is, that you need to use Arduino 1.7 (from arduino.org) especially for OTA programming. Arduino 1.6.x from arduino.cc doesn't work.
Unfortunately they don't really develop their arduinowifi-library well.
I had the same problem on Linux IDE 1.8.1 and I solved it like this:
get https://github.com/arduino-org/Arduino/tree/master/libraries/ArduinoWiFi
add it in .....arduino-1.8.1/libraries/ArduinoWiFi/
restart the IDE. You shall be able to open and run the example sketch under File->Examples->ArduinoWiFi.
I suppose that it will work with any IDE on any platform.

Is there a clean way of disabiling RX control from USBCore in leonardo?

At the moment I'm trying to use the sparkfun promicro to control the RX pin at will using this sketch https://www.sparkfun.com/tutorials/338 .
However I have run into a problem where despite having control over the RX and TX led, it is being interfered by the USBCore.cpp in arduino. I am wondering if there is a clean way to disable control by USBCore over RX and TX pins, while still leaving the USB serial alone, so that I can control these pins directly, even while receiving and sending serial data.
/* Pro Micro Test Code
by: Nathan Seidle
modified by: Jim Lindblom
SparkFun Electronics
date: January 20, 2012
license: Public Domain - please use this code however you'd like.
It's provided as a learning tool.
This code is provided to show how to control the SparkFun
ProMicro's TX and RX LEDs within a sketch. It also serves
to explain the difference between Serial.print() and
Serial1.print().
*/
int RXLED = 17; // The RX LED has a defined Arduino pin
// The TX LED was not so lucky, we'll need to use pre-defined
// macros (TXLED1, TXLED0) to control that.
void setup()
{
pinMode(RXLED, OUTPUT); // Set RX LED as an output
// TX LED is set as an output behind the scenes
Serial.begin(9600); //This pipes to the serial monitor
Serial1.begin(9600); //This is the UART, pipes to sensors attached to board
}
void loop()
{
Serial.println("Hello world"); // Print "Hello World" to the Serial Monitor
Serial1.println("Hello!"); // Print "Hello!" over hardware UART
digitalWrite(RXLED, HIGH); // set the LED on
TXLED1; //TX LED is not tied to a normally controlled pin
delay(1000); // wait for a second
digitalWrite(RXLED, LOW); // set the LED off
TXLED0;
delay(1000); // wait for a second
}
If there is no way to tackle this cleanly without modifying the arduino enviroment, then I'll shall modify USBCore.cpp . However its probbly bad practice to do so.
If its possible in your scenario, you could use pin 17 as an INPUT, hopefully freeing up another pin which you can then use as an OUTPUT.
To do this, just use pinMode() to set pin 17 to an INPUT.
That effectively disables the RXLED functionality. When USBCore write a high to that pin, it merely turns on the pullup resistor. As long as the device driving the input can sink enough current even when the pullup is on, this will have no effect. And so there is no need to modify USBCore.
Edit: The LED lights up when pin 17 is low, which means the source of the signal needs to sink current. That can be avoided if its an issue by severing the PCB track next to the LED, or by desoldering the LED or resistor.

Arduino servo not responding to button press

I'm pretty sure there is a stupid error here but I'm afraid I can't for the life of me work it out!
Simple test program that gets the error:
#include <Servo.h>
Servo myservo;
int testPIN = 13;
int inputPIN = 5;
void setup()
{
myservo.attach(8);
pinMode(testPIN, OUTPUT);
pinMode(inputPIN, INPUT);
}
void loop()
{
if (digitalRead(inputPIN) == HIGH)
{
digitalWrite(testPIN, HIGH);
myservo.write(90);
}
else
{
digitalWrite(testPIN, LOW);
myservo.write(0);
}
}
The arduino sweep example (http://arduino.cc/en/Tutorial/Sweep) works, so I'm fairly confident the electronics works.
The testPIN also goes on and off as expected so the if statement is working as expected.
Any ideas/suggestions welcome!
EDIT - Sorry the error is that the servo doesn't move at all
EDIT 2 - Something a bit odd is going on here. If I copy/paste the sweep loop into the if clause, the servo reacts as expected (ie input = high makes the servo run a sweep loop, which it doesn't break out of until it reaches the end of, as expected). My immediate thought was delays were needed, but they seem to make no difference no matter how long they are or where they are added in the if/else clauses.
I don't know which arduino board you have, but on the arduino uno, i'm pretty sure that the pin 8 is not a PWM output. And you can not run a servo on a non-PWM output.
See this image of the Uno board, and notice that there is no tilde (the indication that a port supports PWM) on pin 8:
Have you tried using SoftwareServo.h instead? This example looks like what you are trying to accomplish: http://playground.arduino.cc/ComponentLib/servo
The sweep program that you're linking to is using pin 9, which is a PWM on an uno. Your code is using pin 8, which is NOT a PWM output. Switch your servo over to pin 9 and change the attach in your code to pin 9 and, assuming that this is your only issue, your code should work.
As suggested in the comments, I've just written a function that moves the servo slowly. Not an elegant solution but servo response time isn't issue so it does the trick.
Thanks for all the help and suggestions, and to #praks411 for the wrapper function work around.